Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 594 lines (528 sloc) 15.616 kb
4a59fd13 » MadCoder
2007-10-15 Add a simple option parser.
1 #include "git-compat-util.h"
2 #include "parse-options.h"
26141b5b » MadCoder
2008-06-23 parse-opt: fake short strings for callers to believe in.
3 #include "cache.h"
269defdf » Jake Goulding
2009-01-26 Make opt_parse_with_commit() non-static
4 #include "commit.h"
73e9da01 » MarkLodato
2010-02-16 Add an optional argument for --color options
5 #include "color.h"
4a59fd13 » MadCoder
2007-10-15 Add a simple option parser.
6
47e9cd28 » trast
2010-06-12 parseopt: wrap rev-parse --parseopt usage for eval consumption
7 static int parse_options_usage(struct parse_opt_ctx_t *ctx,
8 const char * const *usagestr,
9c7304e3 » giuseppe
2010-05-17 print the usage string on stdout instead of stderr
9 const struct option *opts, int err);
41064ebc » gitster
2010-01-11 parse-options.c: mark file-local function static
10
4a59fd13 » MadCoder
2007-10-15 Add a simple option parser.
11 #define OPT_SHORT 1
12 #define OPT_UNSET 2
13
1f275b7c » divanorama
2011-08-11 parse-options: export opterr, optbug
14 int optbug(const struct option *opt, const char *reason)
1e5ce570 » jrn
2010-12-02 parse-options: clearer reporting of API misuse
15 {
16 if (opt->long_name)
17 return error("BUG: option '%s' %s", opt->long_name, reason);
18 return error("BUG: switch '%c' %s", opt->short_name, reason);
19 }
20
1f275b7c » divanorama
2011-08-11 parse-options: export opterr, optbug
21 int opterror(const struct option *opt, const char *reason, int flags)
4a59fd13 » MadCoder
2007-10-15 Add a simple option parser.
22 {
23 if (flags & OPT_SHORT)
24 return error("switch `%c' %s", opt->short_name, reason);
25 if (flags & OPT_UNSET)
26 return error("option `no-%s' %s", opt->long_name, reason);
27 return error("option `%s' %s", opt->long_name, reason);
28 }
29
1cc6985c » MadCoder
2008-07-08 parse-options: add PARSE_OPT_LASTARG_DEFAULT flag
30 static int get_arg(struct parse_opt_ctx_t *p, const struct option *opt,
31 int flags, const char **arg)
32 {
33 if (p->opt) {
34 *arg = p->opt;
35 p->opt = NULL;
36 } else if (p->argc == 1 && (opt->flags & PARSE_OPT_LASTARG_DEFAULT)) {
37 *arg = (const char *)opt->defval;
d5d745f9 » Olivier Marin
2008-07-21 parse-options: fix segmentation fault when a required value is missing
38 } else if (p->argc > 1) {
1cc6985c » MadCoder
2008-07-08 parse-options: add PARSE_OPT_LASTARG_DEFAULT flag
39 p->argc--;
40 *arg = *++p->argv;
41 } else
42 return opterror(opt, "requires a value", flags);
43 return 0;
44 }
45
df217ed6 » bebarino
2009-05-23 parse-opts: add OPT_FILENAME and transition builtins
46 static void fix_filename(const char *prefix, const char **file)
47 {
48 if (!file || !*file || !prefix || is_absolute_path(*file)
49 || !strcmp("-", *file))
50 return;
51 *file = xstrdup(prefix_filename(prefix, strlen(prefix), *file));
52 }
53
7e7bbcb4 » MadCoder
2008-06-23 parse-opt: have parse_options_{start,end}.
54 static int get_value(struct parse_opt_ctx_t *p,
1cc6985c » MadCoder
2008-07-08 parse-options: add PARSE_OPT_LASTARG_DEFAULT flag
55 const struct option *opt, int flags)
4a59fd13 » MadCoder
2007-10-15 Add a simple option parser.
56 {
ffe659f9 » MadCoder
2007-10-15 parse-options: make some arguments optional, add callbacks.
57 const char *s, *arg;
db7244bd » MadCoder
2007-11-07 parse-options new features.
58 const int unset = flags & OPT_UNSET;
df217ed6 » bebarino
2009-05-23 parse-opts: add OPT_FILENAME and transition builtins
59 int err;
4a59fd13 » MadCoder
2007-10-15 Add a simple option parser.
60
db7244bd » MadCoder
2007-11-07 parse-options new features.
61 if (unset && p->opt)
4a59fd13 » MadCoder
2007-10-15 Add a simple option parser.
62 return opterror(opt, "takes no value", flags);
db7244bd » MadCoder
2007-11-07 parse-options new features.
63 if (unset && (opt->flags & PARSE_OPT_NONEG))
64 return opterror(opt, "isn't available", flags);
c1f4ec9e » bebarino
2010-12-01 parse-options: do not infer PARSE_OPT_NOARG from option type
65 if (!(flags & OPT_SHORT) && p->opt && (opt->flags & PARSE_OPT_NOARG))
66 return opterror(opt, "takes no value", flags);
db7244bd » MadCoder
2007-11-07 parse-options new features.
67
68 switch (opt->type) {
b0b3a8b6 » jrn
2010-12-01 parse-options: allow git commands to invent new option types
69 case OPTION_LOWLEVEL_CALLBACK:
70 return (*(parse_opt_ll_cb *)opt->callback)(p, opt, unset);
71
db7244bd » MadCoder
2007-11-07 parse-options new features.
72 case OPTION_BIT:
73 if (unset)
74 *(int *)opt->value &= ~opt->defval;
4a59fd13 » MadCoder
2007-10-15 Add a simple option parser.
75 else
db7244bd » MadCoder
2007-11-07 parse-options new features.
76 *(int *)opt->value |= opt->defval;
77 return 0;
78
2f4b97f9 » René Scharfe
2009-05-07 parseopt: add OPT_NEGBIT
79 case OPTION_NEGBIT:
80 if (unset)
81 *(int *)opt->value |= opt->defval;
82 else
83 *(int *)opt->value &= ~opt->defval;
84 return 0;
85
b04ba2bb » gitster
2011-09-27 parse-options: deprecate OPT_BOOLEAN
86 case OPTION_COUNTUP:
db7244bd » MadCoder
2007-11-07 parse-options new features.
87 *(int *)opt->value = unset ? 0 : *(int *)opt->value + 1;
88 return 0;
89
90 case OPTION_SET_INT:
91 *(int *)opt->value = unset ? 0 : opt->defval;
92 return 0;
93
94 case OPTION_SET_PTR:
95 *(void **)opt->value = unset ? NULL : (void *)opt->defval;
4a59fd13 » MadCoder
2007-10-15 Add a simple option parser.
96 return 0;
97
98 case OPTION_STRING:
1cc6985c » MadCoder
2008-07-08 parse-options: add PARSE_OPT_LASTARG_DEFAULT flag
99 if (unset)
db7244bd » MadCoder
2007-11-07 parse-options new features.
100 *(const char **)opt->value = NULL;
1cc6985c » MadCoder
2008-07-08 parse-options: add PARSE_OPT_LASTARG_DEFAULT flag
101 else if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
ffe659f9 » MadCoder
2007-10-15 parse-options: make some arguments optional, add callbacks.
102 *(const char **)opt->value = (const char *)opt->defval;
1cc6985c » MadCoder
2008-07-08 parse-options: add PARSE_OPT_LASTARG_DEFAULT flag
103 else
104 return get_arg(p, opt, flags, (const char **)opt->value);
4a59fd13 » MadCoder
2007-10-15 Add a simple option parser.
105 return 0;
106
df217ed6 » bebarino
2009-05-23 parse-opts: add OPT_FILENAME and transition builtins
107 case OPTION_FILENAME:
108 err = 0;
109 if (unset)
110 *(const char **)opt->value = NULL;
111 else if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
112 *(const char **)opt->value = (const char *)opt->defval;
113 else
114 err = get_arg(p, opt, flags, (const char **)opt->value);
115
116 if (!err)
117 fix_filename(p->prefix, (const char **)opt->value);
118 return err;
119
ffe659f9 » MadCoder
2007-10-15 parse-options: make some arguments optional, add callbacks.
120 case OPTION_CALLBACK:
db7244bd » MadCoder
2007-11-07 parse-options new features.
121 if (unset)
07fe54db » MadCoder
2008-06-23 parse-opt: do not print errors on unknown options, return -2 intead.
122 return (*opt->callback)(opt, NULL, 1) ? (-1) : 0;
db7244bd » MadCoder
2007-11-07 parse-options new features.
123 if (opt->flags & PARSE_OPT_NOARG)
07fe54db » MadCoder
2008-06-23 parse-opt: do not print errors on unknown options, return -2 intead.
124 return (*opt->callback)(opt, NULL, 0) ? (-1) : 0;
c43a2483 » MadCoder
2007-12-21 Force the sticked form for options with optional arguments.
125 if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
07fe54db » MadCoder
2008-06-23 parse-opt: do not print errors on unknown options, return -2 intead.
126 return (*opt->callback)(opt, NULL, 0) ? (-1) : 0;
1cc6985c » MadCoder
2008-07-08 parse-options: add PARSE_OPT_LASTARG_DEFAULT flag
127 if (get_arg(p, opt, flags, &arg))
128 return -1;
129 return (*opt->callback)(opt, arg, 0) ? (-1) : 0;
ffe659f9 » MadCoder
2007-10-15 parse-options: make some arguments optional, add callbacks.
130
4a59fd13 » MadCoder
2007-10-15 Add a simple option parser.
131 case OPTION_INTEGER:
db7244bd » MadCoder
2007-11-07 parse-options new features.
132 if (unset) {
4a59fd13 » MadCoder
2007-10-15 Add a simple option parser.
133 *(int *)opt->value = 0;
134 return 0;
135 }
c43a2483 » MadCoder
2007-12-21 Force the sticked form for options with optional arguments.
136 if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
ffe659f9 » MadCoder
2007-10-15 parse-options: make some arguments optional, add callbacks.
137 *(int *)opt->value = opt->defval;
138 return 0;
139 }
1cc6985c » MadCoder
2008-07-08 parse-options: add PARSE_OPT_LASTARG_DEFAULT flag
140 if (get_arg(p, opt, flags, &arg))
141 return -1;
142 *(int *)opt->value = strtol(arg, (char **)&s, 10);
4a59fd13 » MadCoder
2007-10-15 Add a simple option parser.
143 if (*s)
144 return opterror(opt, "expects a numerical value", flags);
145 return 0;
146
147 default:
148 die("should not happen, someone must be hit on the forehead");
149 }
150 }
151
7e7bbcb4 » MadCoder
2008-06-23 parse-opt: have parse_options_{start,end}.
152 static int parse_short_opt(struct parse_opt_ctx_t *p, const struct option *options)
4a59fd13 » MadCoder
2007-10-15 Add a simple option parser.
153 {
e0319ff5 » René Scharfe
2009-05-07 parseopt: add OPT_NUMBER_CALLBACK
154 const struct option *numopt = NULL;
155
4a59fd13 » MadCoder
2007-10-15 Add a simple option parser.
156 for (; options->type != OPTION_END; options++) {
157 if (options->short_name == *p->opt) {
158 p->opt = p->opt[1] ? p->opt + 1 : NULL;
159 return get_value(p, options, OPT_SHORT);
160 }
e0319ff5 » René Scharfe
2009-05-07 parseopt: add OPT_NUMBER_CALLBACK
161
162 /*
163 * Handle the numerical option later, explicit one-digit
164 * options take precedence over it.
165 */
166 if (options->type == OPTION_NUMBER)
167 numopt = options;
168 }
169 if (numopt && isdigit(*p->opt)) {
170 size_t len = 1;
171 char *arg;
172 int rc;
173
174 while (isdigit(p->opt[len]))
175 len++;
176 arg = xmemdupz(p->opt, len);
177 p->opt = p->opt[len] ? p->opt + len : NULL;
178 rc = (*numopt->callback)(numopt, arg, 0) ? (-1) : 0;
179 free(arg);
180 return rc;
4a59fd13 » MadCoder
2007-10-15 Add a simple option parser.
181 }
07fe54db » MadCoder
2008-06-23 parse-opt: do not print errors on unknown options, return -2 intead.
182 return -2;
4a59fd13 » MadCoder
2007-10-15 Add a simple option parser.
183 }
184
7e7bbcb4 » MadCoder
2008-06-23 parse-opt: have parse_options_{start,end}.
185 static int parse_long_opt(struct parse_opt_ctx_t *p, const char *arg,
4a59fd13 » MadCoder
2007-10-15 Add a simple option parser.
186 const struct option *options)
187 {
7f275b91 » dscho
2007-10-14 parse-options: Allow abbreviated options when unambiguous
188 const char *arg_end = strchr(arg, '=');
243e0614 » dscho
2007-11-05 parse-options: abbreviation engine fix.
189 const struct option *abbrev_option = NULL, *ambiguous_option = NULL;
190 int abbrev_flags = 0, ambiguous_flags = 0;
7f275b91 » dscho
2007-10-14 parse-options: Allow abbreviated options when unambiguous
191
192 if (!arg_end)
193 arg_end = arg + strlen(arg);
194
4a59fd13 » MadCoder
2007-10-15 Add a simple option parser.
195 for (; options->type != OPTION_END; options++) {
0f1930c5 » René Scharfe
2012-02-25 parse-options: allow positivation of options starting, with no-
196 const char *rest, *long_name = options->long_name;
197 int flags = 0, opt_flags = 0;
4a59fd13 » MadCoder
2007-10-15 Add a simple option parser.
198
0f1930c5 » René Scharfe
2012-02-25 parse-options: allow positivation of options starting, with no-
199 if (!long_name)
4a59fd13 » MadCoder
2007-10-15 Add a simple option parser.
200 continue;
201
0f1930c5 » René Scharfe
2012-02-25 parse-options: allow positivation of options starting, with no-
202 again:
203 rest = skip_prefix(arg, long_name);
580d5bff » MadCoder
2008-03-02 parse-options: new option type to treat an option-like parameter as a…
204 if (options->type == OPTION_ARGUMENT) {
205 if (!rest)
206 continue;
207 if (*rest == '=')
208 return opterror(options, "takes no value", flags);
209 if (*rest)
210 continue;
211 p->out[p->cpidx++] = arg - 2;
212 return 0;
213 }
4a59fd13 » MadCoder
2007-10-15 Add a simple option parser.
214 if (!rest) {
7f275b91 » dscho
2007-10-14 parse-options: Allow abbreviated options when unambiguous
215 /* abbreviated? */
0f1930c5 » René Scharfe
2012-02-25 parse-options: allow positivation of options starting, with no-
216 if (!strncmp(long_name, arg, arg_end - arg)) {
7f275b91 » dscho
2007-10-14 parse-options: Allow abbreviated options when unambiguous
217 is_abbreviated:
243e0614 » dscho
2007-11-05 parse-options: abbreviation engine fix.
218 if (abbrev_option) {
219 /*
220 * If this is abbreviated, it is
221 * ambiguous. So when there is no
222 * exact match later, we need to
223 * error out.
224 */
225 ambiguous_option = abbrev_option;
226 ambiguous_flags = abbrev_flags;
227 }
7f275b91 » dscho
2007-10-14 parse-options: Allow abbreviated options when unambiguous
228 if (!(flags & OPT_UNSET) && *arg_end)
229 p->opt = arg_end + 1;
230 abbrev_option = options;
0f1930c5 » René Scharfe
2012-02-25 parse-options: allow positivation of options starting, with no-
231 abbrev_flags = flags ^ opt_flags;
7f275b91 » dscho
2007-10-14 parse-options: Allow abbreviated options when unambiguous
232 continue;
233 }
6bbfd1fa » andreas-schwab
2009-09-25 parse-opt: ignore negation of OPT_NONEG for ambiguity checks
234 /* negation allowed? */
235 if (options->flags & PARSE_OPT_NONEG)
236 continue;
7f275b91 » dscho
2007-10-14 parse-options: Allow abbreviated options when unambiguous
237 /* negated and abbreviated very much? */
238 if (!prefixcmp("no-", arg)) {
239 flags |= OPT_UNSET;
240 goto is_abbreviated;
241 }
242 /* negated? */
0f1930c5 » René Scharfe
2012-02-25 parse-options: allow positivation of options starting, with no-
243 if (prefixcmp(arg, "no-")) {
244 if (!prefixcmp(long_name, "no-")) {
245 long_name += 3;
246 opt_flags |= OPT_UNSET;
247 goto again;
248 }
4a59fd13 » MadCoder
2007-10-15 Add a simple option parser.
249 continue;
0f1930c5 » René Scharfe
2012-02-25 parse-options: allow positivation of options starting, with no-
250 }
4a59fd13 » MadCoder
2007-10-15 Add a simple option parser.
251 flags |= OPT_UNSET;
0f1930c5 » René Scharfe
2012-02-25 parse-options: allow positivation of options starting, with no-
252 rest = skip_prefix(arg + 3, long_name);
7f275b91 » dscho
2007-10-14 parse-options: Allow abbreviated options when unambiguous
253 /* abbreviated and negated? */
0f1930c5 » René Scharfe
2012-02-25 parse-options: allow positivation of options starting, with no-
254 if (!rest && !prefixcmp(long_name, arg + 3))
7f275b91 » dscho
2007-10-14 parse-options: Allow abbreviated options when unambiguous
255 goto is_abbreviated;
4a59fd13 » MadCoder
2007-10-15 Add a simple option parser.
256 if (!rest)
257 continue;
258 }
259 if (*rest) {
260 if (*rest != '=')
261 continue;
262 p->opt = rest + 1;
263 }
0f1930c5 » René Scharfe
2012-02-25 parse-options: allow positivation of options starting, with no-
264 return get_value(p, options, flags ^ opt_flags);
4a59fd13 » MadCoder
2007-10-15 Add a simple option parser.
265 }
243e0614 » dscho
2007-11-05 parse-options: abbreviation engine fix.
266
267 if (ambiguous_option)
268 return error("Ambiguous option: %s "
269 "(could be --%s%s or --%s%s)",
270 arg,
271 (ambiguous_flags & OPT_UNSET) ? "no-" : "",
272 ambiguous_option->long_name,
273 (abbrev_flags & OPT_UNSET) ? "no-" : "",
274 abbrev_option->long_name);
7f275b91 » dscho
2007-10-14 parse-options: Allow abbreviated options when unambiguous
275 if (abbrev_option)
276 return get_value(p, abbrev_option, abbrev_flags);
07fe54db » MadCoder
2008-06-23 parse-opt: do not print errors on unknown options, return -2 intead.
277 return -2;
4a59fd13 » MadCoder
2007-10-15 Add a simple option parser.
278 }
279
51a9949e » René Scharfe
2009-05-07 parseopt: add PARSE_OPT_NODASH
280 static int parse_nodash_opt(struct parse_opt_ctx_t *p, const char *arg,
281 const struct option *options)
282 {
283 for (; options->type != OPTION_END; options++) {
284 if (!(options->flags & PARSE_OPT_NODASH))
285 continue;
286 if (options->short_name == arg[0] && arg[1] == '\0')
287 return get_value(p, options, OPT_SHORT);
288 }
289 return -2;
290 }
291
1121a878 » Nanako Shiraishi
2008-07-16 parse-options.c: make check_typos() static
292 static void check_typos(const char *arg, const struct option *options)
3a9f0f41 » MadCoder
2008-01-26 parse-options: catch likely typo in presense of aggregated options.
293 {
294 if (strlen(arg) < 3)
295 return;
296
297 if (!prefixcmp(arg, "no-")) {
298 error ("did you mean `--%s` (with two dashes ?)", arg);
299 exit(129);
300 }
301
302 for (; options->type != OPTION_END; options++) {
303 if (!options->long_name)
304 continue;
305 if (!prefixcmp(options->long_name, arg)) {
306 error ("did you mean `--%s` (with two dashes ?)", arg);
307 exit(129);
308 }
309 }
310 }
311
cb9d398c » MadCoder
2009-06-09 parse-options: add parse_options_check to validate option specs.
312 static void parse_options_check(const struct option *opts)
313 {
314 int err = 0;
315
316 for (; opts->type != OPTION_END; opts++) {
317 if ((opts->flags & PARSE_OPT_LASTARG_DEFAULT) &&
1e5ce570 » jrn
2010-12-02 parse-options: clearer reporting of API misuse
318 (opts->flags & PARSE_OPT_OPTARG))
319 err |= optbug(opts, "uses incompatible flags "
320 "LASTARG_DEFAULT and OPTARG");
a02dd4ff » jrn
2010-12-02 parse-options: move NODASH sanity checks to parse_options_check
321 if (opts->flags & PARSE_OPT_NODASH &&
322 ((opts->flags & PARSE_OPT_OPTARG) ||
323 !(opts->flags & PARSE_OPT_NOARG) ||
324 !(opts->flags & PARSE_OPT_NONEG) ||
325 opts->long_name))
326 err |= optbug(opts, "uses feature "
327 "not supported for dashless options");
5c400ed2 » jrn
2010-12-02 parse-options: sanity check PARSE_OPT_NOARG flag
328 switch (opts->type) {
b04ba2bb » gitster
2011-09-27 parse-options: deprecate OPT_BOOLEAN
329 case OPTION_COUNTUP:
5c400ed2 » jrn
2010-12-02 parse-options: sanity check PARSE_OPT_NOARG flag
330 case OPTION_BIT:
331 case OPTION_NEGBIT:
332 case OPTION_SET_INT:
333 case OPTION_SET_PTR:
334 case OPTION_NUMBER:
335 if ((opts->flags & PARSE_OPT_OPTARG) ||
336 !(opts->flags & PARSE_OPT_NOARG))
337 err |= optbug(opts, "should not accept an argument");
338 default:
339 ; /* ok. (usually accepts an argument) */
340 }
cb9d398c » MadCoder
2009-06-09 parse-options: add parse_options_check to validate option specs.
341 }
342 if (err)
1e5ce570 » jrn
2010-12-02 parse-options: clearer reporting of API misuse
343 exit(128);
cb9d398c » MadCoder
2009-06-09 parse-options: add parse_options_check to validate option specs.
344 }
345
7e7bbcb4 » MadCoder
2008-06-23 parse-opt: have parse_options_{start,end}.
346 void parse_options_start(struct parse_opt_ctx_t *ctx,
37782920 » bebarino
2009-05-23 parse-opts: prepare for OPT_FILENAME
347 int argc, const char **argv, const char *prefix,
9ca1169f » bebarino
2010-12-05 parse-options: Don't call parse_options_check() so much
348 const struct option *options, int flags)
7e7bbcb4 » MadCoder
2008-06-23 parse-opt: have parse_options_{start,end}.
349 {
350 memset(ctx, 0, sizeof(*ctx));
351 ctx->argc = argc - 1;
352 ctx->argv = argv + 1;
353 ctx->out = argv;
37782920 » bebarino
2009-05-23 parse-opts: prepare for OPT_FILENAME
354 ctx->prefix = prefix;
a32a4eaa » MadCoder
2008-06-24 parse-opt: add PARSE_OPT_KEEP_ARGV0 parser option.
355 ctx->cpidx = ((flags & PARSE_OPT_KEEP_ARGV0) != 0);
7e7bbcb4 » MadCoder
2008-06-23 parse-opt: have parse_options_{start,end}.
356 ctx->flags = flags;
0d260f9a » René Scharfe
2009-03-09 parseopt: prevent KEEP_UNKNOWN and STOP_AT_NON_OPTION from being used…
357 if ((flags & PARSE_OPT_KEEP_UNKNOWN) &&
358 (flags & PARSE_OPT_STOP_AT_NON_OPTION))
359 die("STOP_AT_NON_OPTION and KEEP_UNKNOWN don't go together");
9ca1169f » bebarino
2010-12-05 parse-options: Don't call parse_options_check() so much
360 parse_options_check(options);
7e7bbcb4 » MadCoder
2008-06-23 parse-opt: have parse_options_{start,end}.
361 }
362
47e9cd28 » trast
2010-06-12 parseopt: wrap rev-parse --parseopt usage for eval consumption
363 static int usage_with_options_internal(struct parse_opt_ctx_t *,
364 const char * const *,
9c7304e3 » giuseppe
2010-05-17 print the usage string on stdout instead of stderr
365 const struct option *, int, int);
dd3bf0f4 » MadCoder
2007-11-19 parse-options: Allow to hide options from the default usage.
366
ff43ec3e » MadCoder
2008-06-23 parse-opt: create parse_options_step.
367 int parse_options_step(struct parse_opt_ctx_t *ctx,
368 const struct option *options,
369 const char * const usagestr[])
4a59fd13 » MadCoder
2007-10-15 Add a simple option parser.
370 {
b92891f9 » René Scharfe
2009-03-08 parseopt: add PARSE_OPT_NO_INTERNAL_HELP
371 int internal_help = !(ctx->flags & PARSE_OPT_NO_INTERNAL_HELP);
372
26141b5b » MadCoder
2008-06-23 parse-opt: fake short strings for callers to believe in.
373 /* we must reset ->opt, unknown short option leave it dangling */
374 ctx->opt = NULL;
375
ff43ec3e » MadCoder
2008-06-23 parse-opt: create parse_options_step.
376 for (; ctx->argc; ctx->argc--, ctx->argv++) {
377 const char *arg = ctx->argv[0];
4a59fd13 » MadCoder
2007-10-15 Add a simple option parser.
378
379 if (*arg != '-' || !arg[1]) {
51a9949e » René Scharfe
2009-05-07 parseopt: add PARSE_OPT_NODASH
380 if (parse_nodash_opt(ctx, arg, options) == 0)
381 continue;
ff43ec3e » MadCoder
2008-06-23 parse-opt: create parse_options_step.
382 if (ctx->flags & PARSE_OPT_STOP_AT_NON_OPTION)
979240fe » jrn
2010-12-01 parse-options: make resuming easier after PARSE_OPT_STOP_AT_NON_OPTION
383 return PARSE_OPT_NON_OPTION;
ff43ec3e » MadCoder
2008-06-23 parse-opt: create parse_options_step.
384 ctx->out[ctx->cpidx++] = ctx->argv[0];
4a59fd13 » MadCoder
2007-10-15 Add a simple option parser.
385 continue;
386 }
387
388 if (arg[1] != '-') {
ff43ec3e » MadCoder
2008-06-23 parse-opt: create parse_options_step.
389 ctx->opt = arg + 1;
b92891f9 » René Scharfe
2009-03-08 parseopt: add PARSE_OPT_NO_INTERNAL_HELP
390 if (internal_help && *ctx->opt == 'h')
47e9cd28 » trast
2010-06-12 parseopt: wrap rev-parse --parseopt usage for eval consumption
391 return parse_options_usage(ctx, usagestr, options, 0);
07fe54db » MadCoder
2008-06-23 parse-opt: do not print errors on unknown options, return -2 intead.
392 switch (parse_short_opt(ctx, options)) {
393 case -1:
47e9cd28 » trast
2010-06-12 parseopt: wrap rev-parse --parseopt usage for eval consumption
394 return parse_options_usage(ctx, usagestr, options, 1);
07fe54db » MadCoder
2008-06-23 parse-opt: do not print errors on unknown options, return -2 intead.
395 case -2:
38916c5b » René Scharfe
2012-03-03 parse-options: typo check for unknown switches
396 if (ctx->opt)
397 check_typos(arg + 1, options);
b5ce3a54 » René Scharfe
2009-03-08 parseopt: add PARSE_OPT_KEEP_UNKNOWN
398 goto unknown;
07fe54db » MadCoder
2008-06-23 parse-opt: do not print errors on unknown options, return -2 intead.
399 }
ff43ec3e » MadCoder
2008-06-23 parse-opt: create parse_options_step.
400 if (ctx->opt)
3a9f0f41 » MadCoder
2008-01-26 parse-options: catch likely typo in presense of aggregated options.
401 check_typos(arg + 1, options);
ff43ec3e » MadCoder
2008-06-23 parse-opt: create parse_options_step.
402 while (ctx->opt) {
b92891f9 » René Scharfe
2009-03-08 parseopt: add PARSE_OPT_NO_INTERNAL_HELP
403 if (internal_help && *ctx->opt == 'h')
47e9cd28 » trast
2010-06-12 parseopt: wrap rev-parse --parseopt usage for eval consumption
404 return parse_options_usage(ctx, usagestr, options, 0);
07fe54db » MadCoder
2008-06-23 parse-opt: do not print errors on unknown options, return -2 intead.
405 switch (parse_short_opt(ctx, options)) {
406 case -1:
47e9cd28 » trast
2010-06-12 parseopt: wrap rev-parse --parseopt usage for eval consumption
407 return parse_options_usage(ctx, usagestr, options, 1);
07fe54db » MadCoder
2008-06-23 parse-opt: do not print errors on unknown options, return -2 intead.
408 case -2:
26141b5b » MadCoder
2008-06-23 parse-opt: fake short strings for callers to believe in.
409 /* fake a short option thing to hide the fact that we may have
410 * started to parse aggregated stuff
411 *
412 * This is leaky, too bad.
413 */
414 ctx->argv[0] = xstrdup(ctx->opt - 1);
415 *(char *)ctx->argv[0] = '-';
b5ce3a54 » René Scharfe
2009-03-08 parseopt: add PARSE_OPT_KEEP_UNKNOWN
416 goto unknown;
07fe54db » MadCoder
2008-06-23 parse-opt: do not print errors on unknown options, return -2 intead.
417 }
3a9f0f41 » MadCoder
2008-01-26 parse-options: catch likely typo in presense of aggregated options.
418 }
4a59fd13 » MadCoder
2007-10-15 Add a simple option parser.
419 continue;
420 }
421
422 if (!arg[2]) { /* "--" */
ff43ec3e » MadCoder
2008-06-23 parse-opt: create parse_options_step.
423 if (!(ctx->flags & PARSE_OPT_KEEP_DASHDASH)) {
424 ctx->argc--;
425 ctx->argv++;
4a59fd13 » MadCoder
2007-10-15 Add a simple option parser.
426 }
427 break;
428 }
429
b92891f9 » René Scharfe
2009-03-08 parseopt: add PARSE_OPT_NO_INTERNAL_HELP
430 if (internal_help && !strcmp(arg + 2, "help-all"))
47e9cd28 » trast
2010-06-12 parseopt: wrap rev-parse --parseopt usage for eval consumption
431 return usage_with_options_internal(ctx, usagestr, options, 1, 0);
b92891f9 » René Scharfe
2009-03-08 parseopt: add PARSE_OPT_NO_INTERNAL_HELP
432 if (internal_help && !strcmp(arg + 2, "help"))
47e9cd28 » trast
2010-06-12 parseopt: wrap rev-parse --parseopt usage for eval consumption
433 return parse_options_usage(ctx, usagestr, options, 0);
07fe54db » MadCoder
2008-06-23 parse-opt: do not print errors on unknown options, return -2 intead.
434 switch (parse_long_opt(ctx, arg + 2, options)) {
435 case -1:
47e9cd28 » trast
2010-06-12 parseopt: wrap rev-parse --parseopt usage for eval consumption
436 return parse_options_usage(ctx, usagestr, options, 1);
07fe54db » MadCoder
2008-06-23 parse-opt: do not print errors on unknown options, return -2 intead.
437 case -2:
b5ce3a54 » René Scharfe
2009-03-08 parseopt: add PARSE_OPT_KEEP_UNKNOWN
438 goto unknown;
07fe54db » MadCoder
2008-06-23 parse-opt: do not print errors on unknown options, return -2 intead.
439 }
b5ce3a54 » René Scharfe
2009-03-08 parseopt: add PARSE_OPT_KEEP_UNKNOWN
440 continue;
441 unknown:
442 if (!(ctx->flags & PARSE_OPT_KEEP_UNKNOWN))
443 return PARSE_OPT_UNKNOWN;
444 ctx->out[ctx->cpidx++] = ctx->argv[0];
445 ctx->opt = NULL;
ff43ec3e » MadCoder
2008-06-23 parse-opt: create parse_options_step.
446 }
447 return PARSE_OPT_DONE;
448 }
449
450 int parse_options_end(struct parse_opt_ctx_t *ctx)
451 {
452 memmove(ctx->out + ctx->cpidx, ctx->argv, ctx->argc * sizeof(*ctx->out));
453 ctx->out[ctx->cpidx + ctx->argc] = NULL;
454 return ctx->cpidx + ctx->argc;
455 }
456
37782920 » bebarino
2009-05-23 parse-opts: prepare for OPT_FILENAME
457 int parse_options(int argc, const char **argv, const char *prefix,
458 const struct option *options, const char * const usagestr[],
459 int flags)
ff43ec3e » MadCoder
2008-06-23 parse-opt: create parse_options_step.
460 {
461 struct parse_opt_ctx_t ctx;
462
9ca1169f » bebarino
2010-12-05 parse-options: Don't call parse_options_check() so much
463 parse_options_start(&ctx, argc, argv, prefix, options, flags);
ff43ec3e » MadCoder
2008-06-23 parse-opt: create parse_options_step.
464 switch (parse_options_step(&ctx, options, usagestr)) {
465 case PARSE_OPT_HELP:
466 exit(129);
979240fe » jrn
2010-12-01 parse-options: make resuming easier after PARSE_OPT_STOP_AT_NON_OPTION
467 case PARSE_OPT_NON_OPTION:
ff43ec3e » MadCoder
2008-06-23 parse-opt: create parse_options_step.
468 case PARSE_OPT_DONE:
469 break;
470 default: /* PARSE_OPT_UNKNOWN */
07fe54db » MadCoder
2008-06-23 parse-opt: do not print errors on unknown options, return -2 intead.
471 if (ctx.argv[0][1] == '-') {
472 error("unknown option `%s'", ctx.argv[0] + 2);
473 } else {
474 error("unknown switch `%c'", *ctx.opt);
475 }
476 usage_with_options(usagestr, options);
4a59fd13 » MadCoder
2007-10-15 Add a simple option parser.
477 }
478
7e7bbcb4 » MadCoder
2008-06-23 parse-opt: have parse_options_{start,end}.
479 return parse_options_end(&ctx);
4a59fd13 » MadCoder
2007-10-15 Add a simple option parser.
480 }
d7a38c54 » MadCoder
2007-10-15 parse-options: be able to generate usages automatically
481
9c7304e3 » giuseppe
2010-05-17 print the usage string on stdout instead of stderr
482 static int usage_argh(const struct option *opts, FILE *outfile)
29f25d49 » bebarino
2009-05-21 parse-options: add PARSE_OPT_LITERAL_ARGHELP for complicated argh's
483 {
484 const char *s;
34aec9f5 » bebarino
2009-06-04 parse-options: simplify usage argh handling
485 int literal = (opts->flags & PARSE_OPT_LITERAL_ARGHELP) || !opts->argh;
29f25d49 » bebarino
2009-05-21 parse-options: add PARSE_OPT_LITERAL_ARGHELP for complicated argh's
486 if (opts->flags & PARSE_OPT_OPTARG)
487 if (opts->long_name)
488 s = literal ? "[=%s]" : "[=<%s>]";
489 else
490 s = literal ? "[%s]" : "[<%s>]";
491 else
492 s = literal ? " %s" : " <%s>";
54e6dc7d » pclouds
2012-05-06 i18n: parseopt: lookup help and argument translations when showing usage
493 return fprintf(outfile, s, opts->argh ? _(opts->argh) : _("..."));
29f25d49 » bebarino
2009-05-21 parse-options: add PARSE_OPT_LITERAL_ARGHELP for complicated argh's
494 }
495
d7a38c54 » MadCoder
2007-10-15 parse-options: be able to generate usages automatically
496 #define USAGE_OPTS_WIDTH 24
497 #define USAGE_GAP 2
498
47e9cd28 » trast
2010-06-12 parseopt: wrap rev-parse --parseopt usage for eval consumption
499 static int usage_with_options_internal(struct parse_opt_ctx_t *ctx,
500 const char * const *usagestr,
501 const struct option *opts, int full, int err)
d7a38c54 » MadCoder
2007-10-15 parse-options: be able to generate usages automatically
502 {
9c7304e3 » giuseppe
2010-05-17 print the usage string on stdout instead of stderr
503 FILE *outfile = err ? stderr : stdout;
504
49b61802 » René Scharfe
2009-03-08 parseopt: make usage optional
505 if (!usagestr)
506 return PARSE_OPT_HELP;
507
47e9cd28 » trast
2010-06-12 parseopt: wrap rev-parse --parseopt usage for eval consumption
508 if (!err && ctx && ctx->flags & PARSE_OPT_SHELL_EVAL)
509 fprintf(outfile, "cat <<\\EOF\n");
510
54e6dc7d » pclouds
2012-05-06 i18n: parseopt: lookup help and argument translations when showing usage
511 fprintf_ln(outfile, _("usage: %s"), _(*usagestr++));
f389c808 » raalkml
2007-10-14 Rework make_usage to print the usage message immediately
512 while (*usagestr && **usagestr)
54e6dc7d » pclouds
2012-05-06 i18n: parseopt: lookup help and argument translations when showing usage
513 /* TRANSLATORS: the colon here should align with the
514 one in "usage: %s" translation */
515 fprintf_ln(outfile, _(" or: %s"), _(*usagestr++));
44d86e91 » peff
2008-06-14 avoid whitespace on empty line in automatic usage message
516 while (*usagestr) {
54e6dc7d » pclouds
2012-05-06 i18n: parseopt: lookup help and argument translations when showing usage
517 if (**usagestr)
518 fprintf_ln(outfile, _(" %s"), _(*usagestr));
519 else
520 putchar('\n');
44d86e91 » peff
2008-06-14 avoid whitespace on empty line in automatic usage message
521 usagestr++;
522 }
d7a38c54 » MadCoder
2007-10-15 parse-options: be able to generate usages automatically
523
524 if (opts->type != OPTION_GROUP)
9c7304e3 » giuseppe
2010-05-17 print the usage string on stdout instead of stderr
525 fputc('\n', outfile);
d7a38c54 » MadCoder
2007-10-15 parse-options: be able to generate usages automatically
526
527 for (; opts->type != OPTION_END; opts++) {
528 size_t pos;
529 int pad;
530
531 if (opts->type == OPTION_GROUP) {
9c7304e3 » giuseppe
2010-05-17 print the usage string on stdout instead of stderr
532 fputc('\n', outfile);
d7a38c54 » MadCoder
2007-10-15 parse-options: be able to generate usages automatically
533 if (*opts->help)
54e6dc7d » pclouds
2012-05-06 i18n: parseopt: lookup help and argument translations when showing usage
534 fprintf(outfile, "%s\n", _(opts->help));
d7a38c54 » MadCoder
2007-10-15 parse-options: be able to generate usages automatically
535 continue;
536 }
dd3bf0f4 » MadCoder
2007-11-19 parse-options: Allow to hide options from the default usage.
537 if (!full && (opts->flags & PARSE_OPT_HIDDEN))
538 continue;
d7a38c54 » MadCoder
2007-10-15 parse-options: be able to generate usages automatically
539
9c7304e3 » giuseppe
2010-05-17 print the usage string on stdout instead of stderr
540 pos = fprintf(outfile, " ");
cbb08c2e » René Scharfe
2012-02-28 parse-options: remove PARSE_OPT_NEGHELP
541 if (opts->short_name) {
51a9949e » René Scharfe
2009-05-07 parseopt: add PARSE_OPT_NODASH
542 if (opts->flags & PARSE_OPT_NODASH)
9c7304e3 » giuseppe
2010-05-17 print the usage string on stdout instead of stderr
543 pos += fprintf(outfile, "%c", opts->short_name);
51a9949e » René Scharfe
2009-05-07 parseopt: add PARSE_OPT_NODASH
544 else
9c7304e3 » giuseppe
2010-05-17 print the usage string on stdout instead of stderr
545 pos += fprintf(outfile, "-%c", opts->short_name);
51a9949e » René Scharfe
2009-05-07 parseopt: add PARSE_OPT_NODASH
546 }
d7a38c54 » MadCoder
2007-10-15 parse-options: be able to generate usages automatically
547 if (opts->long_name && opts->short_name)
9c7304e3 » giuseppe
2010-05-17 print the usage string on stdout instead of stderr
548 pos += fprintf(outfile, ", ");
d7a38c54 » MadCoder
2007-10-15 parse-options: be able to generate usages automatically
549 if (opts->long_name)
cbb08c2e » René Scharfe
2012-02-28 parse-options: remove PARSE_OPT_NEGHELP
550 pos += fprintf(outfile, "--%s", opts->long_name);
e0319ff5 » René Scharfe
2009-05-07 parseopt: add OPT_NUMBER_CALLBACK
551 if (opts->type == OPTION_NUMBER)
9c7304e3 » giuseppe
2010-05-17 print the usage string on stdout instead of stderr
552 pos += fprintf(outfile, "-NUM");
d7a38c54 » MadCoder
2007-10-15 parse-options: be able to generate usages automatically
553
b57c68a6 » jrn
2010-12-01 parse-options: never suppress arghelp if LITERAL_ARGHELP is set
554 if ((opts->flags & PARSE_OPT_LITERAL_ARGHELP) ||
555 !(opts->flags & PARSE_OPT_NOARG))
9c7304e3 » giuseppe
2010-05-17 print the usage string on stdout instead of stderr
556 pos += usage_argh(opts, outfile);
d7a38c54 » MadCoder
2007-10-15 parse-options: be able to generate usages automatically
557
f389c808 » raalkml
2007-10-14 Rework make_usage to print the usage message immediately
558 if (pos <= USAGE_OPTS_WIDTH)
559 pad = USAGE_OPTS_WIDTH - pos;
d7a38c54 » MadCoder
2007-10-15 parse-options: be able to generate usages automatically
560 else {
9c7304e3 » giuseppe
2010-05-17 print the usage string on stdout instead of stderr
561 fputc('\n', outfile);
d7a38c54 » MadCoder
2007-10-15 parse-options: be able to generate usages automatically
562 pad = USAGE_OPTS_WIDTH;
563 }
54e6dc7d » pclouds
2012-05-06 i18n: parseopt: lookup help and argument translations when showing usage
564 fprintf(outfile, "%*s%s\n", pad + USAGE_GAP, "", _(opts->help));
d7a38c54 » MadCoder
2007-10-15 parse-options: be able to generate usages automatically
565 }
9c7304e3 » giuseppe
2010-05-17 print the usage string on stdout instead of stderr
566 fputc('\n', outfile);
f389c808 » raalkml
2007-10-14 Rework make_usage to print the usage message immediately
567
47e9cd28 » trast
2010-06-12 parseopt: wrap rev-parse --parseopt usage for eval consumption
568 if (!err && ctx && ctx->flags & PARSE_OPT_SHELL_EVAL)
569 fputs("EOF\n", outfile);
570
ee68b87a » MadCoder
2008-06-23 parse-opt: Export a non NORETURN usage dumper.
571 return PARSE_OPT_HELP;
d7a38c54 » MadCoder
2007-10-15 parse-options: be able to generate usages automatically
572 }
0ce865b1 » MadCoder
2007-10-14 Add shortcuts for very often used options.
573
c2e86add » bebarino
2011-03-22 Fix sparse warnings
574 void NORETURN usage_with_options(const char * const *usagestr,
ff43ec3e » MadCoder
2008-06-23 parse-opt: create parse_options_step.
575 const struct option *opts)
dd3bf0f4 » MadCoder
2007-11-19 parse-options: Allow to hide options from the default usage.
576 {
47e9cd28 » trast
2010-06-12 parseopt: wrap rev-parse --parseopt usage for eval consumption
577 usage_with_options_internal(NULL, usagestr, opts, 0, 1);
ff43ec3e » MadCoder
2008-06-23 parse-opt: create parse_options_step.
578 exit(129);
dd3bf0f4 » MadCoder
2007-11-19 parse-options: Allow to hide options from the default usage.
579 }
580
c2e86add » bebarino
2011-03-22 Fix sparse warnings
581 void NORETURN usage_msg_opt(const char *msg,
451bb210 » chriscool
2009-02-02 parse-options: add new function "usage_msg_opt"
582 const char * const *usagestr,
583 const struct option *options)
584 {
585 fprintf(stderr, "%s\n\n", msg);
586 usage_with_options(usagestr, options);
587 }
588
47e9cd28 » trast
2010-06-12 parseopt: wrap rev-parse --parseopt usage for eval consumption
589 static int parse_options_usage(struct parse_opt_ctx_t *ctx,
590 const char * const *usagestr,
9c7304e3 » giuseppe
2010-05-17 print the usage string on stdout instead of stderr
591 const struct option *opts, int err)
ee68b87a » MadCoder
2008-06-23 parse-opt: Export a non NORETURN usage dumper.
592 {
47e9cd28 » trast
2010-06-12 parseopt: wrap rev-parse --parseopt usage for eval consumption
593 return usage_with_options_internal(ctx, usagestr, opts, 0, err);
ee68b87a » MadCoder
2008-06-23 parse-opt: Export a non NORETURN usage dumper.
594 }
595
Something went wrong with that request. Please try again.