1
/* Argp example #4 -- a program with somewhat more complicated options */
3
/* This program uses the same features as example 3, but has more options,
4
and somewhat more structure in the -help output. It also shows how you
5
can `steal' the remainder of the input arguments past a certain point, for
6
programs that accept a list of items. It also shows the special argp KEY
7
value ARGP_KEY_NO_ARGS, which is only given if no non-option arguments
8
were supplied to the program.
10
For structuring the help output, two features are used, *headers* which
11
are entries in the options vector with the first four fields being zero,
12
and a two part documentation string (in the variable DOC), which allows
13
documentation both before and after the options; the two parts of DOC are
14
separated by a vertical-tab character ('\v', or '\013'). By convention,
15
the documentation before the options is just a short string saying what
16
the program does, and that afterwards is longer, describing the behavior
17
in more detail. All documentation strings are automatically filled for
18
output, although newlines may be included to force a line break at a
19
particular point. All documenation strings are also passed to the
20
`gettext' function, for possible translation into the current locale. */
23
/* #include <error.h> */
26
const char *argp_program_version = "argp-ex4 1.0";
27
const char *argp_program_bug_address = "<bug-gnu-utils@prep.ai.mit.edu>";
30
"Argp example #4 -- a program with somewhat more complicated options\
31
\vThis part of the documentation comes *after* the options; note that\
32
it is automatically filled, but it's possible to force a line-break,\
34
static char args_doc[] = "ARG1 [STRING...]";
36
/* Keys for options without short-options. */
37
#define OPT_ABORT 1 /* --abort */
39
static struct argp_option options[] = {
40
{"verbose", 'v', 0, 0, "Produce verbose output" },
41
{"quiet", 'q', 0, 0, "Don't produce any output" },
42
{"silent", 's', 0, OPTION_ALIAS },
43
{"output", 'o', "FILE", 0, "Output to FILE instead of standard output" },
45
{0, 0, 0, 0, "The following options should be grouped together:" },
46
{"repeat", 'r', "COUNT", OPTION_ARG_OPTIONAL,
47
"Repeat the output COUNT (default 10) times"},
48
{"abort", OPT_ABORT, 0, 0, "Abort before showing any output"},
52
/* Used by main to communicate with parse_opt. */
55
char *arg1; /* ARG1 */
56
char **strings; /* [STRING...] */
57
int silent, verbose, abort; /* -s, -v, --abort */
58
char *output_file; /* --output=FILE */
59
int repeat_count; /* --repeat[=COUNT] */
63
parse_opt (int key, char *arg, struct argp_state *state)
65
/* Get the INPUT argument from argp_parse, which we know is a pointer to
66
our arguments structure. */
67
struct arguments *arguments = state->input;
72
arguments->silent = 1;
75
arguments->verbose = 1;
78
arguments->output_file = arg;
81
arguments->repeat_count = arg ? atoi (arg) : 10;
87
case ARGP_KEY_NO_ARGS:
91
/* Here we know that STATE->arg_num == 0, since we force argument
92
parsing to end before any more arguments can get here. */
93
arguments->arg1 = arg;
95
/* Now we consume all the rest of the arguments. STATE->next is the
96
index in STATE->argv of the next argument to be parsed, which is the
97
first STRING we're interested in, so we can just use
98
`&state->argv[state->next]' as the value for arguments->strings.
100
IN ADDITION, by setting STATE->next to the end of the arguments, we
101
can force argp to stop parsing here and return. */
102
arguments->strings = &state->argv[state->next];
103
state->next = state->argc;
108
return ARGP_ERR_UNKNOWN;
113
static struct argp argp = { options, parse_opt, args_doc, doc };
115
int main (int argc, char **argv)
118
struct arguments arguments;
120
/* Default values. */
121
arguments.silent = 0;
122
arguments.verbose = 0;
123
arguments.output_file = "-";
124
arguments.repeat_count = 1;
127
argp_parse (&argp, argc, argv, 0, 0, &arguments);
131
fprintf (stderr, "ABORTED");
135
for (i = 0; i < arguments.repeat_count; i++)
137
printf ("ARG1 = %s\n", arguments.arg1);
138
printf ("STRINGS = ");
139
for (j = 0; arguments.strings[j]; j++)
140
printf (j == 0 ? "%s" : ", %s", arguments.strings[j]);
142
printf ("OUTPUT_FILE = %s\nVERBOSE = %s\nSILENT = %s\n",
143
arguments.output_file,
144
arguments.verbose ? "yes" : "no",
145
arguments.silent ? "yes" : "no");