Blame argp/argp-test.c

Packit 6c4009
/* Test program for argp argument parser
Packit 6c4009
   Copyright (C) 1997-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
   Written by Miles Bader <miles@gnu.ai.mit.edu>.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#ifdef HAVE_CONFIG_H
Packit 6c4009
#include <config.h>
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include <time.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
#include <argp.h>
Packit 6c4009
Packit 6c4009
const char *argp_program_version = "argp-test 1.0";
Packit 6c4009

Packit 6c4009
struct argp_option sub_options[] =
Packit 6c4009
{
Packit 6c4009
  {"subopt1",       's',     0,  0, "Nested option 1"},
Packit 6c4009
  {"subopt2",       'S',     0,  0, "Nested option 2"},
Packit 6c4009
Packit 6c4009
  { 0, 0, 0, 0, "Some more nested options:", 10},
Packit 6c4009
  {"subopt3",       'p',     0,  0, "Nested option 3"},
Packit 6c4009
Packit 6c4009
  {"subopt4",       'q',     0,  0, "Nested option 4", 1},
Packit 6c4009
Packit 6c4009
  {0}
Packit 6c4009
};
Packit 6c4009
Packit 6c4009
static const char sub_args_doc[] = "STRING...\n-";
Packit 6c4009
static const char sub_doc[] = "\vThis is the doc string from the sub-arg-parser.";
Packit 6c4009
Packit 6c4009
static error_t
Packit 6c4009
sub_parse_opt (int key, char *arg, struct argp_state *state)
Packit 6c4009
{
Packit 6c4009
  switch (key)
Packit 6c4009
    {
Packit 6c4009
    case ARGP_KEY_NO_ARGS:
Packit 6c4009
      printf ("NO SUB ARGS\n");
Packit 6c4009
      break;
Packit 6c4009
    case ARGP_KEY_ARG:
Packit 6c4009
      printf ("SUB ARG: %s\n", arg);
Packit 6c4009
      break;
Packit 6c4009
Packit 6c4009
    case 's' : case 'S': case 'p': case 'q':
Packit 6c4009
      printf ("SUB KEY %c\n", key);
Packit 6c4009
      break;
Packit 6c4009
Packit 6c4009
    default:
Packit 6c4009
      return ARGP_ERR_UNKNOWN;
Packit 6c4009
    }
Packit 6c4009
  return 0;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static char *
Packit 6c4009
sub_help_filter (int key, const char *text, void *input)
Packit 6c4009
{
Packit 6c4009
  if (key == ARGP_KEY_HELP_EXTRA)
Packit 6c4009
    return strdup ("This is some extra text from the sub parser (note that it \
Packit 6c4009
is preceded by a blank line).");
Packit 6c4009
  else
Packit 6c4009
    return (char *)text;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static struct argp sub_argp = {
Packit 6c4009
  sub_options, sub_parse_opt, sub_args_doc, sub_doc, 0, sub_help_filter
Packit 6c4009
};
Packit 6c4009

Packit 6c4009
/* Structure used to communicate with the parsing functions.  */
Packit 6c4009
struct params
Packit 6c4009
{
Packit 6c4009
  unsigned foonly;		/* Value parsed for foonly.  */
Packit 6c4009
  unsigned foonly_default;	/* Default value for it.  */
Packit 6c4009
};
Packit 6c4009
Packit 6c4009
#define OPT_PGRP 1
Packit 6c4009
#define OPT_SESS 2
Packit 6c4009
Packit 6c4009
struct argp_option options[] =
Packit 6c4009
{
Packit 6c4009
  {"pid",       'p',     "PID", 0, "List the process PID"},
Packit 6c4009
  {"pgrp",      OPT_PGRP,"PGRP",0, "List processes in the process group PGRP"},
Packit 6c4009
  {"no-parent", 'P',	 0,     0, "Include processes without parents"},
Packit 6c4009
  {0,           'x',     0,     OPTION_ALIAS},
Packit 6c4009
  {"all-fields",'Q',     0,     0, "Don't elide unusable fields (normally"
Packit 6c4009
				   " if there's some reason ps can't"
Packit 6c4009
				   " print a field for any process, it's"
Packit 6c4009
				   " removed from the output entirely)" },
Packit 6c4009
  {"reverse",   'r',    0,      0, "Reverse the order of any sort"},
Packit 6c4009
  {"gratuitously-long-reverse-option", 0, 0, OPTION_ALIAS},
Packit 6c4009
  {"session",  OPT_SESS,"SID",  OPTION_ARG_OPTIONAL,
Packit 6c4009
				   "Add the processes from the session"
Packit 6c4009
				   " SID (which defaults to the sid of"
Packit 6c4009
				   " the current process)" },
Packit 6c4009
Packit 6c4009
  {0,0,0,0, "Here are some more options:"},
Packit 6c4009
  {"foonly", 'f', "ZOT", OPTION_ARG_OPTIONAL, "Glork a foonly"},
Packit 6c4009
  {"zaza", 'z', 0, 0, "Snit a zar"},
Packit 6c4009
Packit 6c4009
  {0}
Packit 6c4009
};
Packit 6c4009
Packit 6c4009
static const char args_doc[] = "STRING";
Packit 6c4009
static const char doc[] = "Test program for argp."
Packit 6c4009
 "\vThis doc string comes after the options."
