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
))
56 return opterror(opt
, "takes no value", flags
);
65 *(int *)opt
->value
&= ~opt
->defval
;
67 *(int *)opt
->value
|= opt
->defval
;
72 *(int *)opt
->value
|= opt
->defval
;
74 *(int *)opt
->value
&= ~opt
->defval
;
78 *(int *)opt
->value
= unset
? 0 : *(int *)opt
->value
+ 1;
82 *(int *)opt
->value
= unset
? 0 : opt
->defval
;
86 *(void **)opt
->value
= unset
? NULL
: (void *)opt
->defval
;
91 *(const char **)opt
->value
= NULL
;
92 else if (opt
->flags
& PARSE_OPT_OPTARG
&& !p
->opt
)
93 *(const char **)opt
->value
= (const char *)opt
->defval
;
95 return get_arg(p
, opt
, flags
, (const char **)opt
->value
);
100 return (*opt
->callback
)(opt
, NULL
, 1) ? (-1) : 0;
101 if (opt
->flags
& PARSE_OPT_NOARG
)
102 return (*opt
->callback
)(opt
, NULL
, 0) ? (-1) : 0;
103 if (opt
->flags
& PARSE_OPT_OPTARG
&& !p
->opt
)
104 return (*opt
->callback
)(opt
, NULL
, 0) ? (-1) : 0;
105 if (get_arg(p
, opt
, flags
, &arg
))
107 return (*opt
->callback
)(opt
, arg
, 0) ? (-1) : 0;
111 *(int *)opt
->value
= 0;
114 if (opt
->flags
& PARSE_OPT_OPTARG
&& !p
->opt
) {
115 *(int *)opt
->value
= opt
->defval
;
118 if (get_arg(p
, opt
, flags
, &arg
))
120 *(int *)opt
->value
= strtol(arg
, (char **)&s
, 10);
122 return opterror(opt
, "expects a numerical value", flags
);
126 die("should not happen, someone must be hit on the forehead");
130 static int parse_short_opt(struct parse_opt_ctx_t
*p
, const struct option
*options
)
132 for (; options
->type
!= OPTION_END
; options
++) {
133 if (options
->short_name
== *p
->opt
) {
134 p
->opt
= p
->opt
[1] ? p
->opt
+ 1 : NULL
;
135 return get_value(p
, options
, OPT_SHORT
);
141 static int parse_long_opt(struct parse_opt_ctx_t
*p
, const char *arg
,
142 const struct option
*options
)
144 const char *arg_end
= strchr(arg
, '=');
145 const struct option
*abbrev_option
= NULL
, *ambiguous_option
= NULL
;
146 int abbrev_flags
= 0, ambiguous_flags
= 0;
149 arg_end
= arg
+ strlen(arg
);
151 for (; options
->type
!= OPTION_END
; options
++) {
155 if (!options
->long_name
)
158 rest
= skip_prefix(arg
, options
->long_name
);
159 if (options
->type
== OPTION_ARGUMENT
) {
163 return opterror(options
, "takes no value", flags
);
166 p
->out
[p
->cpidx
++] = arg
- 2;
171 if (!strncmp(options
->long_name
, arg
, arg_end
- arg
)) {
175 * If this is abbreviated, it is
176 * ambiguous. So when there is no
177 * exact match later, we need to
180 ambiguous_option
= abbrev_option
;
181 ambiguous_flags
= abbrev_flags
;
183 if (!(flags
& OPT_UNSET
) && *arg_end
)
184 p
->opt
= arg_end
+ 1;
185 abbrev_option
= options
;
186 abbrev_flags
= flags
;
189 /* negated and abbreviated very much? */
190 if (!prefixcmp("no-", arg
)) {
195 if (strncmp(arg
, "no-", 3))
198 rest
= skip_prefix(arg
+ 3, options
->long_name
);
199 /* abbreviated and negated? */
200 if (!rest
&& !prefixcmp(options
->long_name
, arg
+ 3))
210 return get_value(p
, options
, flags
);
213 if (ambiguous_option
)
214 return error("Ambiguous option: %s "
215 "(could be --%s%s or --%s%s)",
217 (ambiguous_flags
& OPT_UNSET
) ? "no-" : "",
218 ambiguous_option
->long_name
,
219 (abbrev_flags
& OPT_UNSET
) ? "no-" : "",
220 abbrev_option
->long_name
);
222 return get_value(p
, abbrev_option
, abbrev_flags
);
226 static void check_typos(const char *arg
, const struct option
*options
)
231 if (!prefixcmp(arg
, "no-")) {
232 error ("did you mean `--%s` (with two dashes ?)", arg
);
236 for (; options
->type
!= OPTION_END
; options
++) {
237 if (!options
->long_name
)
239 if (!prefixcmp(options
->long_name
, arg
)) {
240 error ("did you mean `--%s` (with two dashes ?)", arg
);
246 void parse_options_start(struct parse_opt_ctx_t
*ctx
,
247 int argc
, const char **argv
, int flags
)
249 memset(ctx
, 0, sizeof(*ctx
));
250 ctx
->argc
= argc
- 1;
251 ctx
->argv
= argv
+ 1;
253 ctx
->cpidx
= ((flags
& PARSE_OPT_KEEP_ARGV0
) != 0);
255 if ((flags
& PARSE_OPT_KEEP_UNKNOWN
) &&
256 (flags
& PARSE_OPT_STOP_AT_NON_OPTION
))
257 die("STOP_AT_NON_OPTION and KEEP_UNKNOWN don't go together");
260 static int usage_with_options_internal(const char * const *,
261 const struct option
*, int);
263 int parse_options_step(struct parse_opt_ctx_t
*ctx
,
264 const struct option
*options
,
265 const char * const usagestr
[])
267 int internal_help
= !(ctx
->flags
& PARSE_OPT_NO_INTERNAL_HELP
);
269 /* we must reset ->opt, unknown short option leave it dangling */
272 for (; ctx
->argc
; ctx
->argc
--, ctx
->argv
++) {
273 const char *arg
= ctx
->argv
[0];
275 if (*arg
!= '-' || !arg
[1]) {
276 if (ctx
->flags
& PARSE_OPT_STOP_AT_NON_OPTION
)
278 ctx
->out
[ctx
->cpidx
++] = ctx
->argv
[0];
284 if (internal_help
&& *ctx
->opt
== 'h')
285 return parse_options_usage(usagestr
, options
);
286 switch (parse_short_opt(ctx
, options
)) {
288 return parse_options_usage(usagestr
, options
);
293 check_typos(arg
+ 1, options
);
295 if (internal_help
&& *ctx
->opt
== 'h')
296 return parse_options_usage(usagestr
, options
);
297 switch (parse_short_opt(ctx
, options
)) {
299 return parse_options_usage(usagestr
, options
);
301 /* fake a short option thing to hide the fact that we may have
302 * started to parse aggregated stuff
304 * This is leaky, too bad.
306 ctx
->argv
[0] = xstrdup(ctx
->opt
- 1);
307 *(char *)ctx
->argv
[0] = '-';
314 if (!arg
[2]) { /* "--" */
315 if (!(ctx
->flags
& PARSE_OPT_KEEP_DASHDASH
)) {
322 if (internal_help
&& !strcmp(arg
+ 2, "help-all"))
323 return usage_with_options_internal(usagestr
, options
, 1);
324 if (internal_help
&& !strcmp(arg
+ 2, "help"))
325 return parse_options_usage(usagestr
, options
);
326 switch (parse_long_opt(ctx
, arg
+ 2, options
)) {
328 return parse_options_usage(usagestr
, options
);
334 if (!(ctx
->flags
& PARSE_OPT_KEEP_UNKNOWN
))
335 return PARSE_OPT_UNKNOWN
;
336 ctx
->out
[ctx
->cpidx
++] = ctx
->argv
[0];
339 return PARSE_OPT_DONE
;
342 int parse_options_end(struct parse_opt_ctx_t
*ctx
)
344 memmove(ctx
->out
+ ctx
->cpidx
, ctx
->argv
, ctx
->argc
* sizeof(*ctx
->out
));
345 ctx
->out
[ctx
->cpidx
+ ctx
->argc
] = NULL
;
346 return ctx
->cpidx
+ ctx
->argc
;
349 int parse_options(int argc
, const char **argv
, const struct option
*options
,
350 const char * const usagestr
[], int flags
)
352 struct parse_opt_ctx_t ctx
;
354 parse_options_start(&ctx
, argc
, argv
, flags
);
355 switch (parse_options_step(&ctx
, options
, usagestr
)) {
360 default: /* PARSE_OPT_UNKNOWN */
361 if (ctx
.argv
[0][1] == '-') {
362 error("unknown option `%s'", ctx
.argv
[0] + 2);
364 error("unknown switch `%c'", *ctx
.opt
);
366 usage_with_options(usagestr
, options
);
369 return parse_options_end(&ctx
);
372 #define USAGE_OPTS_WIDTH 24
375 int usage_with_options_internal(const char * const *usagestr
,
376 const struct option
*opts
, int full
)
379 return PARSE_OPT_HELP
;
381 fprintf(stderr
, "usage: %s\n", *usagestr
++);
382 while (*usagestr
&& **usagestr
)
383 fprintf(stderr
, " or: %s\n", *usagestr
++);
385 fprintf(stderr
, "%s%s\n",
386 **usagestr
? " " : "",
391 if (opts
->type
!= OPTION_GROUP
)
394 for (; opts
->type
!= OPTION_END
; opts
++) {
398 if (opts
->type
== OPTION_GROUP
) {
401 fprintf(stderr
, "%s\n", opts
->help
);
404 if (!full
&& (opts
->flags
& PARSE_OPT_HIDDEN
))
407 pos
= fprintf(stderr
, " ");
408 if (opts
->short_name
)
409 pos
+= fprintf(stderr
, "-%c", opts
->short_name
);
410 if (opts
->long_name
&& opts
->short_name
)
411 pos
+= fprintf(stderr
, ", ");
413 pos
+= fprintf(stderr
, "--%s", opts
->long_name
);
415 switch (opts
->type
) {
416 case OPTION_ARGUMENT
:
419 if (opts
->flags
& PARSE_OPT_OPTARG
)
421 pos
+= fprintf(stderr
, "[=<n>]");
423 pos
+= fprintf(stderr
, "[<n>]");
425 pos
+= fprintf(stderr
, " <n>");
427 case OPTION_CALLBACK
:
428 if (opts
->flags
& PARSE_OPT_NOARG
)
433 if (opts
->flags
& PARSE_OPT_OPTARG
)
435 pos
+= fprintf(stderr
, "[=<%s>]", opts
->argh
);
437 pos
+= fprintf(stderr
, "[<%s>]", opts
->argh
);
439 pos
+= fprintf(stderr
, " <%s>", opts
->argh
);
441 if (opts
->flags
& PARSE_OPT_OPTARG
)
443 pos
+= fprintf(stderr
, "[=...]");
445 pos
+= fprintf(stderr
, "[...]");
447 pos
+= fprintf(stderr
, " ...");
450 default: /* OPTION_{BIT,BOOLEAN,SET_INT,SET_PTR} */
454 if (pos
<= USAGE_OPTS_WIDTH
)
455 pad
= USAGE_OPTS_WIDTH
- pos
;
458 pad
= USAGE_OPTS_WIDTH
;
460 fprintf(stderr
, "%*s%s\n", pad
+ USAGE_GAP
, "", opts
->help
);
464 return PARSE_OPT_HELP
;
467 void usage_with_options(const char * const *usagestr
,
468 const struct option
*opts
)
470 usage_with_options_internal(usagestr
, opts
, 0);
474 int parse_options_usage(const char * const *usagestr
,
475 const struct option
*opts
)
477 return usage_with_options_internal(usagestr
, opts
, 0);
481 /*----- some often used options -----*/
484 int parse_opt_abbrev_cb(const struct option
*opt
, const char *arg
, int unset
)
489 v
= unset
? 0 : DEFAULT_ABBREV
;
491 v
= strtol(arg
, (char **)&arg
, 10);
493 return opterror(opt
, "expects a numerical value", 0);
494 if (v
&& v
< MINIMUM_ABBREV
)
499 *(int *)(opt
->value
) = v
;
503 int parse_opt_approxidate_cb(const struct option
*opt
, const char *arg
,
506 *(unsigned long *)(opt
->value
) = approxidate(arg
);
510 int parse_opt_verbosity_cb(const struct option
*opt
, const char *arg
,
513 int *target
= opt
->value
;
516 /* --no-quiet, --no-verbose */
518 else if (opt
->short_name
== 'v') {
532 int parse_opt_with_commit(const struct option
*opt
, const char *arg
, int unset
)
534 unsigned char sha1
[20];
535 struct commit
*commit
;
539 if (get_sha1(arg
, sha1
))
540 return error("malformed object name %s", arg
);
541 commit
= lookup_commit_reference(sha1
);
543 return error("no such commit %s", arg
);
544 commit_list_insert(commit
, opt
->value
);
549 * This should really be OPTION_FILENAME type as a part of
550 * parse_options that take prefix to do this while parsing.
552 extern const char *parse_options_fix_filename(const char *prefix
, const char *file
)
554 if (!file
|| !prefix
|| is_absolute_path(file
) || !strcmp("-", file
))
556 return prefix_filename(prefix
, strlen(prefix
), file
);