@@ -40,45 +40,47 @@ Specifying command-line options
40
40
Options should be specified as an array of ` adopt_spec ` elements,
41
41
elements, terminated with an ` adopt_spec ` initialized to zeroes.
42
42
43
- int verbose = 0;
44
- int volume = 1;
45
- char *channel = "default";
46
- char *filename1 = NULL;
47
- char *filename2 = NULL;
48
-
49
- adopt_spec opt_specs[] = {
50
- /* `verbose`, above, will be set to `1` when specified. */
51
- { ADOPT_BOOL, "verbose", 'v', &verbose },
52
-
53
- /* `volume` will be set to `0` when `--quiet` is specified, and
54
- * set to `2` when `--loud` is specified. if neither is specified,
55
- * it will retain its default value of `1`, defined above.
56
- */
57
- { ADOPT_SWITCH, "quiet", 'q', &volume, 0 },
58
- { ADOPT_SWITCH, "loud", 'l', &volume, 2 },
59
-
60
- /* `channel` will be set to the given argument if an argument is
61
- * provided to `--channel`, or will be set to `NULL` if no argument
62
- * was specified.
63
- */
64
- { ADOPT_VALUE, "channel", 'c', &channel, NULL },
65
-
66
- /* A double dash (`--`) may be specified to indicate that the parser
67
- * should stop treated arguments as possible options and treat
68
- * remaining arguments as literal arguments; which allows a user
69
- * to specify `--loud` as an actual literal filename (below).
70
- */
71
- { ADOPT_LITERAL },
72
-
73
- /* `filename` will be set to the first bare argument */
74
- { ADOPT_ARG, NULL, 0, &filename1 },
75
-
76
- /* `filename2` will be set to the first bare argument */
77
- { ADOPT_ARG, NULL, 0, &filename2 },
78
-
79
- /* End of the specification list. */
80
- { 0 },
81
- };
43
+ ``` c
44
+ int verbose = 0 ;
45
+ int volume = 1 ;
46
+ char *channel = " default" ;
47
+ char *filename1 = NULL ;
48
+ char *filename2 = NULL ;
49
+
50
+ adopt_spec opt_specs[] = {
51
+ /* `verbose`, above, will be set to `1` when specified. */
52
+ { ADOPT_BOOL, "verbose", 'v', &verbose },
53
+
54
+ /* `volume` will be set to `0` when `--quiet` is specified, and
55
+ * set to `2` when `--loud` is specified. if neither is specified,
56
+ * it will retain its default value of `1`, defined above.
57
+ */
58
+ { ADOPT_SWITCH, "quiet", 'q', &volume, 0 },
59
+ { ADOPT_SWITCH, "loud", 'l', &volume, 2 },
60
+
61
+ /* `channel` will be set to the given argument if an argument is
62
+ * provided to `--channel`, or will be set to `NULL` if no argument
63
+ * was specified.
64
+ */
65
+ { ADOPT_VALUE, "channel", 'c', &channel, NULL },
66
+
67
+ /* A double dash (`--`) may be specified to indicate that the parser
68
+ * should stop treated arguments as possible options and treat
69
+ * remaining arguments as literal arguments; which allows a user
70
+ * to specify `--loud` as an actual literal filename (below).
71
+ */
72
+ { ADOPT_LITERAL },
73
+
74
+ /* `filename` will be set to the first bare argument */
75
+ { ADOPT_ARG, NULL, 0, &filename1 },
76
+
77
+ /* `filename2` will be set to the first bare argument */
78
+ { ADOPT_ARG, NULL, 0, &filename2 },
79
+
80
+ /* End of the specification list. */
81
+ { 0 },
82
+ };
83
+ ```
82
84
83
85
Parsing arguments
84
86
-----------------
@@ -152,20 +154,24 @@ the `--channel` option is specified without a value, then `channel` will
152
154
be set to ` NULL ` when it is read. This can be detected either during the
153
155
processing loop, or after. For example:
154
156
155
- if (!channel) {
156
- fprintf(stderr, "Option: %s requires an argument\n", opt.arg);
157
- return 129;
158
- }
157
+ ``` c
158
+ if (!channel) {
159
+ fprintf (stderr, "Option: %s requires an argument\n", opt.arg);
160
+ return 129;
161
+ }
162
+ ```
159
163
160
164
### Inspecting the ` opt ` struct
161
165
162
166
If you cannot use a sentinal value, perhaps because ` NULL ` is a useful
163
167
value, you can also inspect the ` opt ` struct. For example:
164
168
165
- if (opt.spec->value == &channel && !channel) {
166
- fprintf(stderr, "Option: %s requires an argument\n", opt.arg);
167
- return 129;
168
- }
169
+ ``` c
170
+ if (opt.spec->value == &channel && !channel) {
171
+ fprintf (stderr, "Option: %s requires an argument\n", opt.arg);
172
+ return 129;
173
+ }
174
+ ```
169
175
170
176
Displaying usage
171
177
----------------
@@ -187,49 +193,55 @@ will display it within square brackets.)
187
193
188
194
Adding these to the above example:
189
195
190
- adopt_spec opt_specs[] = {
191
- /* For bools and switches, you probably only want to set the help.
192
- * There is no value to describe, and these are unlikely to be
193
- * required.
194
- */
195
- { ADOPT_BOOL, "verbose", 'v', &verbose,
196
- NULL, "Turn on verbose mode", 0 },
197
-
198
- /* Specifying that an option is an `ADOPT_USAGE_CHOICE` indicates
199
- * that it is orthogonal to the previous entry. These two options
200
- * will be rendered together in help output as `[-q|-l]`.
201
- */
202
- { ADOPT_SWITCH, "quiet", 'q', &volume, 0,
203
- NULL, "Emit no output", 0 },
204
- { ADOPT_SWITCH, "loud", 'l', &volume, 2 },
205
- NULL, "Emit louder than usual output", ADOPT_USAGE_CHOICE },
206
-
207
- /* Set the `value_name` and specify that the value is required.
208
- * This will be rendered in help output as `-c <channel>`;
209
- * if it was not required, it would be rendered as `-c [<channel>]`.
210
- */
211
- { ADOPT_VALUE, "channel", 'c', &channel, NULL,
212
- "channel", "Set the channel number", ADOPT_USAGE_VALUE_REQUIRED },
213
-
214
- { ADOPT_LITERAL },
215
-
216
- /* `filename` is required. It will be rendered in help output as
217
- * `<file1>`.
218
- { ADOPT_ARG, NULL, 0, &filename1, NULL,
219
- "file1", "The first filename", ADOPT_USAGE_REQUIRED },
220
-
221
- /* `filename` is not required. It will be rendered in help output
222
- * as `[<file2>]`.
223
- { ADOPT_ARG, NULL, 0, &filename2, NULL,
224
- "file2", "The second (optional) filename", 0 },
225
-
226
- /* End of the specification list. */
227
- { 0 },
228
- };
196
+ ``` c
197
+ adopt_spec opt_specs[] = {
198
+ /* For bools and switches, you probably only want to set the help.
199
+ * There is no value to describe, and these are unlikely to be
200
+ * required.
201
+ */
202
+ { ADOPT_BOOL, "verbose", 'v', &verbose,
203
+ NULL, "Turn on verbose mode", 0 },
204
+
205
+ /* Specifying that an option is an `ADOPT_USAGE_CHOICE` indicates
206
+ * that it is orthogonal to the previous entry. These two options
207
+ * will be rendered together in help output as `[-q|-l]`.
208
+ */
209
+ { ADOPT_SWITCH, "quiet", 'q', &volume, 0,
210
+ NULL, "Emit no output", 0 },
211
+ { ADOPT_SWITCH, "loud", 'l', &volume, 2 },
212
+ NULL, "Emit louder than usual output", ADOPT_USAGE_CHOICE },
213
+
214
+ /* Set the `value_name` and specify that the value is required.
215
+ * This will be rendered in help output as `-c <channel>`;
216
+ * if it was not required, it would be rendered as `-c [<channel>]`.
217
+ */
218
+ { ADOPT_VALUE, "channel", 'c', &channel, NULL,
219
+ "channel", "Set the channel number", ADOPT_USAGE_VALUE_REQUIRED },
220
+
221
+ { ADOPT_LITERAL },
222
+
223
+ /* `filename` is required. It will be rendered in help output as
224
+ * `<file1>`.
225
+ */
226
+ { ADOPT_ARG, NULL, 0, &filename1, NULL,
227
+ "file1", "The first filename", ADOPT_USAGE_REQUIRED },
228
+
229
+ /* `filename` is not required. It will be rendered in help output
230
+ * as `[<file2>]`.
231
+ */
232
+ { ADOPT_ARG, NULL, 0, &filename2, NULL,
233
+ "file2", "The second (optional) filename", 0 },
234
+
235
+ /* End of the specification list. */
236
+ { 0 },
237
+ };
238
+ ```
229
239
230
240
If you call ` adopt_usage_fprint ` with the above specifications, it will emit:
231
241
232
- usage: ./example [-v] [-q] [-l] [-c <channel>] -- <file1> [<file2>]
242
+ ```
243
+ usage: ./example [-v] [-q] [-l] [-c <channel>] -- <file1> [<file2>]
244
+ ```
233
245
234
246
Inclusion in your product
235
247
-------------------------
@@ -240,15 +252,19 @@ product without using the `adopt_` prefix, you can use the included
240
252
241
253
For example:
242
254
243
- % ./rename.pl ed_opt
255
+ ``` bash
256
+ % ./rename.pl ed_opt
257
+ ```
244
258
245
259
Will product ` ed_opt.c ` and ` ed_opt.h ` that can be included in an
246
260
application, with the variables renamed to ` ed_opt ` (instead of ` adopt ` )
247
261
and enum names renamed to ` ED_OPT ` (instead of ` ADOPT ` ).
248
262
249
263
Or simply:
250
264
251
- % ./rename.pl opt
265
+ ``` bash
266
+ % ./rename.pl opt
267
+ ```
252
268
253
269
To produce ` opt.c ` and ` opt.h ` , with variables prefixed with ` opt ` .
254
270
0 commit comments