Update.
[glibc.git] / manual / examples / argp-ex4.c
blob24dd417a81439fdabb6ea0a93ec5aa837d31f0a2
1 /* Argp example #4 -- a program with somewhat more complicated options */
3 #include <stdlib.h>
4 #include <error.h>
5 #include <argp.h>
7 const char *argp_program_version =
8 "argp-ex4 1.0";
9 const char *argp_program_bug_address =
10 "<bug-gnu-utils@@prep.ai.mit.edu>";
12 /* Program documentation. */
13 static char doc[] =
14 "Argp example #4 -- a program with somewhat more complicated\
15 options\
16 \vThis part of the documentation comes *after* the options;\
17 note that the text is automatically filled, but it's possible\
18 to force a line-break, e.g.\n<-- here.";
20 /* A description of the arguments we accept. */
21 static char args_doc[] = "ARG1 [STRING...]";
23 /* Keys for options without short-options. */
24 #define OPT_ABORT 1 /* --abort */
26 /* The options we understand. */
27 static struct argp_option options[] = {
28 {"verbose", 'v', 0, 0, "Produce verbose output" },
29 {"quiet", 'q', 0, 0, "Don't produce any output" },
30 {"silent", 's', 0, OPTION_ALIAS },
31 {"output", 'o', "FILE", 0,
32 "Output to FILE instead of standard output" },
34 {0,0,0,0, "The following options should be grouped together:" },
35 {"repeat", 'r', "COUNT", OPTION_ARG_OPTIONAL,
36 "Repeat the output COUNT (default 10) times"},
37 {"abort", OPT_ABORT, 0, 0, "Abort before showing any output"},
39 { 0 }
42 /* Used by @code{main} to communicate with @code{parse_opt}. */
43 struct arguments
45 char *arg1; /* @var{arg1} */
46 char **strings; /* [@var{string}@dots{}] */
47 int silent, verbose, abort; /* @samp{-s}, @samp{-v}, @samp{--abort} */
48 char *output_file; /* @var{file} arg to @samp{--output} */
49 int repeat_count; /* @var{count} arg to @samp{--repeat} */
52 /* Parse a single option. */
53 static error_t
54 parse_opt (int key, char *arg, struct argp_state *state)
56 /* Get the @code{input} argument from @code{argp_parse}, which we
57 know is a pointer to our arguments structure. */
58 struct arguments *arguments = state->input;
60 switch (key)
62 case 'q': case 's':
63 arguments->silent = 1;
64 break;
65 case 'v':
66 arguments->verbose = 1;
67 break;
68 case 'o':
69 arguments->output_file = arg;
70 break;
71 case 'r':
72 arguments->repeat_count = arg ? atoi (arg) : 10;
73 break;
74 case OPT_ABORT:
75 arguments->abort = 1;
76 break;
78 case ARGP_KEY_NO_ARGS:
79 argp_usage (state);
81 case ARGP_KEY_ARG:
82 /* Here we know that @code{state->arg_num == 0}, since we
83 force argument parsing to end before any more arguments can
84 get here. */
85 arguments->arg1 = arg;
87 /* Now we consume all the rest of the arguments.
88 @code{state->next} is the index in @code{state->argv} of the
89 next argument to be parsed, which is the first @var{string}
90 we're interested in, so we can just use
91 @code{&state->argv[state->next]} as the value for
92 arguments->strings.
94 @emph{In addition}, by setting @code{state->next} to the end
95 of the arguments, we can force argp to stop parsing here and
96 return. */
97 arguments->strings = &state->argv[state->next];
98 state->next = state->argc;
100 break;
102 default:
103 return ARGP_ERR_UNKNOWN;
105 return 0;
108 /* Our argp parser. */
109 static struct argp argp = { options, parse_opt, args_doc, doc };
111 int main (int argc, char **argv)
113 int i, j;
114 struct arguments arguments;
116 /* Default values. */
117 arguments.silent = 0;
118 arguments.verbose = 0;
119 arguments.output_file = "-";
120 arguments.repeat_count = 1;
121 arguments.abort = 0;
123 /* Parse our arguments; every option seen by @code{parse_opt} will be
124 reflected in @code{arguments}. */
125 argp_parse (&argp, argc, argv, 0, 0, &arguments);
127 if (arguments.abort)
128 error (10, 0, "ABORTED");
130 for (i = 0; i < arguments.repeat_count; i++)
132 printf ("ARG1 = %s\n", arguments.arg1);
133 printf ("STRINGS = ");
134 for (j = 0; arguments.strings[j]; j++)
135 printf (j == 0 ? "%s" : ", %s", arguments.strings[j]);
136 printf ("\n");
137 printf ("OUTPUT_FILE = %s\nVERBOSE = %s\nSILENT = %s\n",
138 arguments.output_file,
139 arguments.verbose ? "yes" : "no",
140 arguments.silent ? "yes" : "no");
143 exit (0);