0
|
1 /*
|
|
2 * Simple commandline argument processing
|
|
3 * Programmed and designed by Matti 'ccr' Hamalainen
|
|
4 * (C) Copyright 2002-2008 Tecnic Software productions (TNSP)
|
|
5 *
|
|
6 * Please read file 'COPYING' for information on license and distribution.
|
|
7 */
|
|
8 /*
|
|
9 Description
|
|
10 ===========
|
|
11 Structures and functions for simple commandline argument processing,
|
|
12 option argument handling (short and long forms supported).
|
|
13
|
|
14 Output function for printing out short on-line help for said options
|
|
15 and program commandline usage.
|
|
16
|
|
17
|
|
18 Format for typical commandline:
|
|
19
|
|
20 $ program -a -b -c --long-d -e argument-for-e --long-f="argument for f"
|
|
21
|
|
22 where -a, -b and -c are short options without required arguments;
|
|
23 --long-d is a long option without argument; -e is short option with
|
|
24 argument and finally --long-f is long option with argument.
|
|
25
|
|
26
|
|
27 Introduction
|
|
28 ============
|
|
29 Handling of commandline options in th_args_process() has various
|
|
30 options, which effect how an option is handled. Also the error
|
|
31 situations (unknown option/no required argument) can be handled
|
|
32 in different ways.
|
|
33
|
|
34 Typically th_args_process() is called as follows:
|
|
35
|
|
36 BOOL optionHandlerCallback(int optionNumber, char *optionArgument, char *optionName)
|
|
37 {
|
|
38 if (option is OK)
|
|
39 return TRUE;
|
|
40 else
|
|
41 return FALSE;
|
|
42 }
|
|
43
|
|
44 BOOL nonoptionHandlerCallback(char *argumentString)
|
|
45 {
|
|
46 // return value same as in optionHandlerCallback
|
|
47 }
|
|
48
|
|
49
|
|
50 int main(int argc, char *argv[])
|
|
51 {
|
|
52 if (th_args_process(argc, argv, optList, optListN,
|
|
53 optionHandlerCallback, nonoptionHandlerCallback)) {
|
|
54 ... arguments OK ...
|
|
55 } else {
|
|
56 ... arguments invalid or required arguments missing ...
|
|
57 }
|
|
58 }
|
|
59
|
|
60
|
|
61 NOTICE!
|
|
62 -------
|
|
63 The return value from handler callbacks affects the return value of
|
|
64 th_args_process(). Additionally, a failure in callback (returns FALSE)
|
|
65 effects the argument processing if bailOut argument of th_args_process()
|
|
66 is TRUE!
|
|
67
|
|
68 If bailOut is TRUE, any error/failure in argument processing (including
|
|
69 callbacks) immediately stops the argument processing and FALSE is
|
|
70 returned from th_args_process().
|
|
71
|
|
72 If bailOut is FALSE, most errors are "ignored", but FALSE is still returned
|
|
73 if any errors occured.
|
|
74
|
|
75
|
|
76 NOTICE #2!
|
|
77 ----------
|
|
78 A small temporary buffer of N*sizeof(BOOL) (where N is number of
|
|
79 options in optList[]) is allocated for processing required options.
|
|
80 If this allocation fails, the program is immediately exited with
|
|
81 code 128.
|
|
82
|
|
83
|
|
84 Examples
|
|
85 ========
|
|
86 Example of different options, in a fictional optionlist struct:
|
|
87
|
|
88 optarg_t optList[] = {
|
|
89 // Option without arguments
|
|
90 { 0, '?', "help", "Show this help", OPT_NONE },
|
|
91
|
|
92 // Option with a required argument
|
|
93 { 1, 'o', "output", "Output file name", OPT_ARGREQ },
|
|
94
|
|
95 // Option with optional argument
|
|
96 { 2, 'f', "foobar", "Use foobar, with optional string", OPT_ARGOPT },
|
|
97
|
|
98 // This option is required to be given, though without other flags
|
|
99 // it may not make much sense.
|
|
100 { 4, 'S', "stupid", "You must give this option", OPT_REQUIRED },
|
|
101
|
|
102 // The flags can be combined with OR operator: this option is both
|
|
103 // required to be specified, and also requires argument (the filename)
|
|
104 { 5, 'i', "input", "Input file name", OPT_REQUIRED | OPT_ARGREQ },
|
|
105
|
|
106
|
|
107 // Option with only long form
|
|
108 { 0, 0, "only-long", "Long option", OPT_NONE },
|
|
109
|
|
110 // Option with only short form
|
|
111 { 0, 's', NULL, "Short option", OPT_NONE },
|
|
112
|
|
113 };
|
|
114
|
20
|
115 const int optListN = (sizeof(optList) / sizeof(optarg_t));
|
0
|
116
|
|
117
|
|
118 */
|
|
119 #ifdef HAVE_CONFIG_H
|
2
|
120 #include "config.h"
|
0
|
121 #endif
|
|
122 #include "th_util.h"
|
|
123 #include "th_args.h"
|
|
124 #include "th_string.h"
|
|
125
|
|
126
|
|
127 /* Check if option requires an argument
|
|
128 */
|
|
129 static BOOL th_args_check_arg(optarg_t *o, char *optArg)
|
|
130 {
|
377
|
131 if ((o->optFlags & OPT_ARGMASK) == OPT_ARGREQ && optArg == NULL)
|
|
132 {
|
|
133 if (o->optShort != 0 && o->optLong != NULL)
|
|
134 {
|
81
|
135 THERR("Option '-%c ARG' (--%s=ARG) requires an argument!\n",
|
377
|
136 o->optShort, o->optLong);
|
|
137 }
|
|
138 else if (o->optShort != 0)
|
|
139 {
|
81
|
140 THERR("Option '-%c ARG' requires an argument!\n", o->optShort);
|
377
|
141 }
|
|
142 else if (o->optLong != NULL)
|
|
143 {
|
81
|
144 THERR("Option --%s=ARG requires an argument!\n", o->optLong);
|
|
145 }
|
377
|
146
|
81
|
147 return FALSE;
|
377
|
148 }
|
|
149 else
|
81
|
150 return TRUE;
|
0
|
151 }
|
|
152
|
|
153
|
|
154 /* Handle short options
|
|
155 */
|
|
156 static BOOL th_args_process_short(char *currArg, int *newArgIndex,
|
81
|
157 BOOL *wasGiven, int argc, char *argv[],
|
|
158 optarg_t optList[], int optListN,
|
|
159 BOOL (*handleOpt)(int, char *, char *))
|
0
|
160 {
|
81
|
161 char *tmpArg = currArg, *optArg;
|
|
162 int optN;
|
|
163 BOOL isFound;
|
377
|
164
|
81
|
165 /* Short options can be combined: -a -b -c == -abc */
|
377
|
166 while (*tmpArg)
|
|
167 {
|
81
|
168 for (optN = 0, isFound = FALSE; (optN < optListN) && !isFound; optN++)
|
377
|
169 if (*tmpArg == optList[optN].optShort)
|
|
170 {
|
|
171 /* Get possible option argument, if needed */
|
|
172 if ((optList[optN].optFlags & OPT_ARGMASK) != 0
|
|
173 && (++(*newArgIndex) < argc))
|
|
174 optArg = argv[*newArgIndex];
|
|
175 else
|
|
176 optArg = NULL;
|
|
177
|
|
178 /* Check if option argument is required */
|
|
179 if (!th_args_check_arg(&optList[optN], optArg))
|
|
180 return FALSE;
|
|
181 else
|
|
182 {
|
|
183 char tmpStr[2] = { 0, 0 };
|
0
|
184
|
377
|
185 /* Option was given succesfully, try to handle it */
|
|
186 wasGiven[optN] = TRUE;
|
|
187
|
|
188 tmpStr[0] = *tmpArg;
|
|
189
|
|
190 if (!handleOpt(optList[optN].optID, optArg, tmpStr))
|
|
191 return FALSE;
|
|
192 }
|
|
193
|
|
194 isFound = TRUE;
|
81
|
195 }
|
377
|
196
|
|
197 if (!isFound)
|
|
198 {
|
81
|
199 THERR("Unknown short option '%c' in argument '-%s'\n",
|
377
|
200 *tmpArg, currArg);
|
81
|
201 return FALSE;
|
|
202 }
|
0
|
203
|
81
|
204 tmpArg++;
|
|
205 }
|
0
|
206
|
81
|
207 return TRUE;
|
0
|
208 }
|
|
209
|
|
210
|
|
211 /* Handle long options
|
|
212 */
|
|
213 static BOOL th_args_process_long(char *currArg, int *newArgIndex,
|
81
|
214 BOOL *wasGiven, int argc, char *argv[],
|
|
215 optarg_t optList[], int optListN,
|
|
216 BOOL (*handleOpt)(int, char *, char *))
|
0
|
217 {
|
81
|
218 int optN, optLen, i;
|
|
219 char *optArg;
|
377
|
220
|
|
221 (void) argc;
|
|
222 (void) argv;
|
|
223 (void) newArgIndex;
|
|
224
|
81
|
225 /* Long option */
|
|
226 for (optN = -1, optLen = i = 0; (i < optListN) && (optN < 0); i++)
|
377
|
227 if (optList[i].optLong)
|
|
228 {
|
|
229 optLen = strlen(optList[i].optLong);
|
|
230 if (strncmp(currArg, optList[i].optLong, optLen) == 0)
|
|
231 optN = i;
|
|
232 }
|
|
233
|
81
|
234 /* Get possible option argument, if needed */
|
377
|
235 if (optN >= 0)
|
|
236 {
|
|
237 if ((optList[optN].optFlags & OPT_ARGMASK) != 0)
|
|
238 {
|
81
|
239 if (currArg[optLen] == '=')
|
|
240 optArg = &currArg[optLen + 1];
|
|
241 else
|
|
242 optArg = NULL;
|
377
|
243 }
|
|
244 else
|
81
|
245 optArg = NULL;
|
0
|
246
|
81
|
247 /* Check if option argument is required */
|
|
248 if (!th_args_check_arg(&optList[optN], optArg))
|
377
|
249 return FALSE;
|
|
250 else
|
|
251 {
|
81
|
252 /* Option was given succesfully, try to handle it */
|
|
253 wasGiven[optN] = TRUE;
|
|
254 if (!handleOpt(optList[optN].optID, optArg, currArg))
|
|
255 return FALSE;
|
|
256 }
|
377
|
257 }
|
|
258 else
|
|
259 {
|
81
|
260 THERR("Unknown long option '--%s'\n", currArg);
|
|
261 return FALSE;
|
|
262 }
|
0
|
263
|
81
|
264 return TRUE;
|
0
|
265 }
|
|
266
|
|
267
|
|
268 /* Process arguments, handling short and long options by
|
|
269 * calling the given callback functions.
|
|
270 */
|
|
271 BOOL th_args_process(int argc, char *argv[],
|
377
|
272 optarg_t optList[], int optListN,
|
|
273 BOOL(*handleOpt) (int, char *, char *),
|
|
274 BOOL(*handleNonOption) (char *), BOOL bailOut)
|
0
|
275 {
|
81
|
276 BOOL endOptions, optionsOK;
|
|
277 int argIndex, newArgIndex, i;
|
|
278 char *currArg;
|
|
279 BOOL *wasGiven;
|
0
|
280
|
81
|
281 /* Allocate wasGiven */
|
|
282 wasGiven = (BOOL *) th_calloc(optListN, sizeof(BOOL));
|
377
|
283 if (!wasGiven)
|
|
284 {
|
|
285 THERR
|
|
286 ("FATAL ERROR! Could not allocate wasGiven in th_args_process()!\n");
|
81
|
287 exit(128);
|
|
288 }
|
0
|
289
|
81
|
290 /* Parse arguments */
|
|
291 argIndex = 1;
|
|
292 optionsOK = TRUE;
|
|
293 endOptions = FALSE;
|
377
|
294 while (argIndex < argc)
|
|
295 {
|
81
|
296 currArg = argv[argIndex];
|
377
|
297 if ((currArg[0] == '-') && !endOptions)
|
|
298 {
|
81
|
299 newArgIndex = argIndex;
|
|
300 currArg++;
|
377
|
301 if (*currArg == '-')
|
|
302 {
|
81
|
303 /* Check for "--", which ends the options-list */
|
|
304 currArg++;
|
377
|
305 if (*currArg == 0)
|
|
306 {
|
81
|
307 endOptions = TRUE;
|
|
308 continue;
|
|
309 }
|
377
|
310
|
81
|
311 /* Long options */
|
|
312 if (!th_args_process_long(currArg, &newArgIndex,
|
377
|
313 wasGiven, argc, argv, optList,
|
|
314 optListN, handleOpt))
|
81
|
315 optionsOK = FALSE;
|
377
|
316 }
|
|
317 else
|
|
318 {
|
81
|
319 /* Short options */
|
|
320 if (!th_args_process_short(currArg, &newArgIndex,
|
377
|
321 wasGiven, argc, argv, optList,
|
|
322 optListN, handleOpt))
|
81
|
323 optionsOK = FALSE;
|
|
324 }
|
0
|
325
|
81
|
326 argIndex = newArgIndex;
|
377
|
327 }
|
|
328 else
|
|
329 {
|
81
|
330 /* Was not option argument */
|
377
|
331 if (handleNonOption == NULL
|
|
332 || (handleNonOption != NULL && !handleNonOption(currArg)))
|
|
333 {
|
81
|
334 THERR("Invalid argument '%s'\n", currArg);
|
|
335 optionsOK = FALSE;
|
|
336 }
|
|
337 }
|
377
|
338
|
81
|
339 /* Check if we bail out on invalid argument */
|
377
|
340 if (!optionsOK && bailOut)
|
|
341 {
|
81
|
342 th_free(wasGiven);
|
|
343 return FALSE;
|
|
344 }
|
377
|
345
|
81
|
346 argIndex++;
|
|
347 }
|
0
|
348
|
81
|
349 /* Check wasGiven by isRequired */
|
|
350 for (i = 0; i < optListN; i++)
|
377
|
351 if ((optList[i].optFlags & OPT_REQUIRED) != 0 && !wasGiven[i])
|
|
352 {
|
|
353 THERR("Option -%s (--%s) is required.\n",
|
|
354 optList[i].optShort, optList[i].optLong);
|
0
|
355
|
377
|
356 optionsOK = FALSE;
|
|
357 if (bailOut)
|
|
358 break;
|
|
359 }
|
|
360
|
81
|
361 th_free(wasGiven);
|
|
362 return optionsOK;
|
0
|
363 }
|
|
364
|
|
365
|
|
366 /* Print help for commandline arguments/options
|
|
367 */
|
|
368 void th_args_help(FILE * outFile,
|
377
|
369 optarg_t optList[], int optListN,
|
|
370 char *progName, char *progUsage)
|
0
|
371 {
|
81
|
372 int i, nrequired;
|
0
|
373
|
81
|
374 fprintf(outFile,
|
377
|
375 "\n%s v%s (%s)\n"
|
|
376 "%s\n"
|
|
377 "%s\n"
|
|
378 "Usage: %s %s\n",
|
|
379 th_prog_name, th_prog_version, th_prog_fullname,
|
|
380 th_prog_author, th_prog_license, progName, progUsage);
|
0
|
381
|
|
382
|
377
|
383 for (i = nrequired = 0; i < optListN; i++)
|
|
384 {
|
81
|
385 optarg_t *o = &optList[i];
|
377
|
386
|
81
|
387 /* Print short option */
|
|
388 if (o->optShort != 0)
|
|
389 fprintf(outFile, " -%c, ", o->optShort);
|
|
390 else
|
|
391 fprintf(outFile, " ");
|
377
|
392
|
81
|
393 /* Print long option */
|
377
|
394 if (o->optLong)
|
|
395 {
|
81
|
396 char tmpStr[64], *p;
|
377
|
397
|
|
398 if ((o->optFlags & OPT_ARGMASK) == OPT_ARGOPT)
|
|
399 {
|
|
400 snprintf(tmpStr, sizeof(tmpStr), "%s[=ARG]",
|
|
401 optList[i].optLong);
|
81
|
402 p = tmpStr;
|
377
|
403 }
|
|
404 else if ((o->optFlags & OPT_ARGMASK) == OPT_ARGREQ)
|
|
405 {
|
|
406 snprintf(tmpStr, sizeof(tmpStr), "%s=ARG",
|
|
407 optList[i].optLong);
|
81
|
408 p = tmpStr;
|
377
|
409 }
|
|
410 else
|
81
|
411 p = o->optLong;
|
377
|
412
|
81
|
413 fprintf(outFile, "--%-15s", p);
|
377
|
414 }
|
|
415 else
|
81
|
416 fprintf(outFile, " ");
|
0
|
417
|
81
|
418 fprintf(outFile, " %s.", optList[i].optDesc);
|
377
|
419
|
|
420 if (o->optFlags & OPT_REQUIRED)
|
|
421 {
|
81
|
422 fprintf(outFile, " [*]\n");
|
|
423 nrequired++;
|
377
|
424 }
|
|
425 else
|
81
|
426 fprintf(outFile, "\n");
|
|
427 }
|
377
|
428
|
81
|
429 if (nrequired > 0)
|
|
430 fprintf(outFile, "(Options marked with [*] are required)\n");
|
0
|
431 }
|