argp-ex3.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. #if (defined(__GLIBC__) && !defined(__UCLIBC__)) || defined(__UCLIBC_HAS_ARGP__)
  50. #include <argp.h>
  51. const char *argp_program_version =
  52. "argp-ex3 1.0";
  53. const char *argp_program_bug_address =
  54. "<bug-gnu-utils@@gnu.org>";
  55. /* Program documentation. */
  56. static char doc[] =
  57. "Argp example #3 -- a program with options and arguments using argp";
  58. /* A description of the arguments we accept. */
  59. static char args_doc[] = "ARG1 ARG2";
  60. /* The options we understand. */
  61. static struct argp_option options[] = {
  62. {"verbose", 'v', 0, 0, "Produce verbose output" },
  63. {"quiet", 'q', 0, 0, "Don't produce any output" },
  64. {"silent", 's', 0, OPTION_ALIAS },
  65. {"output", 'o', "FILE", 0,
  66. "Output to FILE instead of standard output" },
  67. { 0 }
  68. };
  69. /* Used by @code{main} to communicate with @code{parse_opt}. */
  70. struct arguments
  71. {
  72. char *args[2]; /* @var{arg1} & @var{arg2} */
  73. int silent, verbose;
  74. char *output_file;
  75. };
  76. /* Parse a single option. */
  77. static error_t
  78. parse_opt (int key, char *arg, struct argp_state *state)
  79. {
  80. /* Get the @var{input} argument from @code{argp_parse}, which we
  81. know is a pointer to our arguments structure. */
  82. struct arguments *arguments = state->input;
  83. switch (key)
  84. {
  85. case 'q': case 's':
  86. arguments->silent = 1;
  87. break;
  88. case 'v':
  89. arguments->verbose = 1;
  90. break;
  91. case 'o':
  92. arguments->output_file = arg;
  93. break;
  94. case ARGP_KEY_ARG:
  95. if (state->arg_num >= 2)
  96. /* Too many arguments. */
  97. argp_usage (state);
  98. arguments->args[state->arg_num] = arg;
  99. break;
  100. case ARGP_KEY_END:
  101. if (state->arg_num < 2)
  102. /* Not enough arguments. */
  103. argp_usage (state);
  104. break;
  105. default:
  106. return ARGP_ERR_UNKNOWN;
  107. }
  108. return 0;
  109. }
  110. /* Our argp parser. */
  111. static struct argp argp = { options, parse_opt, args_doc, doc };
  112. #endif
  113. int main (int argc, char **argv)
  114. {
  115. #if (defined(__GLIBC__) && !defined(__UCLIBC__)) || defined(__UCLIBC_HAS_ARGP__)
  116. struct arguments arguments;
  117. /* Default values. */
  118. arguments.silent = 0;
  119. arguments.verbose = 0;
  120. arguments.output_file = "-";
  121. /* Parse our arguments; every option seen by @code{parse_opt} will
  122. be reflected in @code{arguments}. */
  123. argp_parse (&argp, argc, argv, 0, 0, &arguments);
  124. printf ("ARG1 = %s\nARG2 = %s\nOUTPUT_FILE = %s\n"
  125. "VERBOSE = %s\nSILENT = %s\n",
  126. arguments.args[0], arguments.args[1],
  127. arguments.output_file,
  128. arguments.verbose ? "yes" : "no",
  129. arguments.silent ? "yes" : "no");
  130. exit (0);
  131. #else
  132. return 23;
  133. #endif
  134. }