argp-ex4.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. #if (defined(__GLIBC__) && !defined(__UCLIBC__)) || defined(__UCLIBC_HAS_ARGP__)
  26. #include <argp.h>
  27. const char *argp_program_version =
  28. "argp-ex4 1.0";
  29. const char *argp_program_bug_address =
  30. "<bug-gnu-utils@@prep.ai.mit.edu>";
  31. /* Program documentation. */
  32. static char doc[] =
  33. "Argp example #4 -- a program with somewhat more complicated\
  34. options\
  35. \vThis part of the documentation comes *after* the options;\
  36. note that the text is automatically filled, but it's possible\
  37. to force a line-break, e.g.\n<-- here.";
  38. /* A description of the arguments we accept. */
  39. static char args_doc[] = "ARG1 [STRING...]";
  40. /* Keys for options without short-options. */
  41. #define OPT_ABORT 1 /* --abort */
  42. /* The options we understand. */
  43. static struct argp_option options[] = {
  44. {"verbose", 'v', 0, 0, "Produce verbose output" },
  45. {"quiet", 'q', 0, 0, "Don't produce any output" },
  46. {"silent", 's', 0, OPTION_ALIAS },
  47. {"output", 'o', "FILE", 0,
  48. "Output to FILE instead of standard output" },
  49. {0,0,0,0, "The following options should be grouped together:" },
  50. {"repeat", 'r', "COUNT", OPTION_ARG_OPTIONAL,
  51. "Repeat the output COUNT (default 10) times"},
  52. {"abort", OPT_ABORT, 0, 0, "Abort before showing any output"},
  53. { 0 }
  54. };
  55. /* Used by @code{main} to communicate with @code{parse_opt}. */
  56. struct arguments
  57. {
  58. char *arg1; /* @var{arg1} */
  59. char **strings; /* [@var{string}@dots{}] */
  60. int silent, verbose, abort; /* @samp{-s}, @samp{-v}, @samp{--abort} */
  61. char *output_file; /* @var{file} arg to @samp{--output} */
  62. int repeat_count; /* @var{count} arg to @samp{--repeat} */
  63. };
  64. /* Parse a single option. */
  65. static error_t
  66. parse_opt (int key, char *arg, struct argp_state *state)
  67. {
  68. /* Get the @code{input} argument from @code{argp_parse}, which we
  69. know is a pointer to our arguments structure. */
  70. struct arguments *arguments = state->input;
  71. switch (key)
  72. {
  73. case 'q': case 's':
  74. arguments->silent = 1;
  75. break;
  76. case 'v':
  77. arguments->verbose = 1;
  78. break;
  79. case 'o':
  80. arguments->output_file = arg;
  81. break;
  82. case 'r':
  83. arguments->repeat_count = arg ? atoi (arg) : 10;
  84. break;
  85. case OPT_ABORT:
  86. arguments->abort = 1;
  87. break;
  88. case ARGP_KEY_NO_ARGS:
  89. argp_usage (state);
  90. case ARGP_KEY_ARG:
  91. /* Here we know that @code{state->arg_num == 0}, since we
  92. force argument parsing to end before any more arguments can
  93. get here. */
  94. arguments->arg1 = arg;
  95. /* Now we consume all the rest of the arguments.
  96. @code{state->next} is the index in @code{state->argv} of the
  97. next argument to be parsed, which is the first @var{string}
  98. we're interested in, so we can just use
  99. @code{&state->argv[state->next]} as the value for
  100. arguments->strings.
  101. @emph{In addition}, by setting @code{state->next} to the end
  102. of the arguments, we can force argp to stop parsing here and
  103. return. */
  104. arguments->strings = &state->argv[state->next];
  105. state->next = state->argc;
  106. break;
  107. default:
  108. return ARGP_ERR_UNKNOWN;
  109. }
  110. return 0;
  111. }
  112. /* Our argp parser. */
  113. static struct argp argp = { options, parse_opt, args_doc, doc };
  114. #endif
  115. int main (int argc, char **argv)
  116. {
  117. #if (defined(__GLIBC__) && !defined(__UCLIBC__)) || defined(__UCLIBC_HAS_ARGP__)
  118. int i, j;
  119. struct arguments arguments;
  120. /* Default values. */
  121. arguments.silent = 0;
  122. arguments.verbose = 0;
  123. arguments.output_file = "-";
  124. arguments.repeat_count = 1;
  125. arguments.abort = 0;
  126. /* Parse our arguments; every option seen by @code{parse_opt} will be
  127. reflected in @code{arguments}. */
  128. argp_parse (&argp, argc, argv, 0, 0, &arguments);
  129. if (arguments.abort)
  130. error (10, 0, "ABORTED");
  131. for (i = 0; i < arguments.repeat_count; i++)
  132. {
  133. printf ("ARG1 = %s\n", arguments.arg1);
  134. printf ("STRINGS = ");
  135. for (j = 0; arguments.strings[j]; j++)
  136. printf (j == 0 ? "%s" : ", %s", arguments.strings[j]);
  137. printf ("\n");
  138. printf ("OUTPUT_FILE = %s\nVERBOSE = %s\nSILENT = %s\n",
  139. arguments.output_file,
  140. arguments.verbose ? "yes" : "no",
  141. arguments.silent ? "yes" : "no");
  142. }
  143. exit (0);
  144. #else
  145. return 23;
  146. #endif
  147. }