annotate th_args.c @ 322:b9c15c57dc8f

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