argp-ex3.c 6.0 KB

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