summaryrefslogtreecommitdiff
path: root/libshouldbeinlibc/argp-test.c
blob: ae72b2230c56c9af085109428d13af4d554be41c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/* Test program for argp argument parser
   Copyright (C) 1997 Free Software Foundation, Inc.
   This file is part of the GNU C Library.
   Written by Miles Bader <miles@gnu.ai.mit.edu>.

   The GNU C Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public License as
   published by the Free Software Foundation; either version 2 of the
   License, or (at your option) any later version.

   The GNU C Library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public
   License along with the GNU C Library; see the file COPYING.LIB.  If not,
   write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
   Boston, MA 02111-1307, USA.  */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <argp.h>

char *argp_program_version = "argp-test 1.0";

struct argp_option sub_options[] =
{
  {"subopt1",       's',     0,  0, "Nested option 1"},
  {"subopt2",       'S',     0,  0, "Nested option 2"},

  { 0, 0, 0, 0, "Some more nested options:", 10},
  {"subopt3",       'p',     0,  0, "Nested option 3"},

  {"subopt4",       'q',     0,  0, "Nested option 4", 1},

  {0}
};

static const char sub_args_doc[] = "STRING...\n-";
static const char sub_doc[] = "\vThis is the doc string from the sub-arg-parser.";

static error_t
sub_parse_opt (int key, char *arg, struct argp_state *state)
{
  switch (key)
    {
    case ARGP_KEY_NO_ARGS:
      printf ("NO SUB ARGS\n");
      break;
    case ARGP_KEY_ARG:
      printf ("SUB ARG: %s\n", arg);
      break;

    case 's' : case 'S': case 'p': case 'q':
      printf ("SUB KEY %c\n", key);
      break;

    default:
      return ARGP_ERR_UNKNOWN;
    }
  return 0;
}

static struct argp sub_argp = {
  sub_options, sub_parse_opt, sub_args_doc, sub_doc
};

#define OPT_PGRP 1
#define OPT_SESS 2

struct argp_option options[] =
{
  {"pid",       'p',     "PID", 0, "List the process PID"},
  {"pgrp",      OPT_PGRP,"PGRP",0, "List processes in the process group PGRP"},
  {"no-parent", 'P',	 0,     0, "Include processes without parents"},
  {0,           'x',     0,     OPTION_ALIAS},
  {"all-fields",'Q',     0,     0, "Don't elide unusable fields (normally"
				   " if there's some reason ps can't"
				   " print a field for any process, it's"
				   " removed from the output entirely)" },
  {"reverse",   'r',    0,      0, "Reverse the order of any sort"},
  {"gratuitously-long-reverse-option", 0, 0, OPTION_ALIAS},
  {"session",  OPT_SESS,"SID",  OPTION_ARG_OPTIONAL,
				   "Add the processes from the session"
				   " SID (which defaults to the sid of"
				   " the current process)" },

  {0,0,0,0, "Here are some more options:"},
  {"foonly", 'f', "ZOT", 0, "Glork a foonly"},
  {"zaza", 'z', 0, 0, "Snit a zar"},

  {0}
};

static const char args_doc[] = "STRING";
static const char doc[] = "Test program for argp."
 "\vThis doc string comes after the options."
 "\nHey!  Some manual formatting!";

static error_t
parse_opt (int key, char *arg, struct argp_state *state)
{
  switch (key)
    {
    case ARGP_KEY_NO_ARGS:
      printf ("NO ARGS\n");
      break;

    case ARGP_KEY_ARG:
      if (state->arg_num > 0)
	return ARGP_ERR_UNKNOWN; /* Leave it for the sub-arg parser.  */
      printf ("ARG: %s\n", arg);
      break;

    case 'p': case 'P': case OPT_PGRP: case 'x': case 'Q':
    case 'r': case OPT_SESS: case 'f': case 'z':
      {
	char buf[10];
	if (isprint (key))
	  sprintf (buf, "%c", key);
	else
	  sprintf (buf, "%d", key);
	if (arg)
	  printf ("KEY %s: %s\n", buf, arg);
	else
	  printf ("KEY %s\n", buf);
      }
      break;

    default:
      return ARGP_ERR_UNKNOWN;
    }
  return 0;
}

static struct argp_child argp_children[] = { { &sub_argp }, { 0 } };
static struct argp argp = { options, parse_opt, args_doc, doc, argp_children };

int
main (int argc, char **argv)
{
  argp_parse (&argp, argc, argv, 0, 0, 0);
  return 0;
}