argp-ex4.c 5.4 KB

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