Back to index

glibc  2.9
argp-test.c
Go to the documentation of this file.
00001 /* Test program for argp argument parser
00002    Copyright (C) 1997 Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004    Written by Miles Bader <miles@gnu.ai.mit.edu>.
00005 
00006    The GNU C Library is free software; you can redistribute it and/or
00007    modify it under the terms of the GNU Lesser General Public
00008    License as published by the Free Software Foundation; either
00009    version 2.1 of the License, or (at your option) any later version.
00010 
00011    The GNU C Library is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014    Lesser General Public License for more details.
00015 
00016    You should have received a copy of the GNU Lesser General Public
00017    License along with the GNU C Library; if not, write to the Free
00018    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00019    02111-1307 USA.  */
00020 
00021 #ifdef HAVE_CONFIG_H
00022 #include <config.h>
00023 #endif
00024 
00025 #include <stdlib.h>
00026 #include <time.h>
00027 #include <string.h>
00028 #include <argp.h>
00029 
00030 const char *argp_program_version = "argp-test 1.0";
00031 
00032 struct argp_option sub_options[] =
00033 {
00034   {"subopt1",       's',     0,  0, "Nested option 1"},
00035   {"subopt2",       'S',     0,  0, "Nested option 2"},
00036 
00037   { 0, 0, 0, 0, "Some more nested options:", 10},
00038   {"subopt3",       'p',     0,  0, "Nested option 3"},
00039 
00040   {"subopt4",       'q',     0,  0, "Nested option 4", 1},
00041 
00042   {0}
00043 };
00044 
00045 static const char sub_args_doc[] = "STRING...\n-";
00046 static const char sub_doc[] = "\vThis is the doc string from the sub-arg-parser.";
00047 
00048 static error_t
00049 sub_parse_opt (int key, char *arg, struct argp_state *state)
00050 {
00051   switch (key)
00052     {
00053     case ARGP_KEY_NO_ARGS:
00054       printf ("NO SUB ARGS\n");
00055       break;
00056     case ARGP_KEY_ARG:
00057       printf ("SUB ARG: %s\n", arg);
00058       break;
00059 
00060     case 's' : case 'S': case 'p': case 'q':
00061       printf ("SUB KEY %c\n", key);
00062       break;
00063 
00064     default:
00065       return ARGP_ERR_UNKNOWN;
00066     }
00067   return 0;
00068 }
00069 
00070 static char *
00071 sub_help_filter (int key, const char *text, void *input)
00072 {
00073   if (key == ARGP_KEY_HELP_EXTRA)
00074     return strdup ("This is some extra text from the sub parser (note that it \
00075 is preceded by a blank line).");
00076   else
00077     return (char *)text;
00078 }
00079 
00080 static struct argp sub_argp = {
00081   sub_options, sub_parse_opt, sub_args_doc, sub_doc, 0, sub_help_filter
00082 };
00083 
00084 /* Structure used to communicate with the parsing functions.  */
00085 struct params
00086 {
00087   unsigned foonly;          /* Value parsed for foonly.  */
00088   unsigned foonly_default;  /* Default value for it.  */
00089 };
00090 
00091 #define OPT_PGRP 1
00092 #define OPT_SESS 2
00093 
00094 struct argp_option options[] =
00095 {
00096   {"pid",       'p',     "PID", 0, "List the process PID"},
00097   {"pgrp",      OPT_PGRP,"PGRP",0, "List processes in the process group PGRP"},
00098   {"no-parent", 'P',  0,     0, "Include processes without parents"},
00099   {0,           'x',     0,     OPTION_ALIAS},
00100   {"all-fields",'Q',     0,     0, "Don't elide unusable fields (normally"
00101                                " if there's some reason ps can't"
00102                                " print a field for any process, it's"
00103                                " removed from the output entirely)" },
00104   {"reverse",   'r',    0,      0, "Reverse the order of any sort"},
00105   {"gratuitously-long-reverse-option", 0, 0, OPTION_ALIAS},
00106   {"session",  OPT_SESS,"SID",  OPTION_ARG_OPTIONAL,
00107                                "Add the processes from the session"
00108                                " SID (which defaults to the sid of"
00109                                " the current process)" },
00110 
00111   {0,0,0,0, "Here are some more options:"},
00112   {"foonly", 'f', "ZOT", OPTION_ARG_OPTIONAL, "Glork a foonly"},
00113   {"zaza", 'z', 0, 0, "Snit a zar"},
00114 
00115   {0}
00116 };
00117 
00118 static const char args_doc[] = "STRING";
00119 static const char doc[] = "Test program for argp."
00120  "\vThis doc string comes after the options."
00121  "\nHey!  Some manual formatting!"
00122  "\nThe current time is: %s";
00123 
00124 static void
00125 popt (int key, char *arg)
00126 {
00127   char buf[10];
00128   if (isprint (key))
00129     sprintf (buf, "%c", key);
00130   else
00131     sprintf (buf, "%d", key);
00132   if (arg)
00133     printf ("KEY %s: %s\n", buf, arg);
00134   else
00135     printf ("KEY %s\n", buf);
00136 }
00137 
00138 static error_t
00139 parse_opt (int key, char *arg, struct argp_state *state)
00140 {
00141   struct params *params = state->input;
00142 
00143   switch (key)
00144     {
00145     case ARGP_KEY_NO_ARGS:
00146       printf ("NO ARGS\n");
00147       break;
00148 
00149     case ARGP_KEY_ARG:
00150       if (state->arg_num > 0)
00151        return ARGP_ERR_UNKNOWN; /* Leave it for the sub-arg parser.  */
00152       printf ("ARG: %s\n", arg);
00153       break;
00154 
00155     case 'f':
00156       if (arg)
00157        params->foonly = atoi (arg);
00158       else
00159        params->foonly = params->foonly_default;
00160       popt (key, arg);
00161       break;
00162 
00163     case 'p': case 'P': case OPT_PGRP: case 'x': case 'Q':
00164     case 'r': case OPT_SESS: case 'z':
00165       popt (key, arg);
00166       break;
00167 
00168     default:
00169       return ARGP_ERR_UNKNOWN;
00170     }
00171   return 0;
00172 }
00173 
00174 static char *
00175 help_filter (int key, const char *text, void *input)
00176 {
00177   char *new_text;
00178   struct params *params = input;
00179 
00180   if (key == ARGP_KEY_HELP_POST_DOC && text)
00181     {
00182       time_t now = time (0);
00183       asprintf (&new_text, text, ctime (&now));
00184     }
00185   else if (key == 'f')
00186     /* Show the default for the --foonly option.  */
00187     asprintf (&new_text, "%s (ZOT defaults to %x)",
00188              text, params->foonly_default);
00189   else
00190     new_text = (char *)text;
00191 
00192   return new_text;
00193 }
00194 
00195 static struct argp_child argp_children[] = { { &sub_argp }, { 0 } };
00196 static struct argp argp = {
00197   options, parse_opt, args_doc, doc, argp_children, help_filter
00198 };
00199 
00200 int
00201 main (int argc, char **argv)
00202 {
00203   struct params params;
00204   params.foonly = 0;
00205   params.foonly_default = random ();
00206   argp_parse (&argp, argc, argv, 0, 0, &params);
00207   printf ("After parsing: foonly = %x\n", params.foonly);
00208   return 0;
00209 }