WvStreams
ex3.c
1/* Argp example #3 - a program with options and arguments using argp */
2
3/* This program uses the same features as example 2, and uses options and
4 arguments.
5
6 We now use the first four fields in ARGP, so here's a description of them:
7 OPTIONS - A pointer to a vector of struct argp_option (see below)
8 PARSER - A function to parse a single option, called by argp
9 ARGS_DOC - A string describing how the non-option arguments should look
10 DOC - A descriptive string about this program; if it contains a
11 vertical tab character (\v), the part after it will be
12 printed *following* the options
13
14 The function PARSER takes the following arguments:
15 KEY - An integer specifying which option this is (taken
16 from the KEY field in each struct argp_option), or
17 a special key specifying something else; the only
18 special keys we use here are ARGP_KEY_ARG, meaning
19 a non-option argument, and ARGP_KEY_END, meaning
20 that all arguments have been parsed
21 ARG - For an option KEY, the string value of its
22 argument, or NULL if it has none
23 STATE- A pointer to a struct argp_state, containing
24 various useful information about the parsing state; used here
25 are the INPUT field, which reflects the INPUT argument to
26 argp_parse, and the ARG_NUM field, which is the number of the
27 current non-option argument being parsed
28 It should return either 0, meaning success, ARGP_ERR_UNKNOWN, meaning the
29 given KEY wasn't recognized, or an errno value indicating some other
30 error.
31
32 Note that in this example, main uses a structure to communicate with the
33 parse_opt function, a pointer to which it passes in the INPUT argument to
34 argp_parse. Of course, it's also possible to use global variables
35 instead, but this is somewhat more flexible.
36
37 The OPTIONS field contains a pointer to a vector of struct argp_option's;
38 that structure has the following fields (if you assign your option
39 structures using array initialization like this example, unspecified
40 fields will be defaulted to 0, and need not be specified):
41 NAME - The name of this option's long option (may be zero)
42 KEY - The KEY to pass to the PARSER function when parsing this option,
43 *and* the name of this option's short option, if it is a
44 printable ascii character
45 ARG - The name of this option's argument, if any
46 FLAGS - Flags describing this option; some of them are:
47 OPTION_ARG_OPTIONAL - The argument to this option is optional
48 OPTION_ALIAS - This option is an alias for the
49 previous option
50 OPTION_HIDDEN - Don't show this option in -help output
51 DOC - A documentation string for this option, shown in -help output
52
53 An options vector should be terminated by an option with all fields zero. */
54
55#include <argp.h>
56
57#include <stdlib.h>
58
59const char *argp_program_version =
60 "argp-ex3 1.0";
61const char *argp_program_bug_address =
62 "<bug-gnu-utils@gnu.org>";
63
64/* Program documentation. */
65static char doc[] =
66 "Argp example #3 -- a program with options and arguments using argp";
67
68/* A description of the arguments we accept. */
69static char args_doc[] = "ARG1 ARG2";
70
71/* The options we understand. */
72static struct argp_option options[] = {
73 {"verbose", 'v', 0, 0, "Produce verbose output", 0},
74 {"quiet", 'q', 0, 0, "Don't produce any output", 0},
75 {"silent", 's', 0, OPTION_ALIAS, 0, 0},
76 {"output", 'o', "FILE", 0,
77 "Output to FILE instead of standard output", 0},
78 {0, 0, 0, 0, 0, 0}
79};
80
81/* Used by `main' to communicate with `parse_opt'. */
83{
84 char *args[2]; /* ARG1 & ARG2 */
85 int silent, verbose;
86 char *output_file;
87};
88
89/* Parse a single option. */
90static error_t
91parse_opt (int key, char *arg, struct argp_state *state)
92{
93 /* Get the INPUT argument from `argp_parse', which we
94 know is a pointer to our arguments structure. */
95 struct arguments *arguments = state->input;
96
97 switch (key)
98 {
99 case 'q': case 's':
100 arguments->silent = 1;
101 break;
102 case 'v':
103 arguments->verbose = 1;
104 break;
105 case 'o':
106 arguments->output_file = arg;
107 break;
108
109 case ARGP_KEY_ARG:
110 if (state->arg_num >= 2)
111 /* Too many arguments. */
112 argp_usage (state);
113
114 arguments->args[state->arg_num] = arg;
115
116 break;
117
118 case ARGP_KEY_END:
119 if (state->arg_num < 2)
120 /* Not enough arguments. */
121 argp_usage (state);
122 break;
123
124 default:
125 return ARGP_ERR_UNKNOWN;
126 }
127 return 0;
128}
129
130/* Our argp parser. */
131static struct argp argp = { options, parse_opt, args_doc, doc, 0, 0, 0 };
132
133int main (int argc, char **argv)
134{
135 struct arguments arguments;
136
137 /* Default values. */
138 arguments.silent = 0;
139 arguments.verbose = 0;
140 arguments.output_file = "-";
141
142 /* Parse our arguments; every option seen by `parse_opt' will
143 be reflected in `arguments'. */
144 argp_parse (&argp, argc, argv, 0, 0, &arguments);
145
146 printf ("ARG1 = %s\nARG2 = %s\nOUTPUT_FILE = %s\n"
147 "VERBOSE = %s\nSILENT = %s\n",
148 arguments.args[0], arguments.args[1],
149 arguments.output_file,
150 arguments.verbose ? "yes" : "no",
151 arguments.silent ? "yes" : "no");
152
153 exit (0);
154}
Definition: argp.h:213
Definition: ex3.c:83