Packit 6c4009
 "\nHey!  Some manual formatting!"
Packit 6c4009
 "\nThe current time is: %s";
Packit 6c4009
Packit 6c4009
static void
Packit 6c4009
popt (int key, char *arg)
Packit 6c4009
{
Packit 6c4009
  char buf[10];
Packit 6c4009
  if (isprint (key))
Packit 6c4009
    sprintf (buf, "%c", key);
Packit 6c4009
  else
Packit 6c4009
    sprintf (buf, "%d", key);
Packit 6c4009
  if (arg)
Packit 6c4009
    printf ("KEY %s: %s\n", buf, arg);
Packit 6c4009
  else
Packit 6c4009
    printf ("KEY %s\n", buf);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static error_t
Packit 6c4009
parse_opt (int key, char *arg, struct argp_state *state)
Packit 6c4009
{
Packit 6c4009
  struct params *params = state->input;
Packit 6c4009
Packit 6c4009
  switch (key)
Packit 6c4009
    {
Packit 6c4009
    case ARGP_KEY_NO_ARGS:
Packit 6c4009
      printf ("NO ARGS\n");
Packit 6c4009
      break;
Packit 6c4009
Packit 6c4009
    case ARGP_KEY_ARG:
Packit 6c4009
      if (state->arg_num > 0)
Packit 6c4009
	return ARGP_ERR_UNKNOWN; /* Leave it for the sub-arg parser.  */
Packit 6c4009
      printf ("ARG: %s\n", arg);
Packit 6c4009
      break;
Packit 6c4009
Packit 6c4009
    case 'f':
Packit 6c4009
      if (arg)
Packit 6c4009
	params->foonly = atoi (arg);
Packit 6c4009
      else
Packit 6c4009
	params->foonly = params->foonly_default;
Packit 6c4009
      popt (key, arg);
Packit 6c4009
      break;
Packit 6c4009
Packit 6c4009
    case 'p': case 'P': case OPT_PGRP: case 'x': case 'Q':
Packit 6c4009
    case 'r': case OPT_SESS: case 'z':
Packit 6c4009
      popt (key, arg);
Packit 6c4009
      break;
Packit 6c4009
Packit 6c4009
    default:
Packit 6c4009
      return ARGP_ERR_UNKNOWN;
Packit 6c4009
    }
Packit 6c4009
  return 0;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static char *
Packit 6c4009
help_filter (int key, const char *text, void *input)
Packit 6c4009
{
Packit 6c4009
  char *new_text;
Packit 6c4009
  struct params *params = input;
Packit 6c4009
Packit 6c4009
  if (key == ARGP_KEY_HELP_POST_DOC && text)
Packit 6c4009
    {
Packit 6c4009
      time_t now = time (0);
Packit 6c4009
      asprintf (&new_text, text, ctime (&now));
Packit 6c4009
    }
Packit 6c4009
  else if (key == 'f')
Packit 6c4009
    /* Show the default for the --foonly option.  */
Packit 6c4009
    asprintf (&new_text, "%s (ZOT defaults to %x)",
Packit 6c4009
	      text, params->foonly_default);
Packit 6c4009
  else
Packit 6c4009
    new_text = (char *)text;
Packit 6c4009
Packit 6c4009
  return new_text;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static struct argp_child argp_children[] = { { &sub_argp }, { 0 } };
Packit 6c4009
static struct argp argp = {
Packit 6c4009
  options, parse_opt, args_doc, doc, argp_children, help_filter
Packit 6c4009
};
Packit 6c4009

Packit 6c4009
int
Packit 6c4009
main (int argc, char **argv)
Packit 6c4009
{
Packit 6c4009
  struct params params;
Packit 6c4009
  params.foonly = 0;
Packit 6c4009
  params.foonly_default = random ();
Packit 6c4009
  argp_parse (&argp, argc, argv, 0, 0, &params);
Packit 6c4009
  printf ("After parsing: foonly = %x\n", params.foonly);
Packit 6c4009
  return 0;
Packit 6c4009
}