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, &cmdline);
55 assert(ret == EINVAL);
57 ret = cmdline_init(mem_ctx, "test1", NULL, NULL, &cmdline);
58 assert(ret == EINVAL);
60 ret = cmdline_init(mem_ctx, "test1", dummy_options, NULL, &cmdline);
61 assert(ret == EINVAL);
66 static struct cmdline_command test2_nofunc[] = {
67 { "nofunc", NULL, NULL, NULL },
71 static struct cmdline_command test2_nohelp[] = {
72 { "nohelp", dummy_func, NULL, NULL },
76 static struct cmdline_command test2_long[] = {
77 { "really really long command with lots of words",
78 dummy_func, "long command help",
79 "<and lots of really long long arguments>" },
83 static struct cmdline_command test2_longhelp[] = {
84 { "longhelp", dummy_func,
85 "this is a really really really long help message" \
86 "with lots of words and lots of description",
91 static struct cmdline_command test2_twowords[] = {
92 { "multiple words", dummy_func, "multiple words help", NULL },
96 static void test2(void)
99 struct cmdline_context *cmdline;
102 mem_ctx = talloc_new(NULL);
103 assert(mem_ctx != NULL);
105 ret = cmdline_init(mem_ctx, "test2", NULL, test2_nofunc, &cmdline);
106 assert(ret == EINVAL);
108 ret = cmdline_init(mem_ctx, "test2", NULL, test2_nohelp, &cmdline);
109 assert(ret == EINVAL);
111 ret = cmdline_init(mem_ctx, "test2", NULL, test2_long, &cmdline);
112 assert(ret == EINVAL);
114 ret = cmdline_init(mem_ctx, "test2", NULL, test2_longhelp, &cmdline);
115 assert(ret == EINVAL);
117 ret = cmdline_init(mem_ctx, "test2", NULL, test2_twowords, &cmdline);
120 talloc_free(mem_ctx);
127 static struct poptOption test3_noname[] = {
128 { NULL, 'o', POPT_ARG_STRING, &test3_data.str, 0,
129 "Noname option", NULL },
133 static struct poptOption test3_notype[] = {
134 { "debug", 'd', POPT_ARG_NONE, NULL, 0,
135 "No argument option", NULL },
139 static struct poptOption test3_noarg[] = {
140 { "debug", 'd', POPT_ARG_STRING, NULL, 0,
141 "No argument option", NULL },
145 static void test3(void)
148 struct cmdline_context *cmdline;
151 mem_ctx = talloc_new(NULL);
152 assert(mem_ctx != NULL);
154 ret = cmdline_init(mem_ctx,
159 assert(ret == EINVAL);
161 ret = cmdline_init(mem_ctx,
166 assert(ret == EINVAL);
168 ret = cmdline_init(mem_ctx,
173 assert(ret == EINVAL);
175 talloc_free(mem_ctx);
178 static int test4_count;
179 static int test4_value;
181 static struct poptOption test4_options[] = {
182 { "count", 'c', POPT_ARG_INT, &test4_count, 0,
183 "Option help of length thirty.", NULL },
184 { "value", 'v', POPT_ARG_INT, &test4_value, 0,
185 "Short description", "Value help of length 23" },
189 static struct cmdline_command test4_commands[] = {
190 { "A really really long command", dummy_func,
191 "This is a really long help message",
192 "<a long arguments message>" },
193 { "short command", dummy_func,
194 "short msg for short command", "<short arg msg>" },
198 static void test4(void)
201 struct cmdline_context *cmdline;
204 mem_ctx = talloc_new(NULL);
205 assert(mem_ctx != NULL);
207 ret = cmdline_init(mem_ctx,
214 cmdline_usage(cmdline, NULL);
215 cmdline_usage(cmdline, "short command");
217 talloc_free(mem_ctx);
220 static int action_func(TALLOC_CTX *mem_ctx,
229 printf("%s\n", argv[0]);
233 static struct cmdline_command action_commands[] = {
234 { "action one", dummy_func, "action one help", NULL },
235 { "action two", action_func, "action two help", NULL },
239 static void test5(void)
242 struct cmdline_context *cmdline;
243 const char *argv1[] = { "test5", "--help" };
244 const char *argv2[] = { "test5", "action" };
245 const char *argv3[] = { "test5", "action", "--help" };
246 const char *argv4[] = { "test5", "action", "one" };
249 mem_ctx = talloc_new(NULL);
250 assert(mem_ctx != NULL);
252 ret = cmdline_init(mem_ctx, "test5", NULL, action_commands, &cmdline);
255 ret = cmdline_parse(cmdline, 2, argv1, true);
258 ret = cmdline_run(cmdline, NULL, &result);
259 assert(ret == EAGAIN);
262 ret = cmdline_parse(cmdline, 2, argv2, true);
263 assert(ret == ENOENT);
265 ret = cmdline_parse(cmdline, 3, argv3, true);
268 ret = cmdline_parse(cmdline, 3, argv4, true);
271 talloc_free(mem_ctx);
274 static void test6(void)
277 struct cmdline_context *cmdline;
278 const char *argv1[] = { "action", "two" };
279 const char *argv2[] = { "action", "two", "arg1" };
280 const char *argv3[] = { "action", "two", "arg1", "arg2" };
283 mem_ctx = talloc_new(NULL);
284 assert(mem_ctx != NULL);
286 ret = cmdline_init(mem_ctx, "test6", NULL, action_commands, &cmdline);
289 ret = cmdline_parse(cmdline, 2, argv1, false);
292 ret = cmdline_run(cmdline, NULL, &result);
294 assert(result == 100);
296 ret = cmdline_parse(cmdline, 3, argv2, false);
299 ret = cmdline_run(cmdline, NULL, &result);
301 assert(result == 200);
303 ret = cmdline_parse(cmdline, 4, argv3, false);
306 ret = cmdline_run(cmdline, NULL, &result);
308 assert(result == 100);
310 talloc_free(mem_ctx);
313 int main(int argc, const char **argv)
318 fprintf(stderr, "Usage %s <testnum>\n", argv[0]);