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. */
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},
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.";
46 sub_parse_opt (int key
, char *arg
, struct argp_state
*state
)
50 case ARGP_KEY_NO_ARGS
:
51 printf ("NO SUB ARGS\n");
54 printf ("SUB ARG: %s\n", arg
);
57 case 's' : case 'S': case 'p': case 'q':
58 printf ("SUB KEY %c\n", key
);
62 return ARGP_ERR_UNKNOWN
;
67 static struct argp sub_argp
= {
68 sub_options
, sub_parse_opt
, sub_args_doc
, sub_doc
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"},
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!";
104 parse_opt (int key
, char *arg
, struct argp_state
*state
)
108 case ARGP_KEY_NO_ARGS
:
109 printf ("NO ARGS\n");
113 if (state
->arg_num
> 0)
114 return ARGP_ERR_UNKNOWN
; /* Leave it for the sub-arg parser. */
115 printf ("ARG: %s\n", arg
);
118 case 'p': case 'P': case OPT_PGRP
: case 'x': case 'Q':
119 case 'r': case OPT_SESS
: case 'f': case 'z':
123 sprintf (buf
, "%c", key
);
125 sprintf (buf
, "%d", key
);
127 printf ("KEY %s: %s\n", buf
, arg
);
129 printf ("KEY %s\n", buf
);
134 return ARGP_ERR_UNKNOWN
;
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);