Back to index

glibc  2.9
argp-ex3.c
Go to the documentation of this file.
00001 /* Argp example #3 -- a program with options and arguments using argp */
00002 
00003 /* This program uses the same features as example 2, and uses options and
00004    arguments.
00005 
00006    We now use the first four fields in ARGP, so here's a description of them:
00007      OPTIONS  -- A pointer to a vector of struct argp_option (see below)
00008      PARSER   -- A function to parse a single option, called by argp
00009      ARGS_DOC -- A string describing how the non-option arguments should look
00010      DOC      -- A descriptive string about this program; if it contains a
00011                  vertical tab character (\v), the part after it will be
00012                  printed *following* the options
00013 
00014    The function PARSER takes the following arguments:
00015      KEY  -- An integer specifying which option this is (taken
00016              from the KEY field in each struct argp_option), or
00017              a special key specifying something else; the only
00018              special keys we use here are ARGP_KEY_ARG, meaning
00019              a non-option argument, and ARGP_KEY_END, meaning
00020              that all arguments have been parsed
00021      ARG  -- For an option KEY, the string value of its
00022              argument, or NULL if it has none
00023      STATE-- A pointer to a struct argp_state, containing
00024              various useful information about the parsing state; used here
00025              are the INPUT field, which reflects the INPUT argument to
00026              argp_parse, and the ARG_NUM field, which is the number of the
00027              current non-option argument being parsed
00028    It should return either 0, meaning success, ARGP_ERR_UNKNOWN, meaning the
00029    given KEY wasn't recognized, or an errno value indicating some other
00030    error.
00031 
00032    Note that in this example, main uses a structure to communicate with the
00033    parse_opt function, a pointer to which it passes in the INPUT argument to
00034    argp_parse.  Of course, it's also possible to use global variables
00035    instead, but this is somewhat more flexible.
00036 
00037    The OPTIONS field contains a pointer to a vector of struct argp_option's;
00038    that structure has the following fields (if you assign your option
00039    structures using array initialization like this example, unspecified
00040    fields will be defaulted to 0, and need not be specified):
00041      NAME   -- The name of this option's long option (may be zero)
00042      KEY    -- The KEY to pass to the PARSER function when parsing this option,
00043                *and* the name of this option's short option, if it is a
00044                printable ascii character
00045      ARG    -- The name of this option's argument, if any
00046      FLAGS  -- Flags describing this option; some of them are:
00047                  OPTION_ARG_OPTIONAL -- The argument to this option is optional
00048                  OPTION_ALIAS        -- This option is an alias for the
00049                                         previous option
00050                  OPTION_HIDDEN       -- Don't show this option in --help output
00051      DOC    -- A documentation string for this option, shown in --help output
00052 
00053    An options vector should be terminated by an option with all fields zero. */
00054 
00055 #include <argp.h>
00056 
00057 const char *argp_program_version =
00058   "argp-ex3 1.0";
00059 const char *argp_program_bug_address =
00060   "<bug-gnu-utils@@gnu.org>";
00061 
00062 /* Program documentation.  */
00063 static char doc[] =
00064   "Argp example #3 -- a program with options and arguments using argp";
00065 
00066 /* A description of the arguments we accept.  */
00067 static char args_doc[] = "ARG1 ARG2";
00068 
00069 /* The options we understand.  */
00070 static struct argp_option options[] = {
00071   {"verbose",  'v', 0,      0,  "Produce verbose output" },
00072   {"quiet",    'q', 0,      0,  "Don't produce any output" },
00073   {"silent",   's', 0,      OPTION_ALIAS },
00074   {"output",   'o', "FILE", 0,
00075    "Output to FILE instead of standard output" },
00076   { 0 }
00077 };
00078 
00079 /* Used by @code{main} to communicate with @code{parse_opt}.  */
00080 struct arguments
00081 {
00082   char *args[2];            /* @var{arg1} & @var{arg2} */
00083   int silent, verbose;
00084   char *output_file;
00085 };
00086 
00087 /* Parse a single option.  */
00088 static error_t
00089 parse_opt (int key, char *arg, struct argp_state *state)
00090 {
00091   /* Get the @var{input} argument from @code{argp_parse}, which we
00092      know is a pointer to our arguments structure.  */
00093   struct arguments *arguments = state->input;
00094 
00095   switch (key)
00096     {
00097     case 'q': case 's':
00098       arguments->silent = 1;
00099       break;
00100     case 'v':
00101       arguments->verbose = 1;
00102       break;
00103     case 'o':
00104       arguments->output_file = arg;
00105       break;
00106 
00107     case ARGP_KEY_ARG:
00108       if (state->arg_num >= 2)
00109        /* Too many arguments.  */
00110        argp_usage (state);
00111 
00112       arguments->args[state->arg_num] = arg;
00113 
00114       break;
00115 
00116     case ARGP_KEY_END:
00117       if (state->arg_num < 2)
00118        /* Not enough arguments.  */
00119        argp_usage (state);
00120       break;
00121 
00122     default:
00123       return ARGP_ERR_UNKNOWN;
00124     }
00125   return 0;
00126 }
00127 
00128 /* Our argp parser.  */
00129 static struct argp argp = { options, parse_opt, args_doc, doc };
00130 
00131 int main (int argc, char **argv)
00132 {
00133   struct arguments arguments;
00134 
00135   /* Default values.  */
00136   arguments.silent = 0;
00137   arguments.verbose = 0;
00138   arguments.output_file = "-";
00139 
00140   /* Parse our arguments; every option seen by @code{parse_opt} will
00141      be reflected in @code{arguments}.  */
00142   argp_parse (&argp, argc, argv, 0, 0, &arguments);
00143 
00144   printf ("ARG1 = %s\nARG2 = %s\nOUTPUT_FILE = %s\n"
00145          "VERBOSE = %s\nSILENT = %s\n",
00146          arguments.args[0], arguments.args[1],
00147          arguments.output_file,
00148          arguments.verbose ? "yes" : "no",
00149          arguments.silent ? "yes" : "no");
00150 
00151   exit (0);
00152 }