2 Command line processing tests
4 Copyright (C) Amitay Isaacs 2018
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, see <http://www.gnu.org/licenses/>.
27 #include "common/cmdline.c"
29 static int dummy_func(TALLOC_CTX
*mem_ctx
,
37 static struct poptOption dummy_options
[] = {
41 static struct cmdline_command dummy_commands
[] = {
45 static void test1(void)
48 struct cmdline_context
*cmdline
;
51 mem_ctx
= talloc_new(NULL
);
52 assert(mem_ctx
!= NULL
);
54 ret
= cmdline_init(mem_ctx
, NULL
, NULL
, NULL
, NULL
, &cmdline
);
55 assert(ret
== EINVAL
);
57 ret
= cmdline_init(mem_ctx
, "test1", NULL
, NULL
, NULL
, &cmdline
);
58 assert(ret
== EINVAL
);
60 ret
= cmdline_init(mem_ctx
,
66 assert(ret
== EINVAL
);
71 static struct cmdline_command test2_nofunc
[] = {
72 { "nofunc", NULL
, NULL
, NULL
},
76 static struct cmdline_command test2_nohelp
[] = {
77 { "nohelp", dummy_func
, NULL
, NULL
},
81 static struct cmdline_command test2_long
[] = {
82 { "really really long command with lots of words",
83 dummy_func
, "long command help",
84 "<and lots of really long long arguments>" },
88 static struct cmdline_command test2_longhelp
[] = {
89 { "longhelp", dummy_func
,
90 "this is a really really really long help message" \
91 "with lots of words and lots of description",
96 static struct cmdline_command test2_twowords
[] = {
97 { "multiple words", dummy_func
, "multiple words help", NULL
},
101 static void test2(void)
104 struct cmdline_context
*cmdline
;
107 mem_ctx
= talloc_new(NULL
);
108 assert(mem_ctx
!= NULL
);
110 ret
= cmdline_init(mem_ctx
,
116 assert(ret
== EINVAL
);
118 ret
= cmdline_init(mem_ctx
,
124 assert(ret
== EINVAL
);
126 ret
= cmdline_init(mem_ctx
,
132 assert(ret
== EINVAL
);
134 ret
= cmdline_init(mem_ctx
,
140 assert(ret
== EINVAL
);
142 ret
= cmdline_init(mem_ctx
,
150 talloc_free(mem_ctx
);
157 static struct poptOption test3_noname
[] = {
158 { NULL
, 'o', POPT_ARG_STRING
, &test3_data
.str
, 0,
159 "Noname option", NULL
},
163 static struct poptOption test3_notype
[] = {
164 { "debug", 'd', POPT_ARG_NONE
, NULL
, 0,
165 "No argument option", NULL
},
169 static struct poptOption test3_noarg
[] = {
170 { "debug", 'd', POPT_ARG_STRING
, NULL
, 0,
171 "No argument option", NULL
},
175 static void test3(void)
178 struct cmdline_context
*cmdline
;
181 mem_ctx
= talloc_new(NULL
);
182 assert(mem_ctx
!= NULL
);
184 ret
= cmdline_init(mem_ctx
,
190 assert(ret
== EINVAL
);
192 ret
= cmdline_init(mem_ctx
,
198 assert(ret
== EINVAL
);
200 ret
= cmdline_init(mem_ctx
,
206 assert(ret
== EINVAL
);
208 talloc_free(mem_ctx
);
211 static int test4_count
;
212 static int test4_value
;
214 static struct poptOption test4_options
[] = {
215 { "count", 'c', POPT_ARG_INT
, &test4_count
, 0,
216 "Option help of length thirty.", NULL
},
217 { "value", 'v', POPT_ARG_INT
, &test4_value
, 0,
218 "Short description", "Value help of length 23" },
222 static struct cmdline_command test4_commands
[] = {
223 { "A really really long command", dummy_func
,
224 "This is a really long help message",
225 "<a long arguments message>" },
226 { "short command", dummy_func
,
227 "short msg for short command", "<short arg msg>" },
231 static void test4(void)
234 struct cmdline_context
*cmdline
;
237 mem_ctx
= talloc_new(NULL
);
238 assert(mem_ctx
!= NULL
);
240 ret
= cmdline_init(mem_ctx
,
248 cmdline_usage(cmdline
, NULL
);
249 cmdline_usage(cmdline
, "short command");
251 talloc_free(mem_ctx
);
254 static int action_func(TALLOC_CTX
*mem_ctx
,
263 printf("%s\n", argv
[0]);
267 static struct cmdline_command action_commands
[] = {
268 { "action one", dummy_func
, "action one help", NULL
},
269 { "action two", action_func
, "action two help", NULL
},
273 static void test5(void)
276 struct cmdline_context
*cmdline
;
277 const char *argv1
[] = { "test5", "--help" };
278 const char *argv2
[] = { "test5", "action" };
279 const char *argv3
[] = { "test5", "action", "--help" };
280 const char *argv4
[] = { "test5", "action", "one" };
283 mem_ctx
= talloc_new(NULL
);
284 assert(mem_ctx
!= NULL
);
286 ret
= cmdline_init(mem_ctx
,
294 ret
= cmdline_parse(cmdline
, 2, argv1
, true);
295 assert(ret
== EAGAIN
);
297 ret
= cmdline_parse(cmdline
, 2, argv2
, true);
298 assert(ret
== ENOENT
);
300 ret
= cmdline_parse(cmdline
, 3, argv3
, true);
301 assert(ret
== EAGAIN
);
303 ret
= cmdline_parse(cmdline
, 3, argv4
, true);
306 talloc_free(mem_ctx
);
309 static void test6(void)
312 struct cmdline_context
*cmdline
;
313 const char *argv1
[] = { "action", "two" };
314 const char *argv2
[] = { "action", "two", "arg1" };
315 const char *argv3
[] = { "action", "two", "arg1", "arg2" };
318 mem_ctx
= talloc_new(NULL
);
319 assert(mem_ctx
!= NULL
);
321 ret
= cmdline_init(mem_ctx
,
329 ret
= cmdline_parse(cmdline
, 2, argv1
, false);
332 ret
= cmdline_run(cmdline
, NULL
, &result
);
334 assert(result
== 100);
336 ret
= cmdline_parse(cmdline
, 3, argv2
, false);
339 ret
= cmdline_run(cmdline
, NULL
, &result
);
341 assert(result
== 200);
343 ret
= cmdline_parse(cmdline
, 4, argv3
, false);
346 ret
= cmdline_run(cmdline
, NULL
, &result
);
348 assert(result
== 100);
350 talloc_free(mem_ctx
);
353 static int test7_func(TALLOC_CTX
*mem_ctx
,
360 printf("%s\n", argv
[0]);
365 static struct cmdline_command test7_basic_commands
[] = {
366 { "cmd1", test7_func
, "command one help", NULL
},
367 { "cmd2", test7_func
, "command two help", NULL
},
371 static struct cmdline_command test7_advanced_commands
[] = {
372 { "cmd3", test7_func
, "command three help", NULL
},
373 { "cmd4", test7_func
, "command four help", NULL
},
377 static struct cmdline_command test7_ultimate_commands
[] = {
378 { "cmd5", test7_func
, "command five help", NULL
},
379 { "cmd6", test7_func
, "command six help", NULL
},
383 static void test7(void)
386 struct cmdline_context
*cmdline
;
387 const char *argv1
[] = { "cmd1", "one" };
388 const char *argv2
[] = { "cmd3", "three" };
389 const char *argv3
[] = { "cmd6", "six" };
392 mem_ctx
= talloc_new(NULL
);
393 assert(mem_ctx
!= NULL
);
395 ret
= cmdline_init(mem_ctx
,
399 test7_basic_commands
,
403 ret
= cmdline_add(cmdline
, "Advanced", test7_advanced_commands
);
406 ret
= cmdline_add(cmdline
, "Ultimate", test7_ultimate_commands
);
409 cmdline_usage(cmdline
, NULL
);
413 ret
= cmdline_parse(cmdline
, 2, argv1
, false);
416 ret
= cmdline_run(cmdline
, NULL
, &result
);
420 ret
= cmdline_parse(cmdline
, 2, argv2
, false);
423 ret
= cmdline_run(cmdline
, NULL
, &result
);
427 ret
= cmdline_parse(cmdline
, 2, argv3
, false);
430 ret
= cmdline_run(cmdline
, NULL
, &result
);
434 talloc_free(mem_ctx
);
438 int main(int argc
, const char **argv
)
443 fprintf(stderr
, "Usage %s <testnum>\n", argv
[0]);