2 * Copyright (c) 2010 Your File System Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR `AS IS'' AND ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 * Test the command line parsing library
29 #include <afsconfig.h>
30 #include <afs/param.h>
36 #include <tests/tap/basic.h>
50 testproc(struct cmd_syndesc *as, void *rock)
52 is_string("foo", as->parms[copt_first].items->data,
53 "first option matches");
54 is_string("bar", as->parms[copt_second].items->data,
55 "second option matches");
56 ok(as->parms[copt_flag].items != NULL,
63 checkList(struct cmd_item *list, ...)
69 el = va_arg(ap, char *);
70 while (el != NULL && list != NULL) {
71 is_string(el, list->data, "list element matches");
73 el = va_arg(ap, char *);
76 if (el == NULL && list == NULL) {
77 ok(1, "List has correct number of elements");
78 } else if (el == NULL) {
79 ok(0, "List has too many elements");
80 } else if (list == NULL) {
81 ok(0, "List has too few elements");
86 main(int argc, char **argv)
89 struct cmd_syndesc *opts;
90 struct cmd_syndesc *retopts;
91 struct cmd_item *list;
95 char *retstring = NULL;
99 initialize_CMD_error_table();
101 opts = cmd_CreateSyntax(NULL, testproc, NULL, NULL);
102 cmd_AddParm(opts, "-flag", CMD_FLAG, CMD_OPTIONAL, "a flag");
103 cmd_AddParm(opts, "-first", CMD_SINGLE, CMD_REQUIRED, "first option");
104 cmd_AddParm(opts, "-second", CMD_LIST, CMD_OPTIONAL, "second option");
106 /* A simple command line */
107 code = cmd_ParseLine("-first foo -second bar -flag", tv, &tc, 100);
108 is_int(0, code, "cmd_ParseLine succeeds");
109 code = cmd_Dispatch(tc, tv);
110 is_int(0, code, "dispatching simple comamnd line succeeds");
111 code = cmd_Parse(tc, tv, &retopts);
112 is_int(0, code, "parsing simple command line succeeds");
113 is_string("foo", retopts->parms[copt_first].items->data,
114 " ... 1st option matches");
115 is_string("bar", retopts->parms[copt_second].items->data,
116 " ... 2nd option matches");
117 ok(retopts->parms[copt_flag].items != NULL, " ... 3rd option matches");
118 cmd_FreeOptions(&retopts);
122 code = cmd_ParseLine("-first foo -second bar -third -flag", tv, &tc, 100);
123 is_int(0, code, "cmd_ParseLine succeeds");
124 code = cmd_Dispatch(tc, tv);
125 is_int(CMD_UNKNOWNSWITCH, code, "invalid options fail as expected");
126 code = cmd_Parse(tc, tv, &retopts);
127 is_int(CMD_UNKNOWNSWITCH, code, "and still fail with cmd_Parse");
130 /* missing parameter */
131 code = cmd_ParseLine("-first foo -second -flag", tv, &tc, 100);
132 is_int(0, code, "cmd_ParseLine succeeds");
133 code = cmd_Dispatch(tc, tv);
134 is_int(CMD_TOOFEW, code, "missing parameters fail as expected");
135 code = cmd_Parse(tc, tv, &retopts);
136 is_int(CMD_TOOFEW, code, "and still fail with cmd_Parse");
140 code = cmd_ParseLine("-second bar -third -flag", tv, &tc, 100);
141 is_int(0, code, "cmd_ParseLine succeeds");
142 code = cmd_Dispatch(tc, tv);
143 is_int(CMD_UNKNOWNSWITCH, code, "missing options fail as expected");
144 code = cmd_Parse(tc, tv, &retopts);
145 is_int(CMD_UNKNOWNSWITCH, code, "and still fail with cmd_Parse");
148 code = cmd_ParseLine("-first foo baz -second bar -third -flag", tv, &tc, 100);
149 is_int(0, code, "cmd_ParseLine succeeds");
150 code = cmd_Dispatch(tc, tv);
151 is_int(CMD_NOTLIST, code, "too many parameters fails as expected");
152 code = cmd_Parse(tc, tv, &retopts);
153 is_int(CMD_NOTLIST, code, "and still fail with cmd_Parse");
156 /* Positional parameters */
157 code = cmd_ParseLine("foo bar -flag", tv, &tc, 100);
158 is_int(0, code, "cmd_ParseLine succeeds");
159 code = cmd_Dispatch(tc, tv);
160 is_int(0, code, "dispatching positional parameters succeeds");
161 code = cmd_Parse(tc, tv, &retopts);
162 is_int(0, code, "and works with cmd_Parse");
163 cmd_FreeOptions(&retopts);
167 code = cmd_ParseLine("-fi foo -s bar -flag", tv, &tc, 100);
168 is_int(0, code, "cmd_ParseLine succeeds");
169 code = cmd_Dispatch(tc, tv);
170 is_int(0, code, "dispatching abbreviations succeeds");
171 code = cmd_Parse(tc, tv, &retopts);
172 is_int(0, code, "and works with cmd_Parse");
173 cmd_FreeOptions(&retopts);
178 code = cmd_ParseLine("-f foo -s bar -flag", tv, &tc, 100);
179 is_int(0, code, "cmd_ParseLine succeeds");
180 code = cmd_Dispatch(tc, tv);
181 is_int(CMD_UNKNOWNSWITCH, code, "ambiguous abbreviations correctly fail");
182 code = cmd_Parse(tc, tv, &retopts);
183 is_int(CMD_UNKNOWNSWITCH, code, "and fail with cmd_Parse too");
186 /* Check that paramaters with abbreviation disabled don't make things
188 cmd_AddParmAtOffset(opts, copt_sugar, "-sugar", CMD_SINGLE,
189 CMD_OPTIONAL | CMD_NOABBRV, "sugar with that");
190 code = cmd_ParseLine("-fi foo -s bar -flag", tv, &tc, 100);
191 is_int(0, code, "cmd_ParseLine succeeds");
192 code = cmd_Dispatch(tc, tv);
193 is_int(0, code, "disabling specific abbreviations succeeds");
194 code = cmd_Parse(tc, tv, &retopts);
195 is_int(0, code, "and works with cmd_Parse into the bargain");
196 cmd_FreeOptions(&retopts);
199 /* Disable positional commands */
200 cmd_DisablePositionalCommands();
201 code = cmd_ParseLine("foo bar -flag", tv, &tc, 100);
202 is_int(0, code, "cmd_ParseLine succeeds");
203 code = cmd_Dispatch(tc, tv);
204 is_int(CMD_NOTLIST, code, "positional parameters can be disabled");
205 code = cmd_Parse(tc, tv, &retopts);
206 is_int(CMD_NOTLIST, code, "and fail with cmd_Parse too");
209 /* Disable abbreviations */
210 cmd_DisableAbbreviations();
211 code = cmd_ParseLine("-fi foo -s bar -flag", tv, &tc, 100);
212 is_int(0, code, "cmd_ParseLine succeeds");
213 code = cmd_Dispatch(tc, tv);
214 is_int(CMD_UNKNOWNSWITCH, code, "dispatching abbreviations succeeds");
215 code = cmd_Parse(tc, tv, &retopts);
216 is_int(CMD_UNKNOWNSWITCH, code, "and fail with cmd_Parse too");
220 /* Try the new cmd_Parse function with something different*/
221 code = cmd_ParseLine("-first one -second two -flag", tv, &tc, 100);
222 is_int(0, code, "cmd_ParseLine succeeds");
223 code = cmd_Parse(tc, tv, &retopts);
224 is_int(0, code, "Parsing with cmd_Parse works");
225 is_string("one", retopts->parms[copt_first].items->data,
226 " ... 1st option matches");
227 is_string("two", retopts->parms[copt_second].items->data,
228 " ... 2nd option matches");
229 ok(retopts->parms[copt_flag].items != NULL, " ... 3rd option matches");
231 cmd_FreeOptions(&retopts);
233 /* Try adding a couple of parameters at specific positions */
234 cmd_AddParmAtOffset(opts, copt_fifth, "-fifth", CMD_SINGLE, CMD_OPTIONAL,
236 cmd_AddParmAtOffset(opts, copt_fourth, "-fourth", CMD_SINGLE, CMD_OPTIONAL,
238 code = cmd_ParseLine("-first a -fourth b -fifth c", tv, &tc, 100);
239 is_int(0, code, "cmd_ParseLine succeeds");
240 code = cmd_Parse(tc, tv, &retopts);
241 is_int(0, code, "parsing our new options succeeds");
242 is_string("b", retopts->parms[copt_fourth].items->data,
243 " Fourth option in right place");
244 is_string("c", retopts->parms[copt_fifth].items->data,
245 " Fifth option in right place");
246 cmd_FreeOptions(&retopts);
249 /* Check Accessors */
250 code = cmd_ParseLine("-first 1 -second second -flag", tv, &tc, 100);
251 is_int(0, code, "cmd_ParseLine succeeds");
252 code = cmd_Parse(tc, tv, &retopts);
254 code = cmd_OptionAsInt(retopts, copt_first, &retval);
255 is_int(0, code, "cmd_OptionsAsInt succeeds");
256 is_int(1, retval, " ... and returns correct value");
257 ok(cmd_OptionPresent(retopts, copt_first),
258 " ... and is marked as present");
260 code = cmd_OptionAsString(retopts, copt_second, &retstring);
261 is_int(0, code, "cmd_OptionsAsString succeeds");
262 is_string("second", retstring, " ... and returns correct value");
265 ok(cmd_OptionPresent(retopts, copt_second),
266 " ... and is marked as present");
268 code = cmd_OptionAsFlag(retopts, copt_flag, &retval);
269 is_int(0, code, "cmd_OptionsAsFlag succeeds");
270 ok(retval, " ... and flag is correct");
271 ok(cmd_OptionPresent(retopts, copt_flag),
272 " ... and is marked as present");
274 ok(!cmd_OptionPresent(retopts, copt_sugar),
275 "Missing option is marked as such");
277 cmd_FreeOptions(&retopts);
281 code = cmd_AddParmAlias(opts, copt_second, "-twa");
282 is_int(0, code, "cmd_AddParmAlias succeeds");
284 code = cmd_ParseLine("-first 1 -twa tup", tv, &tc, 100);
285 is_int(0, code, "cmd_ParseLine succeeds");
286 code = cmd_Parse(tc, tv, &retopts);
287 is_int(0, code, "cmd_Parse succeeds for alias");
288 cmd_OptionAsString(retopts, copt_second, &retstring);
289 is_string("tup", retstring, " ... and we have the correct value");
293 cmd_FreeOptions(&retopts);
296 /* Add something that can be a flag or a value, and put something after
297 * it so we can check for parse problems*/
298 cmd_AddParm(opts, "-perhaps", CMD_SINGLE_OR_FLAG, CMD_OPTIONAL,
300 cmd_AddParm(opts, "-sanity", CMD_SINGLE, CMD_OPTIONAL, "sanity check");
302 /* Try using as an option */
304 code = cmd_ParseLine("-first 1 -perhaps 2 -sanity 3", tv, &tc, 100);
305 is_int(0, code, "cmd_ParseLine succeeds");
306 code = cmd_Parse(tc, tv, &retopts);
307 is_int(0, code, "cmd_Parse succeeds for option-as-flag as opt");
308 code = cmd_OptionAsInt(retopts, copt_perhaps, &retval);
309 is_int(0, code, "cmd_OptionAsInt succeeds");
310 is_int(2, retval, " ... and we have the correct value");
311 cmd_FreeOptions(&retopts);
314 /* And now, as a flag */
316 code = cmd_ParseLine("-first 1 -perhaps -sanity 3", tv, &tc, 100);
317 is_int(0, code, "cmd_ParseLine succeeds");
318 code = cmd_Parse(tc, tv, &retopts);
319 is_int(0, code, "cmd_Parse succeeds for option-as-flag as flag");
320 code = cmd_OptionAsInt(retopts, copt_perhaps, &retval);
321 is_int(CMD_MISSING, code, " ... pulling out a value fails as expected");
322 cmd_OptionAsFlag(retopts, copt_perhaps, &retval);
323 ok(retval, " ... but parsing as a flag works");
324 cmd_FreeOptions(&retopts);
327 /* Check that we can produce help output */
328 code = cmd_ParseLine("-help", tv, &tc, 100);
329 is_int(0, code, "cmd_ParseLine succeeds");
330 code = cmd_Parse(tc, tv, &retopts);
331 is_int(CMD_USAGE, code, "cmd_Parse returns usage error with help output");
332 ok(retopts == NULL, " ... and options is empty");
334 /* Check splitting with '=' */
336 code = cmd_ParseLine("-first 1 -perhaps=6 -sanity=3", tv, &tc, 100);
337 is_int(0, code, "cmd_ParseLine succeeds");
338 code = cmd_Parse(tc, tv, &retopts);
339 is_int(0, code, "cmd_Parse succeeds for items split with '='");
340 code = cmd_OptionAsInt(retopts, copt_perhaps, &retval);
341 is_int(0, code, "cmd_OptionAsInt succeeds");
342 is_int(6, retval, " ... and we have the correct value once");
343 code = cmd_OptionAsInt(retopts, copt_sanity, &retval);
344 is_int(0, code, "cmd_OptionAsInt succeeds");
345 is_int(3, retval, " ... and we have the correct value twice");
346 cmd_FreeOptions(&retopts);
349 /* Check list behaviour */
350 code = cmd_ParseLine("-first 1 -second one two three", tv, &tc, 100);
351 is_int(0, code, "cmd_ParseLine succeeds");
352 code = cmd_Parse(tc, tv, &retopts);
353 is_int(0, code, "cmd_Parse succeeds for a list");
354 code = cmd_OptionAsList(retopts, copt_second, &list);
355 is_int(0, code, "cmd_OptionAsList succeeds");
356 checkList(list, "one", "two", "three", NULL);
357 cmd_FreeOptions(&retopts);