argp-ex4.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /* Argp example #4 -- a program with somewhat more complicated options
  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 3, but has more
  15. options, and somewhat more structure in the -help output. It
  16. also shows how you can `steal' the remainder of the input
  17. arguments past a certain point, for programs that accept a
  18. list of items. It also shows the special argp KEY value
  19. ARGP_KEY_NO_ARGS, which is only given if no non-option
  20. arguments were supplied to the program.
  21. For structuring the help output, two features are used,
  22. *headers* which are entries in the options vector with the
  23. first four fields being zero, and a two part documentation
  24. string (in the variable DOC), which allows documentation both
  25. before and after the options; the two parts of DOC are
  26. separated by a vertical-tab character ('\v', or '\013'). By
  27. convention, the documentation before the options is just a
  28. short string saying what the program does, and that afterwards
  29. is longer, describing the behavior in more detail. All
  30. documentation strings are automatically filled for output,
  31. although newlines may be included to force a line break at a
  32. particular point. All documentation strings are also passed to
  33. the `gettext' function, for possible translation into the
  34. current locale. */
  35. #include <stdlib.h>
  36. #include <error.h>
  37. #include <argp.h>
  38. const char *argp_program_version =
  39. "argp-ex4 1.0";
  40. const char *argp_program_bug_address =
  41. "<bug-gnu-utils@@prep.ai.mit.edu>";
  42. /* Program documentation. */
  43. static char doc[] =
  44. "Argp example #4 -- a program with somewhat more complicated\
  45. options\
  46. \vThis part of the documentation comes *after* the options;\
  47. note that the text is automatically filled, but it's possible\
  48. to force a line-break, e.g.\n<-- here.";
  49. /* A description of the arguments we accept. */
  50. static char args_doc[] = "ARG1 [STRING...]";
  51. /* Keys for options without short-options. */
  52. #define OPT_ABORT 1 /* --abort */
  53. /* The options we understand. */
  54. static struct argp_option options[] = {
  55. {"verbose", 'v', 0, 0, "Produce verbose output" },
  56. {"quiet", 'q', 0, 0, "Don't produce any output" },
  57. {"silent", 's', 0, OPTION_ALIAS },
  58. {"output", 'o', "FILE", 0,
  59. "Output to FILE instead of standard output" },
  60. {0,0,0,0, "The following options should be grouped together:" },
  61. {"repeat", 'r', "COUNT", OPTION_ARG_OPTIONAL,
  62. "Repeat the output COUNT (default 10) times"},
  63. {"abort", OPT_ABORT, 0, 0, "Abort before showing any output"},
  64. { 0 }
  65. };
  66. /* Used by @code{main} to communicate with @code{parse_opt}. */
  67. struct arguments
  68. {
  69. char *arg1; /* @var{arg1} */
  70. char **strings; /* [@var{string}@dots{}] */
  71. int silent, verbose, abort; /* @samp{-s}, @samp{-v}, @samp{--abort} */
  72. char *output_file; /* @var{file} arg to @samp{--output} */
  73. int repeat_count; /* @var{count} arg to @samp{--repeat} */
  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 @code{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 'r':
  94. arguments->repeat_count = arg ? atoi (arg) : 10;
  95. break;
  96. case OPT_ABORT:
  97. arguments->abort = 1;
  98. break;
  99. case ARGP_KEY_NO_ARGS:
  100. argp_usage (state);
  101. case ARGP_KEY_ARG:
  102. /* Here we know that @code{state->arg_num == 0}, since we
  103. force argument parsing to end before any more arguments can
  104. get here. */
  105. arguments->arg1 = arg;
  106. /* Now we consume all the rest of the arguments.
  107. @code{state->next} is the index in @code{state->argv} of the
  108. next argument to be parsed, which is the first @var{string}
  109. we're interested in, so we can just use
  110. @code{&state->argv[state->next]} as the value for
  111. arguments->strings.
  112. @emph{In addition}, by setting @code{state->next} to the end
  113. of the arguments, we can force argp to stop parsing here and
  114. return. */
  115. arguments->strings = &state->argv[state->next];
  116. state->next = state->argc;
  117. break;
  118. default:
  119. return ARGP_ERR_UNKNOWN;
  120. }
  121. return 0;
  122. }
  123. /* Our argp parser. */
  124. static struct argp argp = { options, parse_opt, args_doc, doc };
  125. int
  126. main (int argc, char **argv)
  127. {
  128. int i, j;
  129. struct arguments arguments;
  130. /* Default values. */
  131. arguments.silent = 0;
  132. arguments.verbose = 0;
  133. arguments.output_file = "-";
  134. arguments.repeat_count = 1;
  135. arguments.abort = 0;
  136. /* Parse our arguments; every option seen by @code{parse_opt} will be
  137. reflected in @code{arguments}. */
  138. argp_parse (&argp, argc, argv, 0, 0, &arguments);
  139. if (arguments.abort)
  140. error (10, 0, "ABORTED");
  141. for (i = 0; i < arguments.repeat_count; i++)
  142. {
  143. printf ("ARG1 = %s\n", arguments.arg1);
  144. printf ("STRINGS = ");
  145. for (j = 0; arguments.strings[j]; j++)
  146. printf (j == 0 ? "%s" : ", %s", arguments.strings[j]);
  147. printf ("\n");
  148. printf ("OUTPUT_FILE = %s\nVERBOSE = %s\nSILENT = %s\n",
  149. arguments.output_file,
  150. arguments.verbose ? "yes" : "no",
  151. arguments.silent ? "yes" : "no");
  152. }
  153. exit (0);
  154. }