argp-test.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. #if (defined(__GLIBC__) && !defined(__UCLIBC__)) || defined(__UCLIBC_HAS_ARGP__)
  23. #include <argp.h>
  24. const char *argp_program_version = "argp-test 1.0";
  25. struct argp_option sub_options[] =
  26. {
  27. {"subopt1", 's', 0, 0, "Nested option 1"},
  28. {"subopt2", 'S', 0, 0, "Nested option 2"},
  29. { 0, 0, 0, 0, "Some more nested options:", 10},
  30. {"subopt3", 'p', 0, 0, "Nested option 3"},
  31. {"subopt4", 'q', 0, 0, "Nested option 4", 1},
  32. {0}
  33. };
  34. static const char sub_args_doc[] = "STRING...\n-";
  35. static const char sub_doc[] = "\vThis is the doc string from the sub-arg-parser.";
  36. static error_t
  37. sub_parse_opt (int key, char *arg, struct argp_state *state)
  38. {
  39. switch (key)
  40. {
  41. case ARGP_KEY_NO_ARGS:
  42. printf ("NO SUB ARGS\n");
  43. break;
  44. case ARGP_KEY_ARG:
  45. printf ("SUB ARG: %s\n", arg);
  46. break;
  47. case 's' : case 'S': case 'p': case 'q':
  48. printf ("SUB KEY %c\n", key);
  49. break;
  50. default:
  51. return ARGP_ERR_UNKNOWN;
  52. }
  53. return 0;
  54. }
  55. static char *
  56. sub_help_filter (int key, const char *text, void *input)
  57. {
  58. if (key == ARGP_KEY_HELP_EXTRA)
  59. return strdup ("This is some extra text from the sub parser (note that it \
  60. is preceded by a blank line).");
  61. else
  62. return (char *)text;
  63. }
  64. static struct argp sub_argp = {
  65. sub_options, sub_parse_opt, sub_args_doc, sub_doc, 0, sub_help_filter
  66. };
  67. /* Structure used to communicate with the parsing functions. */
  68. struct params
  69. {
  70. unsigned foonly; /* Value parsed for foonly. */
  71. unsigned foonly_default; /* Default value for it. */
  72. };
  73. #define OPT_PGRP 1
  74. #define OPT_SESS 2
  75. struct argp_option options[] =
  76. {
  77. {"pid", 'p', "PID", 0, "List the process PID"},
  78. {"pgrp", OPT_PGRP,"PGRP",0, "List processes in the process group PGRP"},
  79. {"no-parent", 'P', 0, 0, "Include processes without parents"},
  80. {0, 'x', 0, OPTION_ALIAS},
  81. {"all-fields",'Q', 0, 0, "Don't elide unusable fields (normally"
  82. " if there's some reason ps can't"
  83. " print a field for any process, it's"
  84. " removed from the output entirely)" },
  85. {"reverse", 'r', 0, 0, "Reverse the order of any sort"},
  86. {"gratuitously-long-reverse-option", 0, 0, OPTION_ALIAS},
  87. {"session", OPT_SESS,"SID", OPTION_ARG_OPTIONAL,
  88. "Add the processes from the session"
  89. " SID (which defaults to the sid of"
  90. " the current process)" },
  91. {0,0,0,0, "Here are some more options:"},
  92. {"foonly", 'f', "ZOT", OPTION_ARG_OPTIONAL, "Glork a foonly"},
  93. {"zaza", 'z', 0, 0, "Snit a zar"},
  94. {0}
  95. };
  96. static const char args_doc[] = "STRING";
  97. static const char doc[] = "Test program for argp."
  98. "\vThis doc string comes after the options."
  99. "\nHey! Some manual formatting!"
  100. "\nThe current time is: %s";
  101. static void
  102. popt (int key, char *arg)
  103. {
  104. char buf[10];
  105. if (isprint (key))
  106. sprintf (buf, "%c", key);
  107. else
  108. sprintf (buf, "%d", key);
  109. if (arg)
  110. printf ("KEY %s: %s\n", buf, arg);
  111. else
  112. printf ("KEY %s\n", buf);
  113. }
  114. static error_t
  115. parse_opt (int key, char *arg, struct argp_state *state)
  116. {
  117. struct params *params = state->input;
  118. switch (key)
  119. {
  120. case ARGP_KEY_NO_ARGS:
  121. printf ("NO ARGS\n");
  122. break;
  123. case ARGP_KEY_ARG:
  124. if (state->arg_num > 0)
  125. return ARGP_ERR_UNKNOWN; /* Leave it for the sub-arg parser. */
  126. printf ("ARG: %s\n", arg);
  127. break;
  128. case 'f':
  129. if (arg)
  130. params->foonly = atoi (arg);
  131. else
  132. params->foonly = params->foonly_default;
  133. popt (key, arg);
  134. break;
  135. case 'p': case 'P': case OPT_PGRP: case 'x': case 'Q':
  136. case 'r': case OPT_SESS: case 'z':
  137. popt (key, arg);
  138. break;
  139. default:
  140. return ARGP_ERR_UNKNOWN;
  141. }
  142. return 0;
  143. }
  144. static char *
  145. help_filter (int key, const char *text, void *input)
  146. {
  147. char *new_text;
  148. struct params *params = input;
  149. if (key == ARGP_KEY_HELP_POST_DOC && text)
  150. {
  151. time_t now = time (0);
  152. asprintf (&new_text, text, ctime (&now));
  153. }
  154. else if (key == 'f')
  155. /* Show the default for the --foonly option. */
  156. asprintf (&new_text, "%s (ZOT defaults to %x)",
  157. text, params->foonly_default);
  158. else
  159. new_text = (char *)text;
  160. return new_text;
  161. }
  162. static struct argp_child argp_children[] = { { &sub_argp }, { 0 } };
  163. static struct argp argp = {
  164. options, parse_opt, args_doc, doc, argp_children, help_filter
  165. };
  166. #endif
  167. int
  168. main (int argc, char **argv)
  169. {
  170. #if (defined(__GLIBC__) && !defined(__UCLIBC__)) || defined(__UCLIBC_HAS_ARGP__)
  171. struct params params;
  172. params.foonly = 0;
  173. params.foonly_default = random ();
  174. argp_parse (&argp, argc, argv, 0, 0, &params);
  175. printf ("After parsing: foonly = %x\n", params.foonly);
  176. return 0;
  177. #else
  178. return 23;
  179. #endif
  180. }