2 * Copyright (c) 2019-2021 Philippe Proulx <pproulx@efficios.com>
3 * Copyright (c) 2020-2021 Simon Marchi <simon.marchi@efficios.com>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; under version 2 of the License.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 #include "argpar/argpar.h"
30 * Formats `item` and appends the resulting string to `res_str` to
31 * incrementally build an expected command line string.
35 * ‣ Prefers the `--long-opt=arg` style over the `-s arg` style.
37 * ‣ Uses the `arg<A,B>` form for non-option arguments, where `A` is the
38 * original argument index and `B` is the non-option argument index.
40 static void append_to_res_str(GString
* const res_str
, const argpar_item_t
* const item
)
42 if (res_str
->len
> 0) {
43 g_string_append_c(res_str
, ' ');
46 switch (argpar_item_type(item
)) {
47 case ARGPAR_ITEM_TYPE_OPT
:
49 const argpar_opt_descr_t
* const descr
= argpar_item_opt_descr(item
);
50 const char * const arg
= argpar_item_opt_arg(item
);
52 if (descr
->long_name
) {
53 g_string_append_printf(res_str
, "--%s", descr
->long_name
);
56 g_string_append_printf(res_str
, "=%s", arg
);
58 } else if (descr
->short_name
) {
59 g_string_append_printf(res_str
, "-%c", descr
->short_name
);
62 g_string_append_printf(res_str
, " %s", arg
);
68 case ARGPAR_ITEM_TYPE_NON_OPT
:
70 const char * const arg
= argpar_item_non_opt_arg(item
);
71 const unsigned int orig_index
= argpar_item_non_opt_orig_index(item
);
72 const unsigned int non_opt_index
= argpar_item_non_opt_non_opt_index(item
);
74 g_string_append_printf(res_str
, "%s<%u,%u>", arg
, orig_index
, non_opt_index
);
83 * Parses `cmdline` with the argpar API using the option descriptors
84 * `descrs`, and ensures that the resulting effective command line is
85 * `expected_cmd_line` and that the number of ingested original
86 * arguments is `expected_ingested_orig_args`.
88 * This function splits `cmdline` on spaces to create an original
91 * This function builds the resulting command line from parsing items
92 * by space-separating each formatted item (see append_to_res_str()).
94 static void test_succeed(const char * const cmdline
, const char * const expected_cmd_line
,
95 const argpar_opt_descr_t
* const descrs
,
96 const unsigned int expected_ingested_orig_args
)
98 argpar_iter_t
*iter
= NULL
;
99 const argpar_item_t
*item
= NULL
;
100 const argpar_error_t
*error
= NULL
;
101 GString
* const res_str
= g_string_new(NULL
);
102 gchar
** const argv
= g_strsplit(cmdline
, " ", 0);
103 unsigned int i
, actual_ingested_orig_args
;
107 iter
= argpar_iter_create(g_strv_length(argv
), (const char * const *) argv
, descrs
);
111 argpar_iter_next_status_t status
;
113 ARGPAR_ITEM_DESTROY_AND_RESET(item
);
114 status
= argpar_iter_next(iter
, &item
, &error
);
116 ok(status
== ARGPAR_ITER_NEXT_STATUS_OK
|| status
== ARGPAR_ITER_NEXT_STATUS_END
,
117 "argpar_iter_next() returns the expected status (%d) for command line `%s` (call %u)",
118 status
, cmdline
, i
+ 1);
119 ok(!error
, "argpar_iter_next() doesn't set an error for command line `%s` (call %u)",
122 if (status
== ARGPAR_ITER_NEXT_STATUS_END
) {
124 "argpar_iter_next() doesn't set an item for status `ARGPAR_ITER_NEXT_STATUS_END` "
125 "and command line `%s` (call %u)",
130 append_to_res_str(res_str
, item
);
133 actual_ingested_orig_args
= argpar_iter_ingested_orig_args(iter
);
134 ok(actual_ingested_orig_args
== expected_ingested_orig_args
,
135 "argpar_iter_ingested_orig_args() returns the expected number of ingested original "
136 "arguments for command line `%s`",
139 if (actual_ingested_orig_args
!= expected_ingested_orig_args
) {
140 diag("Expected: %u Got: %u", expected_ingested_orig_args
, actual_ingested_orig_args
);
143 ok(strcmp(expected_cmd_line
, res_str
->str
) == 0,
144 "argpar_iter_next() returns the expected parsing items for command line `%s`", cmdline
);
146 if (strcmp(expected_cmd_line
, res_str
->str
) != 0) {
147 diag("Expected: `%s`", expected_cmd_line
);
148 diag("Got: `%s`", res_str
->str
);
151 argpar_item_destroy(item
);
152 argpar_iter_destroy(iter
);
154 g_string_free(res_str
, TRUE
);
158 static void succeed_tests(void)
162 const argpar_opt_descr_t descrs
[] = {ARGPAR_OPT_DESCR_SENTINEL
};
164 test_succeed("", "", descrs
, 0);
167 /* Single long option */
169 const argpar_opt_descr_t descrs
[] = {{0, '\0', "salut", false}, ARGPAR_OPT_DESCR_SENTINEL
};
171 test_succeed("--salut", "--salut", descrs
, 1);
174 /* Single short option */
176 const argpar_opt_descr_t descrs
[] = {{0, 'f', NULL
, false}, ARGPAR_OPT_DESCR_SENTINEL
};
178 test_succeed("-f", "-f", descrs
, 1);
181 /* Short and long option (aliases) */
183 const argpar_opt_descr_t descrs
[] = {{0, 'f', "flaw", false}, ARGPAR_OPT_DESCR_SENTINEL
};
185 test_succeed("-f --flaw", "--flaw --flaw", descrs
, 2);
188 /* Long option with argument (space form) */
190 const argpar_opt_descr_t descrs
[] = {{0, '\0', "tooth", true}, ARGPAR_OPT_DESCR_SENTINEL
};
192 test_succeed("--tooth 67", "--tooth=67", descrs
, 2);
195 /* Long option with argument (equal form) */
197 const argpar_opt_descr_t descrs
[] = {{0, '\0', "polish", true}, ARGPAR_OPT_DESCR_SENTINEL
};
199 test_succeed("--polish=brick", "--polish=brick", descrs
, 1);
202 /* Short option with argument (space form) */
204 const argpar_opt_descr_t descrs
[] = {{0, 'c', NULL
, true}, ARGPAR_OPT_DESCR_SENTINEL
};
206 test_succeed("-c chilly", "-c chilly", descrs
, 2);
209 /* Short option with argument (glued form) */
211 const argpar_opt_descr_t descrs
[] = {{0, 'c', NULL
, true}, ARGPAR_OPT_DESCR_SENTINEL
};
213 test_succeed("-cchilly", "-c chilly", descrs
, 1);
216 /* Short and long option (aliases) with argument (all forms) */
218 const argpar_opt_descr_t descrs
[] = {{0, 'd', "dry", true}, ARGPAR_OPT_DESCR_SENTINEL
};
220 test_succeed("--dry=rate -dthing --dry street --dry=shape",
221 "--dry=rate --dry=thing --dry=street --dry=shape", descrs
, 5);
224 /* Many short options, last one with argument (glued form) */
226 const argpar_opt_descr_t descrs
[] = {{0, 'd', NULL
, false},
227 {0, 'e', NULL
, false},
228 {0, 'f', NULL
, true},
229 ARGPAR_OPT_DESCR_SENTINEL
};
231 test_succeed("-defmeow", "-d -e -f meow", descrs
, 1);
236 const argpar_opt_descr_t descrs
[] = {{0, 'd', NULL
, false},
237 {0, 'e', "east", true},
238 {0, '\0', "mind", false},
239 ARGPAR_OPT_DESCR_SENTINEL
};
241 test_succeed("-d --mind -destart --mind --east cough -d --east=itch",
242 "-d --mind -d --east=start --mind --east=cough -d --east=itch", descrs
, 8);
245 /* Single non-option argument */
247 const argpar_opt_descr_t descrs
[] = {ARGPAR_OPT_DESCR_SENTINEL
};
249 test_succeed("kilojoule", "kilojoule<0,0>", descrs
, 1);
252 /* Two non-option arguments */
254 const argpar_opt_descr_t descrs
[] = {ARGPAR_OPT_DESCR_SENTINEL
};
256 test_succeed("kilojoule mitaine", "kilojoule<0,0> mitaine<1,1>", descrs
, 2);
259 /* Single non-option argument mixed with options */
261 const argpar_opt_descr_t descrs
[] = {{0, 'd', NULL
, false},
262 {0, '\0', "squeeze", true},
263 ARGPAR_OPT_DESCR_SENTINEL
};
265 test_succeed("-d sprout yes --squeeze little bag -d",
266 "-d sprout<1,0> yes<2,1> --squeeze=little bag<5,2> -d", descrs
, 7);
271 const argpar_opt_descr_t descrs
[] = {{0, '\0', "-fuel", true}, ARGPAR_OPT_DESCR_SENTINEL
};
273 test_succeed("---fuel=three", "---fuel=three", descrs
, 1);
276 /* Long option containing `=` in argument (equal form) */
278 const argpar_opt_descr_t descrs
[] = {{0, '\0', "zebra", true}, ARGPAR_OPT_DESCR_SENTINEL
};
280 test_succeed("--zebra=three=yes", "--zebra=three=yes", descrs
, 1);
283 /* Short option's argument starting with `-` (glued form) */
285 const argpar_opt_descr_t descrs
[] = {{0, 'z', NULL
, true}, ARGPAR_OPT_DESCR_SENTINEL
};
287 test_succeed("-z-will", "-z -will", descrs
, 1);
290 /* Short option's argument starting with `-` (space form) */
292 const argpar_opt_descr_t descrs
[] = {{0, 'z', NULL
, true}, ARGPAR_OPT_DESCR_SENTINEL
};
294 test_succeed("-z -will", "-z -will", descrs
, 2);
297 /* Long option's argument starting with `-` (space form) */
299 const argpar_opt_descr_t descrs
[] = {{0, '\0', "janine", true}, ARGPAR_OPT_DESCR_SENTINEL
};
301 test_succeed("--janine -sutto", "--janine=-sutto", descrs
, 2);
304 /* Long option's argument starting with `-` (equal form) */
306 const argpar_opt_descr_t descrs
[] = {{0, '\0', "janine", true}, ARGPAR_OPT_DESCR_SENTINEL
};
308 test_succeed("--janine=-sutto", "--janine=-sutto", descrs
, 1);
311 /* Long option's empty argument (equal form) */
313 const argpar_opt_descr_t descrs
[] = {{0, 'f', NULL
, false},
314 {0, '\0', "yeah", true},
315 ARGPAR_OPT_DESCR_SENTINEL
};
317 test_succeed("-f --yeah= -f", "-f --yeah= -f", descrs
, 3);
320 /* `-` non-option argument */
322 const argpar_opt_descr_t descrs
[] = {{0, 'f', NULL
, false}, ARGPAR_OPT_DESCR_SENTINEL
};
324 test_succeed("-f - -f", "-f -<1,0> -f", descrs
, 3);
327 /* `--` non-option argument */
329 const argpar_opt_descr_t descrs
[] = {{0, 'f', NULL
, false}, ARGPAR_OPT_DESCR_SENTINEL
};
331 test_succeed("-f -- -f", "-f --<1,0> -f", descrs
, 3);
334 /* Very long name of long option */
336 const char opt_name
[] = "kale-chips-waistcoat-yr-bicycle-rights-gochujang-"
337 "woke-tumeric-flexitarian-biodiesel-chillwave-cliche-"
338 "ethical-cardigan-listicle-pok-pok-sustainable-live-"
339 "edge-jianbing-gochujang-butcher-disrupt-tattooed-"
340 "tumeric-prism-photo-booth-vape-kogi-jean-shorts-"
341 "blog-williamsburg-fingerstache-palo-santo-artisan-"
342 "affogato-occupy-skateboard-adaptogen-neutra-celiac-"
343 "put-a-bird-on-it-kombucha-everyday-carry-hot-chicken-"
344 "craft-beer-subway-tile-tote-bag-disrupt-selvage-"
345 "raclette-art-party-readymade-paleo-heirloom-trust-"
346 "fund-small-batch-kinfolk-woke-cardigan-prism-"
347 "chambray-la-croix-hashtag-unicorn-edison-bulb-tbh-"
348 "cornhole-cliche-tattooed-green-juice-adaptogen-"
349 "kitsch-lo-fi-vexillologist-migas-gentrify-"
351 const argpar_opt_descr_t descrs
[] = {{0, '\0', opt_name
, true}, ARGPAR_OPT_DESCR_SENTINEL
};
354 sprintf(cmdline
, "--%s=23", opt_name
);
355 test_succeed(cmdline
, cmdline
, descrs
, 1);
360 * Parses `cmdline` with the argpar API using the option descriptors
361 * `descrs`, and ensures that argpar_iter_next() fails with status
362 * `expected_status` and that it sets an error having:
364 * ‣ The original argument index `expected_orig_index`.
368 * • The unknown option name `expected_unknown_opt_name`.
370 * • The option descriptor at index `expected_opt_descr_index` of
373 * • The option type `expected_is_short`.
375 * This function splits `cmdline` on spaces to create an original
378 static void test_fail(const char * const cmdline
, const argpar_error_type_t expected_error_type
,
379 const unsigned int expected_orig_index
,
380 const char * const expected_unknown_opt_name
,
381 const unsigned int expected_opt_descr_index
, const bool expected_is_short
,
382 const argpar_opt_descr_t
* const descrs
)
384 argpar_iter_t
*iter
= NULL
;
385 const argpar_item_t
*item
= NULL
;
386 gchar
** const argv
= g_strsplit(cmdline
, " ", 0);
388 const argpar_error_t
*error
= NULL
;
390 iter
= argpar_iter_create(g_strv_length(argv
), (const char * const *) argv
, descrs
);
394 argpar_iter_next_status_t status
;
396 ARGPAR_ITEM_DESTROY_AND_RESET(item
);
397 status
= argpar_iter_next(iter
, &item
, &error
);
398 ok(status
== ARGPAR_ITER_NEXT_STATUS_OK
||
399 (status
== ARGPAR_ITER_NEXT_STATUS_ERROR
&&
400 argpar_error_type(error
) == expected_error_type
),
401 "argpar_iter_next() returns the expected status and error type (%d) "
402 "for command line `%s` (call %u)",
403 expected_error_type
, cmdline
, i
+ 1);
405 if (status
!= ARGPAR_ITER_NEXT_STATUS_OK
) {
407 "argpar_iter_next() doesn't set an item for other status than "
408 "`ARGPAR_ITER_NEXT_STATUS_OK` and command line `%s` (call %u)",
411 "argpar_iter_next() sets an error for other status than "
412 "`ARGPAR_ITER_NEXT_STATUS_OK` and command line `%s` (call %u)",
414 ok(argpar_error_orig_index(error
) == expected_orig_index
,
415 "argpar_iter_next() sets an error with the expected original argument index "
416 "for command line `%s` (call %u)",
419 if (argpar_error_type(error
) == ARGPAR_ERROR_TYPE_UNKNOWN_OPT
) {
420 ok(strcmp(argpar_error_unknown_opt_name(error
), expected_unknown_opt_name
) == 0,
421 "argpar_iter_next() sets an error with the expected unknown option name "
422 "for command line `%s` (call %u)",
427 ok(argpar_error_opt_descr(error
, &is_short
) == &descrs
[expected_opt_descr_index
],
428 "argpar_iter_next() sets an error with the expected option descriptor "
429 "for command line `%s` (call %u)",
431 ok(is_short
== expected_is_short
,
432 "argpar_iter_next() sets an error with the expected option type "
433 "for command line `%s` (call %u)",
440 "argpar_iter_next() sets an item for status `ARGPAR_ITER_NEXT_STATUS_OK` "
441 "and command line `%s` (call %u)",
444 "argpar_iter_next() doesn't set an error for status `ARGPAR_ITER_NEXT_STATUS_OK` "
445 "and command line `%s` (call %u)",
450 ok(strcmp(expected_error, error) == 0,
451 "argpar_iter_next() sets the expected error string "
452 "for command line `%s`", cmdline);
454 if (strcmp(expected_error, error) != 0) {
455 diag("Expected: `%s`", expected_error);
456 diag("Got: `%s`", error);
460 argpar_item_destroy(item
);
461 argpar_iter_destroy(iter
);
462 argpar_error_destroy(error
);
466 static void fail_tests(void)
468 /* Unknown short option (space form) */
470 const argpar_opt_descr_t descrs
[] = {{0, 'd', NULL
, true}, ARGPAR_OPT_DESCR_SENTINEL
};
472 test_fail("-d salut -e -d meow", ARGPAR_ERROR_TYPE_UNKNOWN_OPT
, 2, "-e", 0, false, descrs
);
475 /* Unknown short option (glued form) */
477 const argpar_opt_descr_t descrs
[] = {{0, 'd', 0, true}, ARGPAR_OPT_DESCR_SENTINEL
};
479 test_fail("-dsalut -e -d meow", ARGPAR_ERROR_TYPE_UNKNOWN_OPT
, 1, "-e", 0, false, descrs
);
482 /* Unknown long option (space form) */
484 const argpar_opt_descr_t descrs
[] = {{0, '\0', "sink", true}, ARGPAR_OPT_DESCR_SENTINEL
};
486 test_fail("--sink party --food --sink impulse", ARGPAR_ERROR_TYPE_UNKNOWN_OPT
, 2, "--food",
490 /* Unknown long option (equal form) */
492 const argpar_opt_descr_t descrs
[] = {{0, '\0', "sink", true}, ARGPAR_OPT_DESCR_SENTINEL
};
494 test_fail("--sink=party --food --sink=impulse", ARGPAR_ERROR_TYPE_UNKNOWN_OPT
, 1, "--food",
498 /* Unknown option before non-option argument */
500 const argpar_opt_descr_t descrs
[] = {{0, '\0', "thumb", true}, ARGPAR_OPT_DESCR_SENTINEL
};
502 test_fail("--thumb=party --food=18 bateau --thumb waves", ARGPAR_ERROR_TYPE_UNKNOWN_OPT
, 1,
503 "--food", 0, false, descrs
);
506 /* Unknown option after non-option argument */
508 const argpar_opt_descr_t descrs
[] = {{0, '\0', "thumb", true}, ARGPAR_OPT_DESCR_SENTINEL
};
510 test_fail("--thumb=party wound --food --thumb waves", ARGPAR_ERROR_TYPE_UNKNOWN_OPT
, 2,
511 "--food", 0, false, descrs
);
514 /* Missing long option argument */
516 const argpar_opt_descr_t descrs
[] = {{0, '\0', "thumb", true}, ARGPAR_OPT_DESCR_SENTINEL
};
518 test_fail("allo --thumb", ARGPAR_ERROR_TYPE_MISSING_OPT_ARG
, 1, NULL
, 0, false, descrs
);
521 /* Missing short option argument */
523 const argpar_opt_descr_t descrs
[] = {{0, 'k', NULL
, true}, ARGPAR_OPT_DESCR_SENTINEL
};
525 test_fail("zoom heille -k", ARGPAR_ERROR_TYPE_MISSING_OPT_ARG
, 2, NULL
, 0, true, descrs
);
528 /* Missing short option argument (multiple glued) */
530 const argpar_opt_descr_t descrs
[] = {{0, 'a', NULL
, false},
531 {0, 'b', NULL
, false},
532 {0, 'c', NULL
, true},
533 ARGPAR_OPT_DESCR_SENTINEL
};
535 test_fail("-abc", ARGPAR_ERROR_TYPE_MISSING_OPT_ARG
, 0, NULL
, 2, true, descrs
);
538 /* Unexpected long option argument */
540 const argpar_opt_descr_t descrs
[] = {{0, 'c', "chevre", false}, ARGPAR_OPT_DESCR_SENTINEL
};
542 test_fail("ambulance --chevre=fromage tar -cjv", ARGPAR_ERROR_TYPE_UNEXPECTED_OPT_ARG
, 1,
543 NULL
, 0, false, descrs
);
552 return exit_status();