2 * SPDX-License-Identifier: GPL-2.0-only
3 * SPDX-FileCopyrightText: 2019-2024 Philippe Proulx <pproulx@efficios.com>
4 * SPDX-FileCopyrightText: 2020-2024 Simon Marchi <simon.marchi@efficios.com>
14 #include "argpar/argpar.h"
18 * Formats `item` and appends the resulting string to `res_str` to
19 * incrementally build an expected command line string.
23 * ‣ Prefers the `--long-opt=arg` style over the `-s arg` style.
25 * ‣ Uses the `arg<A,B>` form for non-option arguments, where `A` is the
26 * original argument index and `B` is the non-option argument index.
28 static void append_to_res_str(GString
* const res_str
, const argpar_item_t
* const item
)
30 if (res_str
->len
> 0) {
31 g_string_append_c(res_str
, ' ');
34 switch (argpar_item_type(item
)) {
35 case ARGPAR_ITEM_TYPE_OPT
:
37 const argpar_opt_descr_t
* const descr
= argpar_item_opt_descr(item
);
38 const char * const arg
= argpar_item_opt_arg(item
);
40 if (descr
->long_name
) {
41 g_string_append_printf(res_str
, "--%s", descr
->long_name
);
44 g_string_append_printf(res_str
, "=%s", arg
);
46 } else if (descr
->short_name
) {
47 g_string_append_printf(res_str
, "-%c", descr
->short_name
);
50 g_string_append_printf(res_str
, " %s", arg
);
56 case ARGPAR_ITEM_TYPE_NON_OPT
:
58 const char * const arg
= argpar_item_non_opt_arg(item
);
59 const unsigned int orig_index
= argpar_item_non_opt_orig_index(item
);
60 const unsigned int non_opt_index
= argpar_item_non_opt_non_opt_index(item
);
62 g_string_append_printf(res_str
, "%s<%u,%u>", arg
, orig_index
, non_opt_index
);
71 * Parses `cmdline` with the argpar API using the option descriptors
72 * `descrs`, and ensures that the resulting effective command line is
73 * `expected_cmd_line` and that the number of ingested original
74 * arguments is `expected_ingested_orig_args`.
76 * This function splits `cmdline` on spaces to create an original
79 * This function builds the resulting command line from parsing items
80 * by space-separating each formatted item (see append_to_res_str()).
82 static void test_succeed(const char * const cmdline
, const char * const expected_cmd_line
,
83 const argpar_opt_descr_t
* const descrs
,
84 const unsigned int expected_ingested_orig_args
)
86 argpar_iter_t
*iter
= NULL
;
87 const argpar_item_t
*item
= NULL
;
88 const argpar_error_t
*error
= NULL
;
89 GString
* const res_str
= g_string_new(NULL
);
90 gchar
** const argv
= g_strsplit(cmdline
, " ", 0);
91 unsigned int i
, actual_ingested_orig_args
;
95 iter
= argpar_iter_create(g_strv_length(argv
), (const char * const *) argv
, descrs
);
99 argpar_iter_next_status_t status
;
101 ARGPAR_ITEM_DESTROY_AND_RESET(item
);
102 status
= argpar_iter_next(iter
, &item
, &error
);
104 ok(status
== ARGPAR_ITER_NEXT_STATUS_OK
|| status
== ARGPAR_ITER_NEXT_STATUS_END
,
105 "argpar_iter_next() returns the expected status (%d) for command line `%s` (call %u)",
106 status
, cmdline
, i
+ 1);
107 ok(!error
, "argpar_iter_next() doesn't set an error for command line `%s` (call %u)",
110 if (status
== ARGPAR_ITER_NEXT_STATUS_END
) {
112 "argpar_iter_next() doesn't set an item for status `ARGPAR_ITER_NEXT_STATUS_END` "
113 "and command line `%s` (call %u)",
118 append_to_res_str(res_str
, item
);
121 actual_ingested_orig_args
= argpar_iter_ingested_orig_args(iter
);
122 ok(actual_ingested_orig_args
== expected_ingested_orig_args
,
123 "argpar_iter_ingested_orig_args() returns the expected number of ingested original "
124 "arguments for command line `%s`",
127 if (actual_ingested_orig_args
!= expected_ingested_orig_args
) {
128 diag("Expected: %u Got: %u", expected_ingested_orig_args
, actual_ingested_orig_args
);
131 ok(strcmp(expected_cmd_line
, res_str
->str
) == 0,
132 "argpar_iter_next() returns the expected parsing items for command line `%s`", cmdline
);
134 if (strcmp(expected_cmd_line
, res_str
->str
) != 0) {
135 diag("Expected: `%s`", expected_cmd_line
);
136 diag("Got: `%s`", res_str
->str
);
139 argpar_item_destroy(item
);
140 argpar_iter_destroy(iter
);
142 g_string_free(res_str
, TRUE
);
146 static void succeed_tests(void)
150 const argpar_opt_descr_t descrs
[] = {ARGPAR_OPT_DESCR_SENTINEL
};
152 test_succeed("", "", descrs
, 0);
155 /* Single long option */
157 const argpar_opt_descr_t descrs
[] = {{0, '\0', "salut", false}, ARGPAR_OPT_DESCR_SENTINEL
};
159 test_succeed("--salut", "--salut", descrs
, 1);
162 /* Single short option */
164 const argpar_opt_descr_t descrs
[] = {{0, 'f', NULL
, false}, ARGPAR_OPT_DESCR_SENTINEL
};
166 test_succeed("-f", "-f", descrs
, 1);
169 /* Short and long option (aliases) */
171 const argpar_opt_descr_t descrs
[] = {{0, 'f', "flaw", false}, ARGPAR_OPT_DESCR_SENTINEL
};
173 test_succeed("-f --flaw", "--flaw --flaw", descrs
, 2);
176 /* Long option with argument (space form) */
178 const argpar_opt_descr_t descrs
[] = {{0, '\0', "tooth", true}, ARGPAR_OPT_DESCR_SENTINEL
};
180 test_succeed("--tooth 67", "--tooth=67", descrs
, 2);
183 /* Long option with argument (equal form) */
185 const argpar_opt_descr_t descrs
[] = {{0, '\0', "polish", true}, ARGPAR_OPT_DESCR_SENTINEL
};
187 test_succeed("--polish=brick", "--polish=brick", descrs
, 1);
190 /* Short option with argument (space form) */
192 const argpar_opt_descr_t descrs
[] = {{0, 'c', NULL
, true}, ARGPAR_OPT_DESCR_SENTINEL
};
194 test_succeed("-c chilly", "-c chilly", descrs
, 2);
197 /* Short option with argument (glued form) */
199 const argpar_opt_descr_t descrs
[] = {{0, 'c', NULL
, true}, ARGPAR_OPT_DESCR_SENTINEL
};
201 test_succeed("-cchilly", "-c chilly", descrs
, 1);
204 /* Short and long option (aliases) with argument (all forms) */
206 const argpar_opt_descr_t descrs
[] = {{0, 'd', "dry", true}, ARGPAR_OPT_DESCR_SENTINEL
};
208 test_succeed("--dry=rate -dthing --dry street --dry=shape",
209 "--dry=rate --dry=thing --dry=street --dry=shape", descrs
, 5);
212 /* Many short options, last one with argument (glued form) */
214 const argpar_opt_descr_t descrs
[] = {{0, 'd', NULL
, false},
215 {0, 'e', NULL
, false},
216 {0, 'f', NULL
, true},
217 ARGPAR_OPT_DESCR_SENTINEL
};
219 test_succeed("-defmeow", "-d -e -f meow", descrs
, 1);
224 const argpar_opt_descr_t descrs
[] = {{0, 'd', NULL
, false},
225 {0, 'e', "east", true},
226 {0, '\0', "mind", false},
227 ARGPAR_OPT_DESCR_SENTINEL
};
229 test_succeed("-d --mind -destart --mind --east cough -d --east=itch",
230 "-d --mind -d --east=start --mind --east=cough -d --east=itch", descrs
, 8);
233 /* Single non-option argument */
235 const argpar_opt_descr_t descrs
[] = {ARGPAR_OPT_DESCR_SENTINEL
};
237 test_succeed("kilojoule", "kilojoule<0,0>", descrs
, 1);
240 /* Two non-option arguments */
242 const argpar_opt_descr_t descrs
[] = {ARGPAR_OPT_DESCR_SENTINEL
};
244 test_succeed("kilojoule mitaine", "kilojoule<0,0> mitaine<1,1>", descrs
, 2);
247 /* Single non-option argument mixed with options */
249 const argpar_opt_descr_t descrs
[] = {{0, 'd', NULL
, false},
250 {0, '\0', "squeeze", true},
251 ARGPAR_OPT_DESCR_SENTINEL
};
253 test_succeed("-d sprout yes --squeeze little bag -d",
254 "-d sprout<1,0> yes<2,1> --squeeze=little bag<5,2> -d", descrs
, 7);
259 const argpar_opt_descr_t descrs
[] = {{0, '\0', "-fuel", true}, ARGPAR_OPT_DESCR_SENTINEL
};
261 test_succeed("---fuel=three", "---fuel=three", descrs
, 1);
264 /* Long option containing `=` in argument (equal form) */
266 const argpar_opt_descr_t descrs
[] = {{0, '\0', "zebra", true}, ARGPAR_OPT_DESCR_SENTINEL
};
268 test_succeed("--zebra=three=yes", "--zebra=three=yes", descrs
, 1);
271 /* Short option's argument starting with `-` (glued form) */
273 const argpar_opt_descr_t descrs
[] = {{0, 'z', NULL
, true}, ARGPAR_OPT_DESCR_SENTINEL
};
275 test_succeed("-z-will", "-z -will", descrs
, 1);
278 /* Short option's argument starting with `-` (space form) */
280 const argpar_opt_descr_t descrs
[] = {{0, 'z', NULL
, true}, ARGPAR_OPT_DESCR_SENTINEL
};
282 test_succeed("-z -will", "-z -will", descrs
, 2);
285 /* Long option's argument starting with `-` (space form) */
287 const argpar_opt_descr_t descrs
[] = {{0, '\0', "janine", true}, ARGPAR_OPT_DESCR_SENTINEL
};
289 test_succeed("--janine -sutto", "--janine=-sutto", descrs
, 2);
292 /* Long option's argument starting with `-` (equal form) */
294 const argpar_opt_descr_t descrs
[] = {{0, '\0', "janine", true}, ARGPAR_OPT_DESCR_SENTINEL
};
296 test_succeed("--janine=-sutto", "--janine=-sutto", descrs
, 1);
299 /* Long option's empty argument (equal form) */
301 const argpar_opt_descr_t descrs
[] = {{0, 'f', NULL
, false},
302 {0, '\0', "yeah", true},
303 ARGPAR_OPT_DESCR_SENTINEL
};
305 test_succeed("-f --yeah= -f", "-f --yeah= -f", descrs
, 3);
308 /* `-` non-option argument */
310 const argpar_opt_descr_t descrs
[] = {{0, 'f', NULL
, false}, ARGPAR_OPT_DESCR_SENTINEL
};
312 test_succeed("-f - -f", "-f -<1,0> -f", descrs
, 3);
315 /* `--` non-option argument */
317 const argpar_opt_descr_t descrs
[] = {{0, 'f', NULL
, false}, ARGPAR_OPT_DESCR_SENTINEL
};
319 test_succeed("-f -- -f", "-f --<1,0> -f", descrs
, 3);
322 /* Very long name of long option */
324 const char opt_name
[] = "kale-chips-waistcoat-yr-bicycle-rights-gochujang-"
325 "woke-tumeric-flexitarian-biodiesel-chillwave-cliche-"
326 "ethical-cardigan-listicle-pok-pok-sustainable-live-"
327 "edge-jianbing-gochujang-butcher-disrupt-tattooed-"
328 "tumeric-prism-photo-booth-vape-kogi-jean-shorts-"
329 "blog-williamsburg-fingerstache-palo-santo-artisan-"
330 "affogato-occupy-skateboard-adaptogen-neutra-celiac-"
331 "put-a-bird-on-it-kombucha-everyday-carry-hot-chicken-"
332 "craft-beer-subway-tile-tote-bag-disrupt-selvage-"
333 "raclette-art-party-readymade-paleo-heirloom-trust-"
334 "fund-small-batch-kinfolk-woke-cardigan-prism-"
335 "chambray-la-croix-hashtag-unicorn-edison-bulb-tbh-"
336 "cornhole-cliche-tattooed-green-juice-adaptogen-"
337 "kitsch-lo-fi-vexillologist-migas-gentrify-"
339 const argpar_opt_descr_t descrs
[] = {{0, '\0', opt_name
, true}, ARGPAR_OPT_DESCR_SENTINEL
};
342 sprintf(cmdline
, "--%s=23", opt_name
);
343 test_succeed(cmdline
, cmdline
, descrs
, 1);
348 * Parses `cmdline` with the argpar API using the option descriptors
349 * `descrs`, and ensures that argpar_iter_next() fails with status
350 * `expected_status` and that it sets an error having:
352 * ‣ The original argument index `expected_orig_index`.
356 * • The unknown option name `expected_unknown_opt_name`.
358 * • The option descriptor at index `expected_opt_descr_index` of
361 * • The option type `expected_is_short`.
363 * This function splits `cmdline` on spaces to create an original
366 static void test_fail(const char * const cmdline
, const argpar_error_type_t expected_error_type
,
367 const unsigned int expected_orig_index
,
368 const char * const expected_unknown_opt_name
,
369 const unsigned int expected_opt_descr_index
, const bool expected_is_short
,
370 const argpar_opt_descr_t
* const descrs
)
372 argpar_iter_t
*iter
= NULL
;
373 const argpar_item_t
*item
= NULL
;
374 gchar
** const argv
= g_strsplit(cmdline
, " ", 0);
376 const argpar_error_t
*error
= NULL
;
378 iter
= argpar_iter_create(g_strv_length(argv
), (const char * const *) argv
, descrs
);
382 argpar_iter_next_status_t status
;
384 ARGPAR_ITEM_DESTROY_AND_RESET(item
);
385 status
= argpar_iter_next(iter
, &item
, &error
);
386 ok(status
== ARGPAR_ITER_NEXT_STATUS_OK
||
387 (status
== ARGPAR_ITER_NEXT_STATUS_ERROR
&&
388 argpar_error_type(error
) == expected_error_type
),
389 "argpar_iter_next() returns the expected status and error type (%d) "
390 "for command line `%s` (call %u)",
391 expected_error_type
, cmdline
, i
+ 1);
393 if (status
!= ARGPAR_ITER_NEXT_STATUS_OK
) {
395 "argpar_iter_next() doesn't set an item for other status than "
396 "`ARGPAR_ITER_NEXT_STATUS_OK` and command line `%s` (call %u)",
399 "argpar_iter_next() sets an error for other status than "
400 "`ARGPAR_ITER_NEXT_STATUS_OK` and command line `%s` (call %u)",
402 ok(argpar_error_orig_index(error
) == expected_orig_index
,
403 "argpar_iter_next() sets an error with the expected original argument index "
404 "for command line `%s` (call %u)",
407 if (argpar_error_type(error
) == ARGPAR_ERROR_TYPE_UNKNOWN_OPT
) {
408 ok(strcmp(argpar_error_unknown_opt_name(error
), expected_unknown_opt_name
) == 0,
409 "argpar_iter_next() sets an error with the expected unknown option name "
410 "for command line `%s` (call %u)",
415 ok(argpar_error_opt_descr(error
, &is_short
) == &descrs
[expected_opt_descr_index
],
416 "argpar_iter_next() sets an error with the expected option descriptor "
417 "for command line `%s` (call %u)",
419 ok(is_short
== expected_is_short
,
420 "argpar_iter_next() sets an error with the expected option type "
421 "for command line `%s` (call %u)",
428 "argpar_iter_next() sets an item for status `ARGPAR_ITER_NEXT_STATUS_OK` "
429 "and command line `%s` (call %u)",
432 "argpar_iter_next() doesn't set an error for status `ARGPAR_ITER_NEXT_STATUS_OK` "
433 "and command line `%s` (call %u)",
438 ok(strcmp(expected_error, error) == 0,
439 "argpar_iter_next() sets the expected error string "
440 "for command line `%s`", cmdline);
442 if (strcmp(expected_error, error) != 0) {
443 diag("Expected: `%s`", expected_error);
444 diag("Got: `%s`", error);
448 argpar_item_destroy(item
);
449 argpar_iter_destroy(iter
);
450 argpar_error_destroy(error
);
454 static void fail_tests(void)
456 /* Unknown short option (space form) */
458 const argpar_opt_descr_t descrs
[] = {{0, 'd', NULL
, true}, ARGPAR_OPT_DESCR_SENTINEL
};
460 test_fail("-d salut -e -d meow", ARGPAR_ERROR_TYPE_UNKNOWN_OPT
, 2, "-e", 0, false, descrs
);
463 /* Unknown short option (glued form) */
465 const argpar_opt_descr_t descrs
[] = {{0, 'd', 0, true}, ARGPAR_OPT_DESCR_SENTINEL
};
467 test_fail("-dsalut -e -d meow", ARGPAR_ERROR_TYPE_UNKNOWN_OPT
, 1, "-e", 0, false, descrs
);
470 /* Unknown long option (space form) */
472 const argpar_opt_descr_t descrs
[] = {{0, '\0', "sink", true}, ARGPAR_OPT_DESCR_SENTINEL
};
474 test_fail("--sink party --food --sink impulse", ARGPAR_ERROR_TYPE_UNKNOWN_OPT
, 2, "--food",
478 /* Unknown long option (equal form) */
480 const argpar_opt_descr_t descrs
[] = {{0, '\0', "sink", true}, ARGPAR_OPT_DESCR_SENTINEL
};
482 test_fail("--sink=party --food --sink=impulse", ARGPAR_ERROR_TYPE_UNKNOWN_OPT
, 1, "--food",
486 /* Unknown option before non-option argument */
488 const argpar_opt_descr_t descrs
[] = {{0, '\0', "thumb", true}, ARGPAR_OPT_DESCR_SENTINEL
};
490 test_fail("--thumb=party --food=18 bateau --thumb waves", ARGPAR_ERROR_TYPE_UNKNOWN_OPT
, 1,
491 "--food", 0, false, descrs
);
494 /* Unknown option after non-option argument */
496 const argpar_opt_descr_t descrs
[] = {{0, '\0', "thumb", true}, ARGPAR_OPT_DESCR_SENTINEL
};
498 test_fail("--thumb=party wound --food --thumb waves", ARGPAR_ERROR_TYPE_UNKNOWN_OPT
, 2,
499 "--food", 0, false, descrs
);
502 /* Missing long option argument */
504 const argpar_opt_descr_t descrs
[] = {{0, '\0', "thumb", true}, ARGPAR_OPT_DESCR_SENTINEL
};
506 test_fail("allo --thumb", ARGPAR_ERROR_TYPE_MISSING_OPT_ARG
, 1, NULL
, 0, false, descrs
);
509 /* Missing short option argument */
511 const argpar_opt_descr_t descrs
[] = {{0, 'k', NULL
, true}, ARGPAR_OPT_DESCR_SENTINEL
};
513 test_fail("zoom heille -k", ARGPAR_ERROR_TYPE_MISSING_OPT_ARG
, 2, NULL
, 0, true, descrs
);
516 /* Missing short option argument (multiple glued) */
518 const argpar_opt_descr_t descrs
[] = {{0, 'a', NULL
, false},
519 {0, 'b', NULL
, false},
520 {0, 'c', NULL
, true},
521 ARGPAR_OPT_DESCR_SENTINEL
};
523 test_fail("-abc", ARGPAR_ERROR_TYPE_MISSING_OPT_ARG
, 0, NULL
, 2, true, descrs
);
526 /* Unexpected long option argument */
528 const argpar_opt_descr_t descrs
[] = {{0, 'c', "chevre", false}, ARGPAR_OPT_DESCR_SENTINEL
};
530 test_fail("ambulance --chevre=fromage tar -cjv", ARGPAR_ERROR_TYPE_UNEXPECTED_OPT_ARG
, 1,
531 NULL
, 0, false, descrs
);
540 return exit_status();