Update to 2.1.x development version
[glibc.git] / argp / argp-test.c
blobae72b2230c56c9af085109428d13af4d554be41c
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@gnu.ai.mit.edu>.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Library General Public License as
8 published by the Free Software Foundation; either version 2 of the
9 License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Library General Public License for more details.
16 You should have received a copy of the GNU Library General Public
17 License along with the GNU C Library; see the file COPYING.LIB. If not,
18 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
21 #ifdef HAVE_CONFIG_H
22 #include <config.h>
23 #endif
25 #include <argp.h>
27 char *argp_program_version = "argp-test 1.0";
29 struct argp_option sub_options[] =
31 {"subopt1", 's', 0, 0, "Nested option 1"},
32 {"subopt2", 'S', 0, 0, "Nested option 2"},
34 { 0, 0, 0, 0, "Some more nested options:", 10},
35 {"subopt3", 'p', 0, 0, "Nested option 3"},
37 {"subopt4", 'q', 0, 0, "Nested option 4", 1},
39 {0}
42 static const char sub_args_doc[] = "STRING...\n-";
43 static const char sub_doc[] = "\vThis is the doc string from the sub-arg-parser.";
45 static error_t
46 sub_parse_opt (int key, char *arg, struct argp_state *state)
48 switch (key)
50 case ARGP_KEY_NO_ARGS:
51 printf ("NO SUB ARGS\n");
52 break;
53 case ARGP_KEY_ARG:
54 printf ("SUB ARG: %s\n", arg);
55 break;
57 case 's' : case 'S': case 'p': case 'q':
58 printf ("SUB KEY %c\n", key);
59 break;
61 default:
62 return ARGP_ERR_UNKNOWN;
64 return 0;
67 static struct argp sub_argp = {
68 sub_options, sub_parse_opt, sub_args_doc, sub_doc
71 #define OPT_PGRP 1
72 #define OPT_SESS 2
74 struct argp_option options[] =
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)" },
91 {0,0,0,0, "Here are some more options:"},
92 {"foonly", 'f', "ZOT", 0, "Glork a foonly"},
93 {"zaza", 'z', 0, 0, "Snit a zar"},
95 {0}
98 static const char args_doc[] = "STRING";
99 static const char doc[] = "Test program for argp."
100 "\vThis doc string comes after the options."
101 "\nHey! Some manual formatting!";
103 static error_t
104 parse_opt (int key, char *arg, struct argp_state *state)
106 switch (key)
108 case ARGP_KEY_NO_ARGS:
109 printf ("NO ARGS\n");
110 break;
112 case ARGP_KEY_ARG:
113 if (state->arg_num > 0)
114 return ARGP_ERR_UNKNOWN; /* Leave it for the sub-arg parser. */
115 printf ("ARG: %s\n", arg);
116 break;
118 case 'p': case 'P': case OPT_PGRP: case 'x': case 'Q':
119 case 'r': case OPT_SESS: case 'f': case 'z':
121 char buf[10];
122 if (isprint (key))
123 sprintf (buf, "%c", key);
124 else
125 sprintf (buf, "%d", key);
126 if (arg)
127 printf ("KEY %s: %s\n", buf, arg);
128 else
129 printf ("KEY %s\n", buf);
131 break;
133 default:
134 return ARGP_ERR_UNKNOWN;
136 return 0;
139 static struct argp_child argp_children[] = { { &sub_argp }, { 0 } };
140 static struct argp argp = { options, parse_opt, args_doc, doc, argp_children };
143 main (int argc, char **argv)
145 argp_parse (&argp, argc, argv, 0, 0, 0);
146 return 0;