@@ -15,9 +15,14 @@ Types of options
1515----------------
1616
1717* Boolean values are options that do not take a value, and are
18- either set or unset, for example ` -v ` or ` --verbose ` . Booleans
19- are shorthand for switches that are assigned a value of ` 1 ` when
20- present.
18+ either set or unset, for example ` -v ` or ` --verbose ` . If a
19+ boolean has a long name (eg ` --verbose ` ) then it implicitly has
20+ a negation prefixed by ` no- ` (in this case, ` --no-verbose ` ).
21+ The given value will be set to ` 1 ` when the boolean is given on
22+ the command line and ` 0 ` when its negation is given.
23+ * Accumulators are options that can be provided multiple times to
24+ increase its value. For example, ` -v ` will set verbosity to ` 1 ` ,
25+ but ` -vvv ` will set verbosity to ` 3 ` .
2126* Switches are options that do not take a value on the command
2227 line, for example ` --long ` or ` --short ` . When a switch is present
2328 on the command line, a variable will be set to a predetermined value.
@@ -28,8 +33,8 @@ Types of options
2833* Literal separators are bare double-dashes, ` -- ` as a lone
2934 word, indicating that further words will be treated as
3035 arguments (even if they begin with a dash).
31- * Arguments are options that are bare words, not prefixed with
32- a single or double dash, for example ` filename.txt ` .
36+ * Arguments are bare words, not prefixed with a single or double dash,
37+ for example ` filename.txt ` .
3338* Argument lists are the remainder of arguments, not prefixed
3439 with dashes. For example, an array: ` file1.txt ` , ` file2.txt ` ,
3540 ...
@@ -41,15 +46,21 @@ Options should be specified as an array of `adopt_spec` elements,
4146elements, terminated with an ` adopt_spec ` initialized to zeroes.
4247
4348``` c
49+ int debug = 0 ;
4450int verbose = 0 ;
4551int volume = 1 ;
4652char *channel = " default" ;
4753char *filename1 = NULL ;
4854char *filename2 = NULL ;
4955
5056adopt_spec opt_specs[] = {
51- /* `verbose`, above, will be set to `1` when specified. */
52- { ADOPT_BOOL, "verbose", 'v', &verbose },
57+ /* `verbose`, above, will increment each time `-v` is specified. */
58+ { ADOPT_ACCUMULATOR, "verbose", 'v', &verbose },
59+
60+ /* `debug`, above, will be set to `1` when `--debug` is specified,
61+ * or to `0` if `--no-debug` is specified.
62+ */
63+ { ADOPT_BOOL, "debug", 'd', &debug },
5364
5465 /* `volume` will be set to `0` when `--quiet` is specified, and
5566 * set to `2` when `--loud` is specified. if neither is specified,
@@ -99,7 +110,7 @@ int main(int argc, const char **argv)
99110 const char * value;
100111 const char * file;
101112
102- if (adopt_parse(&result, opt_specs, argv + 1, argc - 1) < 0) {
113+ if (adopt_parse(&result, opt_specs, argv + 1, argc - 1, ADOPT_PARSE_DEFAULT ) < 0) {
103114 adopt_status_fprint(stderr, &opt);
104115 adopt_usage_fprint(stderr, argv[ 0] , opt_specs);
105116 return 129;
0 commit comments