Mercurial > hg > th-libs
comparison th_args.c @ 457:85fa3d333556
Actually, revert the boolean changes .. meh.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Tue, 02 Jan 2018 23:09:29 +0200 |
parents | 347bfd3e017e |
children | e4ce60239d16 |
comparison
equal
deleted
inserted
replaced
456:1bf886fa9db5 | 457:85fa3d333556 |
---|---|
21 * @param argc number of arguments | 21 * @param argc number of arguments |
22 * @param argv argument string array | 22 * @param argv argument string array |
23 * @param opts options list array | 23 * @param opts options list array |
24 * @param nopts number of elements in options list array | 24 * @param nopts number of elements in options list array |
25 * @param handle_option function pointer to callback that handles option arguments | 25 * @param handle_option function pointer to callback that handles option arguments |
26 * @param doProcess if true, actually handle the argument, aka call the handle_option() function. if false, only validity of options are checked. | 26 * @param doProcess if TRUE, actually handle the argument, aka call the handle_option() function. if FALSE, only validity of options are checked. |
27 * @param isLong true if the option is a --long-format one | 27 * @param isLong TRUE if the option is a --long-format one |
28 */ | 28 */ |
29 static bool th_args_process_opt( | 29 static BOOL th_args_process_opt( |
30 char *currArg, int *argIndex, | 30 char *currArg, int *argIndex, |
31 int argc, char *argv[], | 31 int argc, char *argv[], |
32 const th_optarg opts[], int nopts, | 32 const th_optarg opts[], int nopts, |
33 bool (*handle_option)(int id, char *, char *), | 33 BOOL (*handle_option)(int id, char *, char *), |
34 bool doProcess, bool isLong) | 34 BOOL doProcess, BOOL isLong) |
35 { | 35 { |
36 const th_optarg *opt = NULL; | 36 const th_optarg *opt = NULL; |
37 char *optArg = NULL; | 37 char *optArg = NULL; |
38 int optIndex; | 38 int optIndex; |
39 | 39 |
83 if (optArg == NULL) | 83 if (optArg == NULL) |
84 { | 84 { |
85 THERR("Option '%s%s' requires an argument.\n", | 85 THERR("Option '%s%s' requires an argument.\n", |
86 isLong ? "--" : "-", | 86 isLong ? "--" : "-", |
87 currArg); | 87 currArg); |
88 return false; | 88 return FALSE; |
89 } | 89 } |
90 } | 90 } |
91 | 91 |
92 // Option was given succesfully, try to process it | 92 // Option was given succesfully, try to process it |
93 if (doProcess && !handle_option(opt->id, optArg, currArg)) | 93 if (doProcess && !handle_option(opt->id, optArg, currArg)) |
94 return false; | 94 return FALSE; |
95 } | 95 } |
96 else | 96 else |
97 { | 97 { |
98 THERR("Unknown %s option '%s%s'\n", | 98 THERR("Unknown %s option '%s%s'\n", |
99 isLong ? "long" : "short", | 99 isLong ? "long" : "short", |
100 isLong ? "--" : "-", | 100 isLong ? "--" : "-", |
101 currArg); | 101 currArg); |
102 | 102 |
103 return false; | 103 return FALSE; |
104 } | 104 } |
105 | 105 |
106 return true; | 106 return TRUE; |
107 } | 107 } |
108 | 108 |
109 | 109 |
110 /** | 110 /** |
111 * Process given array of commandline arguments, handling short | 111 * Process given array of commandline arguments, handling short |
116 * @param opts supported option list array | 116 * @param opts supported option list array |
117 * @param nopts number of elements in the option list array | 117 * @param nopts number of elements in the option list array |
118 * @param handle_option callback function | 118 * @param handle_option callback function |
119 * @param handle_other callback function | 119 * @param handle_other callback function |
120 * @param flags processing flags | 120 * @param flags processing flags |
121 * @return return true if all is well | 121 * @return return TRUE if all is well |
122 */ | 122 */ |
123 bool th_args_process(int argc, char *argv[], | 123 BOOL th_args_process(int argc, char *argv[], |
124 const th_optarg *opts, const int nopts, | 124 const th_optarg *opts, const int nopts, |
125 bool(*handle_option)(int id, char *, char *), | 125 BOOL(*handle_option)(int id, char *, char *), |
126 bool(*handle_other)(char *), const int flags) | 126 BOOL(*handle_other)(char *), const int flags) |
127 { | 127 { |
128 int argIndex, handleFlags = flags & OPTH_ONLY_MASK; | 128 int argIndex, handleFlags = flags & OPTH_ONLY_MASK; |
129 bool optionsOK = true, endOfOptions = false; | 129 BOOL optionsOK = TRUE, endOfOptions = FALSE; |
130 | 130 |
131 for (argIndex = 1; argIndex < argc; argIndex++) | 131 for (argIndex = 1; argIndex < argc; argIndex++) |
132 { | 132 { |
133 char *str = argv[argIndex]; | 133 char *str = argv[argIndex]; |
134 if (*str == '-' && !endOfOptions) | 134 if (*str == '-' && !endOfOptions) |
135 { | 135 { |
136 // Should we process options? | 136 // Should we process options? |
137 bool doProcess = (handleFlags & OPTH_ONLY_OPTS) || handleFlags == 0; | 137 BOOL doProcess = (handleFlags & OPTH_ONLY_OPTS) || handleFlags == 0; |
138 bool isLong; | 138 BOOL isLong; |
139 | 139 |
140 str++; | 140 str++; |
141 if (*str == '-') | 141 if (*str == '-') |
142 { | 142 { |
143 // Check for "--", which ends the options-list | 143 // Check for "--", which ends the options-list |
144 str++; | 144 str++; |
145 if (*str == 0) | 145 if (*str == 0) |
146 { | 146 { |
147 endOfOptions = true; | 147 endOfOptions = TRUE; |
148 continue; | 148 continue; |
149 } | 149 } |
150 | 150 |
151 // We have a long option | 151 // We have a long option |
152 isLong = true; | 152 isLong = TRUE; |
153 } | 153 } |
154 else | 154 else |
155 isLong = false; | 155 isLong = FALSE; |
156 | 156 |
157 if (!th_args_process_opt(str, &argIndex, argc, argv, | 157 if (!th_args_process_opt(str, &argIndex, argc, argv, |
158 opts, nopts, handle_option, doProcess, isLong)) | 158 opts, nopts, handle_option, doProcess, isLong)) |
159 optionsOK = false; | 159 optionsOK = FALSE; |
160 } | 160 } |
161 else | 161 else |
162 if (handleFlags == OPTH_ONLY_OTHER || handleFlags == 0) | 162 if (handleFlags == OPTH_ONLY_OTHER || handleFlags == 0) |
163 { | 163 { |
164 // Was not option argument | 164 // Was not option argument |
165 if (handle_other == NULL || | 165 if (handle_other == NULL || |
166 (handle_other != NULL && !handle_other(str))) | 166 (handle_other != NULL && !handle_other(str))) |
167 { | 167 { |
168 THERR("Invalid argument '%s'\n", str); | 168 THERR("Invalid argument '%s'\n", str); |
169 optionsOK = false; | 169 optionsOK = FALSE; |
170 } | 170 } |
171 } | 171 } |
172 | 172 |
173 // Check if we bail out on invalid argument | 173 // Check if we bail out on invalid argument |
174 if (!optionsOK && (flags & OPTH_BAILOUT)) | 174 if (!optionsOK && (flags & OPTH_BAILOUT)) |
175 return false; | 175 return FALSE; |
176 } | 176 } |
177 | 177 |
178 return optionsOK; | 178 return optionsOK; |
179 } | 179 } |
180 | 180 |