~ubuntu-branches/ubuntu/raring/ifile/raring

« back to all changes in this revision

Viewing changes to argp/argp-ex4.c

  • Committer: Bazaar Package Importer
  • Author(s): Jens Peter Secher
  • Date: 2004-11-19 23:30:24 UTC
  • Revision ID: james.westby@ubuntu.com-20041119233024-3s7sqpy963jx22eu
Tags: upstream-1.3.4
ImportĀ upstreamĀ versionĀ 1.3.4

Show diffs side-by-side

added added

removed removed

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