argp-test.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /* Test program for argp argument parser
  2. Copyright (C) 1997 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. Written by Miles Bader <miles at gnu.ai.mit.edu>.
  5. The GNU C Library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public
  7. License as published by the Free Software Foundation; either
  8. version 2.1 of the License, or (at your option) any later version.
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with the GNU C Library; see the file COPYING.LIB. If
  15. not, see <http://www.gnu.org/licenses/>.  */
  16. #ifdef HAVE_CONFIG_H
  17. #include <config.h>
  18. #endif
  19. #include <stdlib.h>
  20. #include <time.h>
  21. #include <string.h>
  22. #include <argp.h>
  23. const char *argp_program_version = "argp-test 1.0";
  24. struct argp_option sub_options[] =
  25. {
  26. {"subopt1", 's', 0, 0, "Nested option 1"},
  27. {"subopt2", 'S', 0, 0, "Nested option 2"},
  28. { 0, 0, 0, 0, "Some more nested options:", 10},
  29. {"subopt3", 'p', 0, 0, "Nested option 3"},
  30. {"subopt4", 'q', 0, 0, "Nested option 4", 1},
  31. {0}
  32. };
  33. static const char sub_args_doc[] = "STRING...\n-";
  34. static const char sub_doc[] = "\vThis is the doc string from the sub-arg-parser.";
  35. static error_t
  36. sub_parse_opt (int key, char *arg, struct argp_state *state)
  37. {
  38. switch (key)
  39. {
  40. case ARGP_KEY_NO_ARGS:
  41. printf ("NO SUB ARGS\n");
  42. break;
  43. case ARGP_KEY_ARG:
  44. printf ("SUB ARG: %s\n", arg);
  45. break;
  46. case 's' : case 'S': case 'p': case 'q':
  47. printf ("SUB KEY %c\n", key);
  48. break;
  49. default:
  50. return ARGP_ERR_UNKNOWN;
  51. }
  52. return 0;
  53. }
  54. static char *
  55. sub_help_filter (int key, const char *text, void *input)
  56. {
  57. if (key == ARGP_KEY_HELP_EXTRA)
  58. return strdup ("This is some extra text from the sub parser (note that it \
  59. is preceded by a blank line).");
  60. else
  61. return (char *)text;
  62. }
  63. static struct argp sub_argp = {
  64. sub_options, sub_parse_opt, sub_args_doc, sub_doc, 0, sub_help_filter
  65. };
  66. /* Structure used to communicate with the parsing functions. */
  67. struct params
  68. {
  69. unsigned foonly; /* Value parsed for foonly. */
  70. unsigned foonly_default; /* Default value for it. */
  71. };
  72. #define OPT_PGRP 1
  73. #define OPT_SESS 2
  74. struct argp_option options[] =
  75. {
  76. {"pid", 'p', "PID", 0, "List the process PID"},
  77. {"pgrp", OPT_PGRP,"PGRP",0, "List processes in the process group PGRP"},
  78. {"no-parent", 'P', 0, 0, "Include processes without parents"},
  79. {0, 'x', 0, OPTION_ALIAS},
  80. {"all-fields",'Q', 0, 0, "Don't elide unusable fields (normally"
  81. " if there's some reason ps can't"
  82. " print a field for any process, it's"
  83. " removed from the output entirely)" },
  84. {"reverse", 'r', 0, 0, "Reverse the order of any sort"},
  85. {"gratuitously-long-reverse-option", 0, 0, OPTION_ALIAS},
  86. {"session", OPT_SESS,"SID", OPTION_ARG_OPTIONAL,
  87. "Add the processes from the session"
  88. " SID (which defaults to the sid of"
  89. " the current process)" },
  90. {0,0,0,0, "Here are some more options:"},
  91. {"foonly", 'f', "ZOT", OPTION_ARG_OPTIONAL, "Glork a foonly"},
  92. {"zaza", 'z', 0, 0, "Snit a zar"},
  93. {0}
  94. };
  95. static const char args_doc[] = "STRING";
  96. static const char doc[] = "Test program for argp."
  97. "\vThis doc string comes after the options."
  98. "\nHey! Some manual formatting!"
  99. "\nThe current time is: %s";
  100. static void
  101. popt (int key, char *arg)
  102. {
  103. char buf[10];
  104. if (isprint (key))
  105. sprintf (buf, "%c", key);
  106. else
  107. sprintf (buf, "%d", key);
  108. if (arg)
  109. printf ("KEY %s: %s\n", buf, arg);
  110. else
  111. printf ("KEY %s\n", buf);
  112. }
  113. static error_t
  114. parse_opt (int key, char *arg, struct argp_state *state)
  115. {
  116. struct params *params = state->input;
  117. switch (key)
  118. {
  119. case ARGP_KEY_NO_ARGS:
  120. printf ("NO ARGS\n");
  121. break;
  122. case ARGP_KEY_ARG:
  123. if (state->arg_num > 0)
  124. return ARGP_ERR_UNKNOWN; /* Leave it for the sub-arg parser. */
  125. printf ("ARG: %s\n", arg);
  126. break;
  127. case 'f':
  128. if (arg)
  129. params->foonly = atoi (arg);
  130. else
  131. params->foonly = params->foonly_default;
  132. popt (key, arg);
  133. break;
  134. case 'p': case 'P': case OPT_PGRP: case 'x': case 'Q':
  135. case 'r': case OPT_SESS: case 'z':
  136. popt (key, arg);
  137. break;
  138. default:
  139. return ARGP_ERR_UNKNOWN;
  140. }
  141. return 0;
  142. }
  143. static char *
  144. help_filter (int key, const char *text, void *input)
  145. {
  146. char *new_text;
  147. struct params *params = input;
  148. if (key == ARGP_KEY_HELP_POST_DOC && text)
  149. {
  150. time_t now = time (0);
  151. asprintf (&new_text, text, ctime (&now));
  152. }
  153. else if (key == 'f')
  154. /* Show the default for the --foonly option. */
  155. asprintf (&new_text, "%s (ZOT defaults to %x)",
  156. text, params->foonly_default);
  157. else
  158. new_text = (char *)text;
  159. return new_text;
  160. }
  161. static struct argp_child argp_children[] = { { &sub_argp }, { 0 } };
  162. static struct argp argp = {
  163. options, parse_opt, args_doc, doc, argp_children, help_filter
  164. };
  165. int
  166. main (int argc, char **argv)
  167. {
  168. struct params params;
  169. params.foonly = 0;
  170. params.foonly_default = random ();
  171. argp_parse (&argp, argc, argv, 0, 0, &params);
  172. printf ("After parsing: foonly = %x\n", params.foonly);
  173. return 0;
  174. }