Wednesday, September 15, 2010

Processing input arguments in C program

Parsing Sort options:
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

void usage (const char *programName)
{
  printf ("Usage: %s [-kldeh]\n"
          "          -k <val_k>   Value for the the option -k\n"
          "          -l <val_l>   Value for the the option -l\n"
          "          -d           Does not have any value\n"
          "          -e           Does not have any value\n"
          "          -h           Print this message\n\n",
          programName);

  exit (EXIT_SUCCESS);
}

int main (int argc, char **argv)
{
  char *val_k = NULL;
  int index = 0;
  int c = 0;
  
  if (1 == argc)
  {
    printf ("This tool needs at lease one input arguments.\n");
    usage (argv[0]);
  }

  /* Check the input arguments/options */
  while (-1 != (c = getopt (argc, argv, "k:l:deh")))
  {
    switch (c)
    {
      case 'k':
        /* Assign the optarg pointer to a local variable for further use. */
        val_k = optarg;
        printf ("value for the option [k] is \"%s\"\n", val_k);
        break;
      case 'l':
        /* We can use the variable optarg directly too. */
        printf ("value for the option [l] is \"%s\"\n", optarg);
        break;
      case 'd':
        printf ("option [d] is given.\n");
        break;
      case 'e':
        printf ("option [e] is given.\n");
        break;
      case 'h':
        usage (argv[0]);
      case '?':
        if (isprint (optopt))
          fprintf (stderr, "Unknown option `-%c'.\n", optopt);
        else
          fprintf (stderr,
                   "Unknown option character `\\x%x'.\n",
                   optopt);
        usage (argv[0]);
      default:
        abort ();
    }
  }

  for (index = optind; index < argc; index++)
  {
    printf ("Extra arguments [%d] --> %s\n", index, argv[index]);
  }

  return 0;
}


Parsing Long options:

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <getopt.h>

static struct option options[] = {
  /* These options set a flag.   */
  {"option_k", 1, 0, 'k'},
  {"option_l", 1, 0, 'l'},
  {"option_d", 0, 0, 'd'},
  {"option_e", 0, 0, 'e'},
  {"help", 0, 0, 'h'},
  {0, 0, 0, 0}
};

void usage (const char *programName)
{
  printf ("Usage: %s [-kldeh]\n"
          "          -k or --option_k <val_k>   Value for the the option -k\n"
          "          -l or --option_l <val_l>   Value for the the option -l\n"
          "          -d or --option_d           Does not have any value\n"
          "          -e or --option_e           Does not have any value\n"
          "          -h or --help               Print this message\n\n",
          programName);

  exit (EXIT_SUCCESS);
}

int main (int argc, char **argv)
{
  char *val_k = NULL;
  int index = 0;
  int c = 0;
  int option_index = 0;

  if (1 == argc)
  {
    printf ("This tool needs at lease one input arguments.\n");
    usage (argv[0]);
  }

  /* Check the input arguments/options */
  while (-1 != (c = getopt_long (argc, argv, "k:l:deh",
                                 options, &option_index)))
  {
    switch (c)
    {
      case 'k':
        /* Assign the optarg pointer to a local variable for further use. */
        val_k = optarg;
        printf ("value for the option [k] is \"%s\"\n", val_k);
        break;
      case 'l':
        /* We can use the variable optarg directly too. */
        printf ("value for the option [l] is \"%s\"\n", optarg);
        break;
      case 'd':
        printf ("option [d] is given.\n");
        break;
      case 'e':
        printf ("option [e] is given.\n");
        break;
      case 'h':
        usage (argv[0]);
      case '?':
        if (isprint (optopt))
          fprintf (stderr, "Unknown option `-%c'.\n", optopt);
        else
          fprintf (stderr,
                   "Unknown option character `\\x%x'.\n",
                   optopt);
        usage (argv[0]);
      default:
        abort ();
    }
  }

  for (index = optind; index < argc; index++)
  {
    printf ("Extra arguments [%d] --> %s\n", index, argv[index]);
  }

  return 0;
}

Ref: http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_22.html