1 #include "git-compat-util.h"
2 #include "parse-options.h"
9 static int opterror(const struct option
*opt
, const char *reason
, int flags
)
11 if (flags
& OPT_SHORT
)
12 return error("switch `%c' %s", opt
->short_name
, reason
);
13 if (flags
& OPT_UNSET
)
14 return error("option `no-%s' %s", opt
->long_name
, reason
);
15 return error("option `%s' %s", opt
->long_name
, reason
);
18 static int get_arg(struct parse_opt_ctx_t
*p
, const struct option
*opt
,
19 int flags
, const char **arg
)
24 } else if (p
->argc
== 1 && (opt
->flags
& PARSE_OPT_LASTARG_DEFAULT
)) {
25 *arg
= (const char *)opt
->defval
;
26 } else if (p
->argc
> 1) {
30 return opterror(opt
, "requires a value", flags
);
34 static int get_value(struct parse_opt_ctx_t
*p
,
35 const struct option
*opt
, int flags
)
38 const int unset
= flags
& OPT_UNSET
;
41 return opterror(opt
, "takes no value", flags
);
42 if (unset
&& (opt
->flags
& PARSE_OPT_NONEG
))
43 return opterror(opt
, "isn't available", flags
);
45 if (!(flags
& OPT_SHORT
) && p
->opt
) {
48 if (!(opt
->flags
& PARSE_OPT_NOARG
))
55 return opterror(opt
, "takes no value", flags
);
64 *(int *)opt
->value
&= ~opt
->defval
;
66 *(int *)opt
->value
|= opt
->defval
;
70 *(int *)opt
->value
= unset
? 0 : *(int *)opt
->value
+ 1;
74 *(int *)opt
->value
= unset
? 0 : opt
->defval
;
78 *(void **)opt
->value
= unset
? NULL
: (void *)opt
->defval
;
83 *(const char **)opt
->value
= NULL
;
84 else if (opt
->flags
& PARSE_OPT_OPTARG
&& !p
->opt
)
85 *(const char **)opt
->value
= (const char *)opt
->defval
;
87 return get_arg(p
, opt
, flags
, (const char **)opt
->value
);
92 return (*opt
->callback
)(opt
, NULL
, 1) ? (-1) : 0;
93 if (opt
->flags
& PARSE_OPT_NOARG
)
94 return (*opt
->callback
)(opt
, NULL
, 0) ? (-1) : 0;
95 if (opt
->flags
& PARSE_OPT_OPTARG
&& !p
->opt
)
96 return (*opt
->callback
)(opt
, NULL
, 0) ? (-1) : 0;
97 if (get_arg(p
, opt
, flags
, &arg
))
99 return (*opt
->callback
)(opt
, arg
, 0) ? (-1) : 0;
103 *(int *)opt
->value
= 0;
106 if (opt
->flags
& PARSE_OPT_OPTARG
&& !p
->opt
) {
107 *(int *)opt
->value
= opt
->defval
;
110 if (get_arg(p
, opt
, flags
, &arg
))
112 *(int *)opt
->value
= strtol(arg
, (char **)&s
, 10);
114 return opterror(opt
, "expects a numerical value", flags
);
118 die("should not happen, someone must be hit on the forehead");
122 static int parse_short_opt(struct parse_opt_ctx_t
*p
, const struct option
*options
)
124 for (; options
->type
!= OPTION_END
; options
++) {
125 if (options
->short_name
== *p
->opt
) {
126 p
->opt
= p
->opt
[1] ? p
->opt
+ 1 : NULL
;
127 return get_value(p
, options
, OPT_SHORT
);
133 static int parse_long_opt(struct parse_opt_ctx_t
*p
, const char *arg
,
134 const struct option
*options
)
136 const char *arg_end
= strchr(arg
, '=');
137 const struct option
*abbrev_option
= NULL
, *ambiguous_option
= NULL
;
138 int abbrev_flags
= 0, ambiguous_flags
= 0;
141 arg_end
= arg
+ strlen(arg
);
143 for (; options
->type
!= OPTION_END
; options
++) {
147 if (!options
->long_name
)
150 rest
= skip_prefix(arg
, options
->long_name
);
151 if (options
->type
== OPTION_ARGUMENT
) {
155 return opterror(options
, "takes no value", flags
);
158 p
->out
[p
->cpidx
++] = arg
- 2;
163 if (!strncmp(options
->long_name
, arg
, arg_end
- arg
)) {
167 * If this is abbreviated, it is
168 * ambiguous. So when there is no
169 * exact match later, we need to
172 ambiguous_option
= abbrev_option
;
173 ambiguous_flags
= abbrev_flags
;
175 if (!(flags
& OPT_UNSET
) && *arg_end
)
176 p
->opt
= arg_end
+ 1;
177 abbrev_option
= options
;
178 abbrev_flags
= flags
;
181 /* negated and abbreviated very much? */
182 if (!prefixcmp("no-", arg
)) {
187 if (strncmp(arg
, "no-", 3))
190 rest
= skip_prefix(arg
+ 3, options
->long_name
);
191 /* abbreviated and negated? */
192 if (!rest
&& !prefixcmp(options
->long_name
, arg
+ 3))
202 return get_value(p
, options
, flags
);
205 if (ambiguous_option
)
206 return error("Ambiguous option: %s "
207 "(could be --%s%s or --%s%s)",
209 (ambiguous_flags
& OPT_UNSET
) ? "no-" : "",
210 ambiguous_option
->long_name
,
211 (abbrev_flags
& OPT_UNSET
) ? "no-" : "",
212 abbrev_option
->long_name
);
214 return get_value(p
, abbrev_option
, abbrev_flags
);
218 static void check_typos(const char *arg
, const struct option
*options
)
223 if (!prefixcmp(arg
, "no-")) {
224 error ("did you mean `--%s` (with two dashes ?)", arg
);
228 for (; options
->type
!= OPTION_END
; options
++) {
229 if (!options
->long_name
)
231 if (!prefixcmp(options
->long_name
, arg
)) {
232 error ("did you mean `--%s` (with two dashes ?)", arg
);
238 void parse_options_start(struct parse_opt_ctx_t
*ctx
,
239 int argc
, const char **argv
, int flags
)
241 memset(ctx
, 0, sizeof(*ctx
));
242 ctx
->argc
= argc
- 1;
243 ctx
->argv
= argv
+ 1;
245 ctx
->cpidx
= ((flags
& PARSE_OPT_KEEP_ARGV0
) != 0);
249 static int usage_with_options_internal(const char * const *,
250 const struct option
*, int);
252 int parse_options_step(struct parse_opt_ctx_t
*ctx
,
253 const struct option
*options
,
254 const char * const usagestr
[])
256 /* we must reset ->opt, unknown short option leave it dangling */
259 for (; ctx
->argc
; ctx
->argc
--, ctx
->argv
++) {
260 const char *arg
= ctx
->argv
[0];
262 if (*arg
!= '-' || !arg
[1]) {
263 if (ctx
->flags
& PARSE_OPT_STOP_AT_NON_OPTION
)
265 ctx
->out
[ctx
->cpidx
++] = ctx
->argv
[0];
271 if (*ctx
->opt
== 'h')
272 return parse_options_usage(usagestr
, options
);
273 switch (parse_short_opt(ctx
, options
)) {
275 return parse_options_usage(usagestr
, options
);
277 return PARSE_OPT_UNKNOWN
;
280 check_typos(arg
+ 1, options
);
282 if (*ctx
->opt
== 'h')
283 return parse_options_usage(usagestr
, options
);
284 switch (parse_short_opt(ctx
, options
)) {
286 return parse_options_usage(usagestr
, options
);
288 /* fake a short option thing to hide the fact that we may have
289 * started to parse aggregated stuff
291 * This is leaky, too bad.
293 ctx
->argv
[0] = xstrdup(ctx
->opt
- 1);
294 *(char *)ctx
->argv
[0] = '-';
295 return PARSE_OPT_UNKNOWN
;
301 if (!arg
[2]) { /* "--" */
302 if (!(ctx
->flags
& PARSE_OPT_KEEP_DASHDASH
)) {
309 if (!strcmp(arg
+ 2, "help-all"))
310 return usage_with_options_internal(usagestr
, options
, 1);
311 if (!strcmp(arg
+ 2, "help"))
312 return parse_options_usage(usagestr
, options
);
313 switch (parse_long_opt(ctx
, arg
+ 2, options
)) {
315 return parse_options_usage(usagestr
, options
);
317 return PARSE_OPT_UNKNOWN
;
320 return PARSE_OPT_DONE
;
323 int parse_options_end(struct parse_opt_ctx_t
*ctx
)
325 memmove(ctx
->out
+ ctx
->cpidx
, ctx
->argv
, ctx
->argc
* sizeof(*ctx
->out
));
326 ctx
->out
[ctx
->cpidx
+ ctx
->argc
] = NULL
;
327 return ctx
->cpidx
+ ctx
->argc
;
330 int parse_options(int argc
, const char **argv
, const struct option
*options
,
331 const char * const usagestr
[], int flags
)
333 struct parse_opt_ctx_t ctx
;
335 parse_options_start(&ctx
, argc
, argv
, flags
);
336 switch (parse_options_step(&ctx
, options
, usagestr
)) {
341 default: /* PARSE_OPT_UNKNOWN */
342 if (ctx
.argv
[0][1] == '-') {
343 error("unknown option `%s'", ctx
.argv
[0] + 2);
345 error("unknown switch `%c'", *ctx
.opt
);
347 usage_with_options(usagestr
, options
);
350 return parse_options_end(&ctx
);
353 #define USAGE_OPTS_WIDTH 24
356 int usage_with_options_internal(const char * const *usagestr
,
357 const struct option
*opts
, int full
)
359 fprintf(stderr
, "usage: %s\n", *usagestr
++);
360 while (*usagestr
&& **usagestr
)
361 fprintf(stderr
, " or: %s\n", *usagestr
++);
363 fprintf(stderr
, "%s%s\n",
364 **usagestr
? " " : "",
369 if (opts
->type
!= OPTION_GROUP
)
372 for (; opts
->type
!= OPTION_END
; opts
++) {
376 if (opts
->type
== OPTION_GROUP
) {
379 fprintf(stderr
, "%s\n", opts
->help
);
382 if (!full
&& (opts
->flags
& PARSE_OPT_HIDDEN
))
385 pos
= fprintf(stderr
, " ");
386 if (opts
->short_name
)
387 pos
+= fprintf(stderr
, "-%c", opts
->short_name
);
388 if (opts
->long_name
&& opts
->short_name
)
389 pos
+= fprintf(stderr
, ", ");
391 pos
+= fprintf(stderr
, "--%s", opts
->long_name
);
393 switch (opts
->type
) {
394 case OPTION_ARGUMENT
:
397 if (opts
->flags
& PARSE_OPT_OPTARG
)
399 pos
+= fprintf(stderr
, "[=<n>]");
401 pos
+= fprintf(stderr
, "[<n>]");
403 pos
+= fprintf(stderr
, " <n>");
405 case OPTION_CALLBACK
:
406 if (opts
->flags
& PARSE_OPT_NOARG
)
411 if (opts
->flags
& PARSE_OPT_OPTARG
)
413 pos
+= fprintf(stderr
, "[=<%s>]", opts
->argh
);
415 pos
+= fprintf(stderr
, "[<%s>]", opts
->argh
);
417 pos
+= fprintf(stderr
, " <%s>", opts
->argh
);
419 if (opts
->flags
& PARSE_OPT_OPTARG
)
421 pos
+= fprintf(stderr
, "[=...]");
423 pos
+= fprintf(stderr
, "[...]");
425 pos
+= fprintf(stderr
, " ...");
428 default: /* OPTION_{BIT,BOOLEAN,SET_INT,SET_PTR} */
432 if (pos
<= USAGE_OPTS_WIDTH
)
433 pad
= USAGE_OPTS_WIDTH
- pos
;
436 pad
= USAGE_OPTS_WIDTH
;
438 fprintf(stderr
, "%*s%s\n", pad
+ USAGE_GAP
, "", opts
->help
);
442 return PARSE_OPT_HELP
;
445 void usage_with_options(const char * const *usagestr
,
446 const struct option
*opts
)
448 usage_with_options_internal(usagestr
, opts
, 0);
452 int parse_options_usage(const char * const *usagestr
,
453 const struct option
*opts
)
455 return usage_with_options_internal(usagestr
, opts
, 0);
459 /*----- some often used options -----*/
462 int parse_opt_abbrev_cb(const struct option
*opt
, const char *arg
, int unset
)
467 v
= unset
? 0 : DEFAULT_ABBREV
;
469 v
= strtol(arg
, (char **)&arg
, 10);
471 return opterror(opt
, "expects a numerical value", 0);
472 if (v
&& v
< MINIMUM_ABBREV
)
477 *(int *)(opt
->value
) = v
;
481 int parse_opt_approxidate_cb(const struct option
*opt
, const char *arg
,
484 *(unsigned long *)(opt
->value
) = approxidate(arg
);
488 int parse_opt_verbosity_cb(const struct option
*opt
, const char *arg
,
491 int *target
= opt
->value
;
494 /* --no-quiet, --no-verbose */
496 else if (opt
->short_name
== 'v') {
510 int parse_opt_with_commit(const struct option
*opt
, const char *arg
, int unset
)
512 unsigned char sha1
[20];
513 struct commit
*commit
;
517 if (get_sha1(arg
, sha1
))
518 return error("malformed object name %s", arg
);
519 commit
= lookup_commit_reference(sha1
);
521 return error("no such commit %s", arg
);
522 commit_list_insert(commit
, opt
->value
);
527 * This should really be OPTION_FILENAME type as a part of
528 * parse_options that take prefix to do this while parsing.
530 extern const char *parse_options_fix_filename(const char *prefix
, const char *file
)
532 if (!file
|| !prefix
|| is_absolute_path(file
) || !strcmp("-", file
))
534 return prefix_filename(prefix
, strlen(prefix
), file
);