argp-ex3.c 5.4 KB

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