home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / dos / strings / c_string / getopt.doc < prev    next >
Encoding:
Text File  |  1986-04-09  |  3.3 KB  |  149 lines

  1.  
  2. /*f File   : getopt.doc
  3. .TH GETOPT 3 local
  4. .DA 25 March 1982
  5. .SH NAME
  6. getopt \- get option letter from argv
  7. .SH SYNOPSIS
  8. .ft B
  9. int getopt(argc, argv, optstring)
  10. .br
  11. int argc;
  12. .br
  13. char **argv;
  14. .br
  15. char *optstring;
  16. .sp
  17. extern char *optarg;
  18. .br
  19. extern int optind;
  20. .ft
  21. .SH DESCRIPTION
  22. .I Getopt
  23. returns the next option letter in
  24. .I argv
  25. that matches a letter in
  26. .IR optstring .
  27. .I Optstring
  28. is a string of recognized option letters;
  29. if a letter is followed by a colon, the option is expected to have
  30. an argument that may or may not be separated from it by white space.
  31. .I Optarg
  32. is set to point to the start of the option argument on return from
  33. .IR getopt .
  34. .PP
  35. .I Getopt
  36. places in
  37. .I optind
  38. the
  39. .I argv
  40. index of the next argument to be processed.
  41. Because
  42. .I optind
  43. is external, it is normally initialized to zero automatically
  44. before the first call to
  45. .IR getopt .
  46. .PP
  47. When all options have been processed (i.e., up to the first
  48. non-option argument),
  49. .I getopt
  50. returns
  51. .BR EOF .
  52. The special option
  53. .B \-\-
  54. may be used to delimit the end of the options;
  55. .B EOF
  56. will be returned, and
  57. .B \-\-
  58. will be skipped.
  59. .SH SEE ALSO
  60. getopt(1)
  61. .SH DIAGNOSTICS
  62. .I Getopt
  63. prints an error message on
  64. .I stderr
  65. and returns a question mark
  66. .RB ( ? )
  67. when it encounters an option letter not included in
  68. .IR optstring .
  69. .SH EXAMPLE
  70. The following code fragment shows how one might process the arguments
  71. for a command that can take the mutually exclusive options
  72. .B a
  73. and
  74. .BR b ,
  75. and the options
  76. .B f
  77. and
  78. .BR o ,
  79. both of which require arguments:
  80. .PP
  81. .RS
  82. .nf
  83. main(argc, argv)
  84.     int argc;
  85.     char **argv;
  86.     {
  87.         int c;
  88.         extern int optind;
  89.         extern char *optarg;
  90.         \&.
  91.         \&.
  92.         \&.
  93.         while ((c = getopt(argc, argv, "abf:o:")) != EOF) {
  94.             switch (c) {
  95.                 case 'a':
  96.                     if (bflg) errflg++; else aflg++;
  97.                     break;
  98.                 case 'b':
  99.                     if (aflg) errflg++; else bflg++;
  100.                     break;
  101.                 case 'f':
  102.                     ifile = optarg;
  103.                     break;
  104.                 case 'o':
  105.                     ofile = optarg;
  106.                     break;
  107.                 case '?':
  108.                 default:
  109.                     errflg++;
  110.                     break;
  111.             }
  112.         }
  113.         if (errflg) {
  114.             fprintf(stderr, "Usage: ...");
  115.             exit(2);
  116.         }
  117.         for (; optind < argc; optind++) {
  118.             \&.
  119.             \&.
  120.             \&.
  121.         }
  122.         \&.
  123.         \&.
  124.         \&.
  125.     }
  126. .RE
  127. .PP
  128. A template similar to this can be found in
  129. .IR /usr/pub/template.c .
  130. .SH HISTORY
  131. Written by Henry Spencer, working from a Bell Labs manual page.
  132. Behavior believed identical to the Bell version.
  133. .SH BUGS
  134. It is not obvious how
  135. `\-'
  136. standing alone should be treated; this version treats it as
  137. a non-option argument, which is not always right.
  138. .PP
  139. Option arguments are allowed to begin with `\-';
  140. this is reasonable but reduces the amount of error checking possible.
  141. .PP
  142. .I Getopt
  143. is quite flexible but the obvious price must be paid: there is much
  144. it could do that it doesn't, like
  145. checking mutually exclusive options, checking type of
  146. option arguments, etc.
  147. */
  148.  
  149.