Skip to content

Commit 98d01a0

Browse files
committed
usage: move ahead of help in the struct
Since the usage information is now used for parsing and not only displaying to end-users, it's less optional. Move it ahead of the help information in the struct.
1 parent e22e057 commit 98d01a0

File tree

4 files changed

+48
-45
lines changed

4 files changed

+48
-45
lines changed

adopt.h

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ typedef enum {
4848
} adopt_type_t;
4949

5050
/**
51-
* Usage information for an argument, to be displayed to the end-user.
52-
* This is only for display, the parser ignores this usage information.
51+
* Additional information about an option, including parsing
52+
* restrictions and usage information to be displayed to the end-user.
5353
*/
5454
typedef enum {
5555
/** This argument is required. */
@@ -77,47 +77,50 @@ typedef struct adopt_spec {
7777
const char alias;
7878

7979
/**
80-
* If this spec is of type `ADOPT_BOOL`, this is a pointer to
81-
* an `int` that will be set to `1` if the option is specified.
80+
* If this spec is of type `ADOPT_TYPE_BOOL`, this is a pointer
81+
* to an `int` that will be set to `1` if the option is specified.
8282
*
83-
* If this spec is of type `ADOPT_SWITCH`, this is a pointer to
84-
* an `int` that will be set to the opt's `value` (below) when
83+
* If this spec is of type `ADOPT_TYPE_SWITCH`, this is a pointer
84+
* to an `int` that will be set to the opt's `value` (below) when
8585
* this option is specified.
8686
*
87-
* If this spec is of type `ADOPT_VALUE` or `ADOPT_VALUE_OPTIONAL`,
88-
* this is a pointer to a `char *`, that will be set to the value
87+
* If this spec is of type `ADOPT_TYPE_VALUE`,
88+
* `ADOPT_TYPE_VALUE_OPTIONAL`, or `ADOPT_TYPE_ARG`, this is
89+
* a pointer to a `char *` that will be set to the value
8990
* specified on the command line.
9091
*
91-
* If this spec is of type `ADOPT_VALUES`, this is a pointer to a
92-
* `char **` that will be set to the remaining values specified on
93-
* the command line.
92+
* If this spec is of type `ADOPT_TYPE_ARGS`, this is a pointer
93+
* to a `char **` that will be set to the remaining values
94+
* specified on the command line.
9495
*/
9596
void *value;
9697

9798
/**
98-
* If this spec is of type `ADOPT_SWITCH`, this is the value to
99-
* set in the option's `value_ptr` pointer when it is specified.
99+
* If this spec is of type `ADOPT_TYPE_SWITCH`, this is the value
100+
* to set in the option's `value_ptr` pointer when it is specified.
100101
* This is ignored for other opt types.
101102
*/
102103
int switch_value;
103104

104105
/**
105106
* The name of the value, provided when creating usage information.
106107
* This is required only for the functions that display usage
107-
* information and only when a spec is of type `ADOPT_VALUE`.
108+
* information and only when a spec is of type `ADOPT_TYPE_VALUE,
109+
* `ADOPT_TYPE_ARG` or `ADOPT_TYPE_ARGS``.
108110
*/
109111
const char *value_name;
110112

111113
/**
112-
* Short description of the option, used when creating usage
113-
* information. This is only used when creating usage information.
114+
* Optional usage flags that change parsing behavior and how
115+
* usage information is shown to the end-user.
114116
*/
115-
const char *help;
117+
uint32_t usage;
116118

117119
/**
118-
* Optional `adopt_usage_t`, used when creating usage information.
120+
* Optional short description of the option to display to the
121+
* end-user. This is only used when creating usage information.
119122
*/
120-
adopt_usage_t usage;
123+
const char *help;
121124
} adopt_spec;
122125

123126
/** Return value for `adopt_parser_next`. */

examples/loop.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,20 @@ static char **other = NULL;
1919

2020
adopt_spec opt_specs[] = {
2121
{ ADOPT_TYPE_BOOL, "verbose", 'v', &verbose, 0,
22-
NULL, "Turn on verbose information", 0 },
22+
NULL, 0, "Turn on verbose information" },
2323
{ ADOPT_TYPE_SWITCH, "quiet", 'q', &volume, 0,
24-
NULL, "Emit no output", ADOPT_USAGE_REQUIRED },
24+
NULL, ADOPT_USAGE_REQUIRED, "Emit no output" },
2525
{ ADOPT_TYPE_SWITCH, "loud", 'l', &volume, 2,
26-
NULL, "Emit louder than usual output", ADOPT_USAGE_CHOICE },
26+
NULL, ADOPT_USAGE_CHOICE, "Emit louder than usual output" },
2727
{ ADOPT_TYPE_VALUE, "channel", 'c', &channel, 0,
28-
"channel", "Set the channel", 0 },
28+
"channel", 0, "Set the channel" },
2929
{ ADOPT_TYPE_LITERAL },
3030
{ ADOPT_TYPE_ARG, NULL, 0, &filename1, 0,
31-
"file1", "The first filename", ADOPT_USAGE_REQUIRED },
31+
"file1", ADOPT_USAGE_REQUIRED, "The first filename" },
3232
{ ADOPT_TYPE_ARG, NULL, 0, &filename2, 0,
33-
"file2", "The second (optional) filename", 0 },
33+
"file2", 0, "The second (optional) filename" },
3434
{ ADOPT_TYPE_ARGS, NULL, 0, &other, 0,
35-
"other", "The other (optional) arguments", 0 },
35+
"other", 0, "The other (optional) arguments" },
3636
{ 0 }
3737
};
3838

examples/parse.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,20 @@ static char **other = NULL;
1919

