home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume3 / egetopt / demo.c next >
C/C++ Source or Header  |  1989-02-03  |  2KB  |  80 lines

  1. #include <stdio.h>
  2.  
  3. /*
  4.  * This is a short program for demonstrating the capabilities of egetopt().
  5.  * Run it with various combinations of options and arguments on the command
  6.  * line to see how egetopt() works.
  7.  *
  8.  * Experiment around with this by changing some of my settings and
  9.  * recompiling.
  10.  */
  11.  
  12. #define OPT_STRING    "abc~d~e?f?"
  13. /* Meaning:
  14.  *
  15.  * -a and -b take no arguments.
  16.  * -c and -d take mandatory arguments (I set 'optneed' to '~', below).
  17.  * -e and -f take optional arguments (I set 'optmaybe' to '?', below).
  18.  */
  19.  
  20. #define OPT_CHARS    "-+="
  21. /* Meaning:
  22.  *
  23.  * Options can begin with '-', '+', or '='.
  24.  */
  25.  
  26.  
  27. /*
  28.  * New global variables used in egetopt() only:
  29.  */
  30. extern int optneed;    /* character used for mandatory arguments */
  31. extern int optmaybe;    /* character used for optional arguments */
  32. extern int optchar;    /* character which begins a given argument */
  33. extern int optbad;    /* what egetopt() returns for a bad option */
  34. extern int opterrfd;    /* where egetopt() error messages go */
  35. extern char *optstart;    /* string which contains valid option start chars */
  36.  
  37. /*
  38.  * Global variables which exist in getopt() and egetopt():
  39.  */
  40. extern int optind;    /* index of current argv[] */
  41. extern int optopt;    /* the actual option pointed to */
  42. extern int opterr;    /* set to 0 to suppress egetopt's error messages */
  43. extern char *optarg;    /* the argument of the option */
  44.  
  45. main(argc, argv)
  46. int argc;
  47. char **argv;
  48. {
  49.     int ch;
  50.  
  51.     opterrfd = fileno(stdout);    /* errors to stdout */
  52.     opterr = 0;        /* set this to 1 to get egetopt's error msgs */
  53.     optbad = '!';        /* return '!' instead of '?' on error */
  54.     optneed = '~';        /* mandatory arg identifier (in OPT_STRING) */
  55.     optmaybe = '?';        /* optional arg identifier (in OPT_STRING) */
  56.     optstart = OPT_CHARS;    /* characters that can start options */
  57.  
  58.     while ((ch = egetopt(argc, argv, OPT_STRING)) != EOF) {
  59.         printf("\n\toption index (optind) after egetopt(): %5d\n",
  60.             optind);
  61.         printf("\t\tegetopt() return value:            %c (%d)\n",
  62.             ch, ch);
  63.         printf("\t\tchar that begins option (optchar): %c\n",
  64.             optchar);
  65.         printf("\t\tactual char looked at (optopt):    %c\n",
  66.             optopt);
  67.         printf("\t\toption argument:                   \"%s\"\n",
  68.             optarg == NULL ? "(null)" : optarg);
  69.     }
  70.  
  71.     for (; optind < argc; ++optind) {
  72.         printf("\n\targument index                         %5d\n",
  73.             optind);
  74.         printf("\t\targument:                          \"%s\"\n",
  75.             argv[optind] == NULL ? "(null)" : argv[optind]);
  76.     }
  77.  
  78.     exit(0);
  79. }
  80.