1 /* Argp example #4 -- a program with somewhat more complicated options */
7 const char *argp_program_version
=
9 const char *argp_program_bug_address
=
10 "<bug-gnu-utils@@prep.ai.mit.edu>";
12 /* Program documentation. */
14 "Argp example #4 -- a program with somewhat more complicated\
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"},
42 /* Used by @code{main} to communicate with @code{parse_opt}. */
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. */
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
;
63 arguments
->silent
= 1;
66 arguments
->verbose
= 1;
69 arguments
->output_file
= arg
;
72 arguments
->repeat_count
= arg
? atoi (arg
) : 10;
78 case ARGP_KEY_NO_ARGS
:
82 /* Here we know that @code{state->arg_num == 0}, since we
83 force argument parsing to end before any more arguments can
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
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
97 arguments
->strings
= &state
->argv
[state
->next
];
98 state
->next
= state
->argc
;
103 return ARGP_ERR_UNKNOWN
;
108 /* Our argp parser. */
109 static struct argp argp
= { options
, parse_opt
, args_doc
, doc
};
111 int main (int argc
, char **argv
)
114 struct arguments arguments
;
116 /* Default values. */
117 arguments
.silent
= 0;
118 arguments
.verbose
= 0;
119 arguments
.output_file
= "-";
120 arguments
.repeat_count
= 1;
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
);
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
]);
137 printf ("OUTPUT_FILE = %s\nVERBOSE = %s\nSILENT = %s\n",
138 arguments
.output_file
,
139 arguments
.verbose
? "yes" : "no",
140 arguments
.silent
? "yes" : "no");