2020
adopt_spec opt_specs[] = {
2121
{ ADOPT_TYPE_BOOL, "verbose", 'v', &verbose, 0,
22-
NULL, "Turn on verbose information", 0 },
22+
NULL, 0, "Turn on verbose information" },
2323
{ ADOPT_TYPE_SWITCH, "quiet", 'q', &volume, 0,
24-
NULL, "Emit no output", ADOPT_USAGE_REQUIRED },
24+
NULL, ADOPT_USAGE_REQUIRED, "Emit no output" },
2525
{ ADOPT_TYPE_SWITCH, "loud", 'l', &volume, 2,
26-
NULL, "Emit louder than usual output", ADOPT_USAGE_CHOICE },
26+
NULL, ADOPT_USAGE_CHOICE, "Emit louder than usual output" },
2727
{ ADOPT_TYPE_VALUE, "channel", 'c', &channel, 0,
28-
"channel", "Set the channel", 0 },
28+
"channel", 0, "Set the channel" },
2929
{ ADOPT_TYPE_LITERAL },
3030
{ ADOPT_TYPE_ARG, NULL, 0, &filename1, 0,
31-
"file1", "The first filename", ADOPT_USAGE_REQUIRED },
31+
"file1", ADOPT_USAGE_REQUIRED, "The first filename" },
3232
{ ADOPT_TYPE_ARG, NULL, 0, &filename2, 0,
33-
"file2", "The second (optional) filename", 0 },
33+
"file2", 0, "The second (optional) filename" },
3434
{ ADOPT_TYPE_ARGS, NULL, 0, &other, 0,
35-
"other", "The other (optional) arguments", 0 },
35+
"other", 0, "The other (optional) arguments" },
3636
{ 0 }
3737
};
3838

tests/adopt.c

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -782,9 +782,9 @@ void test_adopt__required_choice_missing(void)
782782
adopt_opt result;
783783

784784
adopt_spec specs[] = {
785-
{ ADOPT_TYPE_SWITCH, "foo", 'f', &foo, 'f', NULL, NULL, ADOPT_USAGE_REQUIRED },
786-
{ ADOPT_TYPE_VALUE, "bar", 0, &bar, 'b', NULL, NULL, ADOPT_USAGE_CHOICE },
787-
{ ADOPT_TYPE_ARGS, "argz", 0, &argz, 0, NULL, NULL, 0 },
785+
{ ADOPT_TYPE_SWITCH, "foo", 'f', &foo, 'f', NULL, ADOPT_USAGE_REQUIRED },
786+
{ ADOPT_TYPE_VALUE, "bar", 0, &bar, 'b', NULL, ADOPT_USAGE_CHOICE },
787+
{ ADOPT_TYPE_ARGS, "argz", 0, &argz, 0, NULL, 0 },
788788
{ 0 },
789789
};
790790

@@ -807,10 +807,10 @@ void test_adopt__required_choice_specified(void)
807807
adopt_opt result;
808808

809809
adopt_spec specs[] = {
810-
{ ADOPT_TYPE_SWITCH, "foo", 'f', &foo, 'f', NULL, NULL, ADOPT_USAGE_REQUIRED },
811-
{ ADOPT_TYPE_VALUE, "bar", 0, &bar, 'b', NULL, NULL, ADOPT_USAGE_CHOICE },
812-
{ ADOPT_TYPE_ARG, "baz", 0, &baz, 'z', NULL, NULL, ADOPT_USAGE_REQUIRED },
813-
{ ADOPT_TYPE_ARGS, "argz", 0, &argz, 0, NULL, NULL, 0 },
810+
{ ADOPT_TYPE_SWITCH, "foo", 'f', &foo, 'f', NULL, ADOPT_USAGE_REQUIRED },
811+
{ ADOPT_TYPE_VALUE, "bar", 0, &bar, 'b', NULL, ADOPT_USAGE_CHOICE },
812+
{ ADOPT_TYPE_ARG, "baz", 0, &baz, 'z', NULL, ADOPT_USAGE_REQUIRED },
813+
{ ADOPT_TYPE_ARGS, "argz", 0, &argz, 0, NULL, 0 },
814814
{ 0 },
815815
};
816816

@@ -833,11 +833,11 @@ void test_adopt__choice_switch_or_arg_advances_arg(void)
833833
adopt_opt result;
834834

835835
adopt_spec specs[] = {
836-
{ ADOPT_TYPE_SWITCH, "foo", 'f', &foo, 'f', NULL, NULL },
837-
{ ADOPT_TYPE_SWITCH, "fooz", 'z', &foo, 'z', NULL, NULL, ADOPT_USAGE_CHOICE },
838-
{ ADOPT_TYPE_VALUE, "bar", 0, &bar, 'b', NULL, NULL, ADOPT_USAGE_CHOICE },
839-
{ ADOPT_TYPE_ARG, "baz", 0, &baz, 0, NULL, NULL, ADOPT_USAGE_CHOICE },
840-
{ ADOPT_TYPE_ARG, "final", 0, &final, 0, NULL, NULL, 0 },
836+
{ ADOPT_TYPE_SWITCH, "foo", 'f', &foo, 'f', NULL, 0 },
837+
{ ADOPT_TYPE_SWITCH, "fooz", 'z', &foo, 'z', NULL, ADOPT_USAGE_CHOICE },
838+
{ ADOPT_TYPE_VALUE, "bar", 0, &bar, 'b', NULL, ADOPT_USAGE_CHOICE },
839+
{ ADOPT_TYPE_ARG, "baz", 0, &baz, 0, NULL, ADOPT_USAGE_CHOICE },
840+
{ ADOPT_TYPE_ARG, "final", 0, &final, 0, NULL, 0 },
841841
{ 0 },
842842
};
843843

0 commit comments

Comments
 (0)