Skip to content

Commit 65dce6f

Browse files
committed
usage: add USAGE_STOP_PARSING to stop parsing
Some arguments like `--help` are successful and should stop the parsing, so that we do not enforce required arguments.
1 parent 0d7a704 commit 65dce6f

File tree

3 files changed

+43
-3
lines changed

3 files changed

+43
-3
lines changed

adopt.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,9 @@ adopt_status_t adopt_parse(
369369
opt->status != ADOPT_STATUS_DONE)
370370
return opt->status;
371371

372+
if ((opt->spec->usage & ADOPT_USAGE_STOP_PARSING))
373+
return (opt->status = ADOPT_STATUS_DONE);
374+
372375
given_specs[given_idx++] = opt->spec;
373376
}
374377

adopt.h

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ typedef enum {
5252
* restrictions and usage information to be displayed to the end-user.
5353
*/
5454
typedef enum {
55+
/** Defaults for the argument. */
56+
ADOPT_USAGE_DEFAULT = 0,
57+
5558
/** This argument is required. */
5659
ADOPT_USAGE_REQUIRED = (1u << 0),
5760

@@ -63,14 +66,20 @@ typedef enum {
6366
*/
6467
ADOPT_USAGE_CHOICE = (1u << 1),
6568

69+
/**
70+
* This argument short-circuits the remainder of parsing.
71+
* Useful for arguments like `--help`.
72+
*/
73+
ADOPT_USAGE_STOP_PARSING = (1u << 2),
74+
6675
/** The argument's value is optional ("-n" or "-n foo") */
67-
ADOPT_USAGE_VALUE_OPTIONAL = (1u << 2),
76+
ADOPT_USAGE_VALUE_OPTIONAL = (1u << 3),
6877

6978
/** This argument should not be displayed in usage. */
70-
ADOPT_USAGE_HIDDEN = (1u << 3),
79+
ADOPT_USAGE_HIDDEN = (1u << 4),
7180

7281
/** In usage, show the long format instead of the abbreviated format. */
73-
ADOPT_USAGE_SHOW_LONG = (1u << 4),
82+
ADOPT_USAGE_SHOW_LONG = (1u << 5),
7483
} adopt_usage_t;
7584

7685
/** Specification for an available option. */

tests/adopt.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -922,3 +922,31 @@ void test_adopt__choice_switch_or_arg_advances_arg(void)
922922
cl_assert_equal_p(NULL, baz);
923923
cl_assert_equal_s("actually_final", final);
924924
}
925+
926+
void test_adopt__stop(void)
927+
{
928+
int foo = 0, bar = 0, help = 0, baz = 0;
929+
adopt_opt result;
930+
931+
adopt_spec specs[] = {
932+
{ ADOPT_TYPE_SWITCH, "foo", 'f', &foo, 'f', ADOPT_USAGE_REQUIRED },
933+
{ ADOPT_TYPE_SWITCH, "bar", 0, &bar, 'b', ADOPT_USAGE_REQUIRED },
934+
{ ADOPT_TYPE_SWITCH, "help", 0, &help, 'h', ADOPT_USAGE_STOP_PARSING },
935+
{ ADOPT_TYPE_SWITCH, "baz", 0, &baz, 'z', ADOPT_USAGE_REQUIRED },
936+
{ 0 },
937+
};
938+
939+
char *args[] = { "-f", "--help", "-z" };
940+
941+
cl_assert_equal_i(ADOPT_STATUS_DONE, adopt_parse(&result, specs, args, 2));
942+
943+
cl_assert_equal_i(ADOPT_STATUS_DONE, result.status);
944+
cl_assert_equal_s("--help", result.arg);
945+
cl_assert_equal_p(NULL, result.value);
946+
cl_assert_equal_i(0, result.args_len);
947+
948+
cl_assert_equal_i('f', foo);
949+
cl_assert_equal_p('h', help);
950+
cl_assert_equal_p(0, bar);
951+
cl_assert_equal_p(0, baz);
952+
}

0 commit comments

Comments
 (0)