3 subgetopt \- get option character from command line
5 .B #include <subgetopt.h>
15 int \fBsgoptproblem\fP;
17 int \fBsgopt(\fP\fIargc,argv,opts\fR\fB)\fP;
26 returns the next valid command-line option character
30 Valid option characters are listed in the
37 may be followed by a colon,
40 .I option argument\fR.
41 Avoid using the characters ?, :, and \- as option characters.
49 .I command-line argument
53 Options are listed in cmdargs which begin with
55 Several options which do not take optargs may be combined
58 An option which takes an optarg may be handled in two ways.
59 If it appears at the very end of a cmdarg,
60 then the entire next cmdarg is the optarg.
61 But if there are any characters in the cmdarg
62 after the option character,
63 then those characters form the optarg.
64 The optarg is returned in
68 looks at the cmdarg which follows the optarg.
70 If a cmdarg does not begin with a hyphen,
71 or if it is a lone hyphen not followed by any characters,
72 or if it begins with two hyphens,
73 then it terminates option processing,
76 returns an appropriate code.
77 If there are two hyphens,
79 will advance attention to the next cmdarg,
80 so it can be called again to read further options.
83 should be used only when
89 are defined all the time.
91 is defined all the time;
94 has just returned an option with optarg.
97 is typically used as follows.
100 #include <subgetopt.h>
102 main(argc,argv) int argc; char **argv; { int opt;
104 while ((opt = sgopt(argc,argv,"a:s")) != sgoptdone)
110 printf("opt a with optarg %s\\n",sgoptarg); break;
114 printf("opt s with no optarg\\n"); break;
118 if (argv[sgoptind] && (sgoptind < argc))
120 printf("illegal opt %c\\n",sgoptproblem);
124 printf("missing arg, opt %c\\n",sgoptproblem);
132 while (*argv) printf("argument %s\\n",*argv++);
139 The end of the command line is
144 whichever comes first.
146 these two markers coincide,
152 .B sgoptind < \fIargc\fR.
153 The above code shows both tests as an illustration.
155 .B Multiple option sets:
156 One useful technique is to call
160 until it returns EOF,
165 until it returns EOF.
166 The user can provide primary options, then a double hyphen,
167 and then secondary options.
168 No special handling is needed if some or all of the options are
170 The same technique can be used for any number of option sets
173 .B Multiple command lines:
185 keeps track of its position in
191 which are initialized to 1 and 0.
193 .I argv\fB[sgoptind][sgoptpos]
194 and following characters.
198 that no more options are available by
201 which is initialized to
203 which is defined as \-1.
210 .B Ending conditions:
217 or the current cmdarg
226 If the current character
229 moves to the beginning of the next cmdarg.
230 It then checks the ending conditions again.
234 the current position is the begining of the cmdarg,
237 the current character
243 to the next character.
244 If that character is zero,
247 back to the beginning of the cmdarg,
250 If the character is a minus sign,
252 moves to the beginning of the next cmdarg,
258 records the current character,
260 and moves to the next character.
261 There are three possibilities:
264 is an option character without optarg in
269 is an option character with optarg in
280 appears as an option character without optarg in
289 appears as an option character with optarg in
294 to the current position,
295 and moves to the next cmdarg.
310 the current cmdarg is null,
339 is actually a macro abbreviation for
343 variables are also macros
346 These macros are defined in
355 subgetopt version 0.9, 931129.
357 Placed into the public domain by Daniel J. Bernstein.