~ubuntu-branches/ubuntu/maverick/eglibc/maverick-security

« back to all changes in this revision

Viewing changes to manual/argp-ex4.c.texi

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2010-05-30 11:05:12 UTC
  • mfrom: (1.1.7 upstream)
  • Revision ID: james.westby@ubuntu.com-20100530110512-axurnekxr2yygcf2
Tags: 2.12-0ubuntu1
* Build eglibc_2.12.orig.tar.gz, based on 2.12 branch (r10591).
* Merge with Debian (r4299, trunk).

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* @r{Argp example #4 -- a program with somewhat more complicated options} */
2
 
 
3
 
/* @r{This program uses the same features as example 3, but has more
4
 
   options, and somewhat more structure in the -help output.  It
5
 
   also shows how you can `steal' the remainder of the input
6
 
   arguments past a certain point, for programs that accept a
7
 
   list of items.  It also shows the special argp KEY value
8
 
   ARGP_KEY_NO_ARGS, which is only given if no non-option
9
 
   arguments were supplied to the program.
10
 
 
11
 
   For structuring the help output, two features are used,
12
 
   *headers* which are entries in the options vector with the
13
 
   first four fields being zero, and a two part documentation
14
 
   string (in the variable DOC), which allows documentation both
15
 
   before and after the options; the two parts of DOC are
16
 
   separated by a vertical-tab character ('\v', or '\013').  By
17
 
   convention, the documentation before the options is just a
18
 
   short string saying what the program does, and that afterwards
19
 
   is longer, describing the behavior in more detail.  All
20
 
   documentation strings are automatically filled for output,
21
 
   although newlines may be included to force a line break at a
22
 
   particular point.  All documentation strings are also passed to
23
 
   the `gettext' function, for possible translation into the
24
 
   current locale.} */
25
 
 
26
 
#include <stdlib.h>
27
 
#include <error.h>
28
 
#include <argp.h>
29
 
 
30
 
const char *argp_program_version =
31
 
  "argp-ex4 1.0";
32
 
const char *argp_program_bug_address =
33
 
  "<bug-gnu-utils@@prep.ai.mit.edu>";
34
 
 
35
 
/* @r{Program documentation.} */
36
 
static char doc[] =
37
 
  "Argp example #4 -- a program with somewhat more complicated\
38
 
options\
39
 
\vThis part of the documentation comes *after* the options;\
40
 
 note that the text is automatically filled, but it's possible\
41
 
 to force a line-break, e.g.\n<-- here.";
42
 
 
43
 
/* @r{A description of the arguments we accept.} */
44
 
static char args_doc[] = "ARG1 [STRING...]";
45
 
 
46
 
/* @r{Keys for options without short-options.} */
47
 
#define OPT_ABORT  1            /* @r{--abort} */
48
 
 
49
 
/* @r{The options we understand.} */
50
 
static struct argp_option options[] = @{
51
 
  @{"verbose",  'v', 0,       0, "Produce verbose output" @},
52
 
  @{"quiet",    'q', 0,       0, "Don't produce any output" @},
53
 
  @{"silent",   's', 0,       OPTION_ALIAS @},
54
 
  @{"output",   'o', "FILE",  0,
55
 
   "Output to FILE instead of standard output" @},
56
 
 
57
 
  @{0,0,0,0, "The following options should be grouped together:" @},
58
 
  @{"repeat",   'r', "COUNT", OPTION_ARG_OPTIONAL,
59
 
   "Repeat the output COUNT (default 10) times"@},
60
 
  @{"abort",    OPT_ABORT, 0, 0, "Abort before showing any output"@},
61
 
 
62
 
  @{ 0 @}
63
 
@};
64
 
 
65
 
/* @r{Used by @code{main} to communicate with @code{parse_opt}.} */
66
 
struct arguments
67
 
@{
68
 
  char *arg1;                   /* @r{@var{arg1}} */
69
 
  char **strings;               /* @r{[@var{string}@dots{}]} */
70
 
  int silent, verbose, abort;   /* @r{@samp{-s}, @samp{-v}, @samp{--abort}} */
71
 
  char *output_file;            /* @r{@var{file} arg to @samp{--output}} */
72
 
  int repeat_count;             /* @r{@var{count} arg to @samp{--repeat}} */
73
 
@};
74
 
 
75
 
/* @r{Parse a single option.} */
76
 
static error_t
77
 
parse_opt (int key, char *arg, struct argp_state *state)
78
 
@{
79
 
  /* @r{Get the @code{input} argument from @code{argp_parse}, which we
80
 
     know is a pointer to our arguments structure.} */
81
 
  struct arguments *arguments = state->input;
82
 
 
83
 
  switch (key)
84
 
    @{
85
 
    case 'q': case 's':
86
 
      arguments->silent = 1;
87
 
      break;
88
 
    case 'v':
89
 
      arguments->verbose = 1;
90
 
      break;
91
 
    case 'o':
92
 
      arguments->output_file = arg;
93
 
      break;
94
 
    case 'r':
95
 
      arguments->repeat_count = arg ? atoi (arg) : 10;
96
 
      break;
97
 
    case OPT_ABORT:
98
 
      arguments->abort = 1;
99
 
      break;
100
 
 
101
 
    case ARGP_KEY_NO_ARGS:
102
 
      argp_usage (state);
103
 
 
104
 
    case ARGP_KEY_ARG:
105
 
      /* @r{Here we know that @code{state->arg_num == 0}, since we
106
 
         force argument parsing to end before any more arguments can
107
 
         get here.} */
108
 
      arguments->arg1 = arg;
109
 
 
110
 
      /* @r{Now we consume all the rest of the arguments.
111
 
         @code{state->next} is the index in @code{state->argv} of the
112
 
         next argument to be parsed, which is the first @var{string}
113
 
         we're interested in, so we can just use
114
 
         @code{&state->argv[state->next]} as the value for
115
 
         arguments->strings.
116
 
 
117
 
         @emph{In addition}, by setting @code{state->next} to the end
118
 
         of the arguments, we can force argp to stop parsing here and
119
 
         return.} */
120
 
      arguments->strings = &state->argv[state->next];
121
 
      state->next = state->argc;
122
 
 
123
 
      break;
124
 
 
125
 
    default:
126
 
      return ARGP_ERR_UNKNOWN;
127
 
    @}
128
 
  return 0;
129
 
@}
130
 
 
131
 
/* @r{Our argp parser.} */
132
 
static struct argp argp = @{ options, parse_opt, args_doc, doc @};
133
 
 
134
 
int main (int argc, char **argv)
135
 
@{
136
 
  int i, j;
137
 
  struct arguments arguments;
138
 
 
139
 
  /* @r{Default values.} */
140
 
  arguments.silent = 0;
141
 
  arguments.verbose = 0;
142
 
  arguments.output_file = "-";
143
 
  arguments.repeat_count = 1;
144
 
  arguments.abort = 0;
145
 
 
146
 
  /* @r{Parse our arguments; every option seen by @code{parse_opt} will be
147
 
     reflected in @code{arguments}.} */
148
 
  argp_parse (&argp, argc, argv, 0, 0, &arguments);
149
 
 
150
 
  if (arguments.abort)
151
 
    error (10, 0, "ABORTED");
152
 
 
153
 
  for (i = 0; i < arguments.repeat_count; i++)
154
 
    @{
155
 
      printf ("ARG1 = %s\n", arguments.arg1);
156
 
      printf ("STRINGS = ");
157
 
      for (j = 0; arguments.strings[j]; j++)
158
 
        printf (j == 0 ? "%s" : ", %s", arguments.strings[j]);
159
 
      printf ("\n");
160
 
      printf ("OUTPUT_FILE = %s\nVERBOSE = %s\nSILENT = %s\n",
161
 
              arguments.output_file,
162
 
              arguments.verbose ? "yes" : "no",
163
 
              arguments.silent ? "yes" : "no");
164
 
    @}
165
 
 
166
 
  exit (0);
167
 
@}