1 #include "git-compat-util.h"
2 #include "parse-options.h"
8 static inline const char *get_arg(struct parse_opt_ctx_t
*p
)
11 const char *res
= p
->opt
;
19 static inline const char *skip_prefix(const char *str
, const char *prefix
)
21 size_t len
= strlen(prefix
);
22 return strncmp(str
, prefix
, len
) ? NULL
: str
+ len
;
25 static int opterror(const struct option
*opt
, const char *reason
, int flags
)
27 if (flags
& OPT_SHORT
)
28 return error("switch `%c' %s", opt
->short_name
, reason
);
29 if (flags
& OPT_UNSET
)
30 return error("option `no-%s' %s", opt
->long_name
, reason
);
31 return error("option `%s' %s", opt
->long_name
, reason
);
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
);
61 arg
= p
->opt
? p
->opt
: (p
->argc
> 1 ? p
->argv
[1] : NULL
);
65 *(int *)opt
->value
&= ~opt
->defval
;
67 *(int *)opt
->value
|= opt
->defval
;
71 *(int *)opt
->value
= unset
? 0 : *(int *)opt
->value
+ 1;
75 *(int *)opt
->value
= unset
? 0 : opt
->defval
;
79 *(void **)opt
->value
= unset
? NULL
: (void *)opt
->defval
;
84 *(const char **)opt
->value
= NULL
;
87 if (opt
->flags
& PARSE_OPT_OPTARG
&& !p
->opt
) {
88 *(const char **)opt
->value
= (const char *)opt
->defval
;
92 return opterror(opt
, "requires a value", flags
);
93 *(const char **)opt
->value
= get_arg(p
);
98 return (*opt
->callback
)(opt
, NULL
, 1) ? (-1) : 0;
99 if (opt
->flags
& PARSE_OPT_NOARG
)
100 return (*opt
->callback
)(opt
, NULL
, 0) ? (-1) : 0;
101 if (opt
->flags
& PARSE_OPT_OPTARG
&& !p
->opt
)
102 return (*opt
->callback
)(opt
, NULL
, 0) ? (-1) : 0;
104 return opterror(opt
, "requires a value", flags
);
105 return (*opt
->callback
)(opt
, get_arg(p
), 0) ? (-1) : 0;
109 *(int *)opt
->value
= 0;
112 if (opt
->flags
& PARSE_OPT_OPTARG
&& !p
->opt
) {
113 *(int *)opt
->value
= opt
->defval
;
117 return opterror(opt
, "requires a value", flags
);
118 *(int *)opt
->value
= strtol(get_arg(p
), (char **)&s
, 10);
120 return opterror(opt
, "expects a numerical value", flags
);
124 die("should not happen, someone must be hit on the forehead");
128 static int parse_short_opt(struct parse_opt_ctx_t
*p
, const struct option
*options
)
130 for (; options
->type
!= OPTION_END
; options
++) {
131 if (options
->short_name
== *p
->opt
) {
132 p
->opt
= p
->opt
[1] ? p
->opt
+ 1 : NULL
;
133 return get_value(p
, options
, OPT_SHORT
);
139 static int parse_long_opt(struct parse_opt_ctx_t
*p
, const char *arg
,
140 const struct option
*options
)
142 const char *arg_end
= strchr(arg
, '=');
143 const struct option
*abbrev_option
= NULL
, *ambiguous_option
= NULL
;
144 int abbrev_flags
= 0, ambiguous_flags
= 0;
147 arg_end
= arg
+ strlen(arg
);
149 for (; options
->type
!= OPTION_END
; options
++) {
153 if (!options
->long_name
)
156 rest
= skip_prefix(arg
, options
->long_name
);
157 if (options
->type
== OPTION_ARGUMENT
) {
161 return opterror(options
, "takes no value", flags
);
164 p
->out
[p
->cpidx
++] = arg
- 2;
169 if (!strncmp(options
->long_name
, arg
, arg_end
- arg
)) {
173 * If this is abbreviated, it is
174 * ambiguous. So when there is no
175 * exact match later, we need to
178 ambiguous_option
= abbrev_option
;
179 ambiguous_flags
= abbrev_flags
;
181 if (!(flags
& OPT_UNSET
) && *arg_end
)
182 p
->opt
= arg_end
+ 1;
183 abbrev_option
= options
;
184 abbrev_flags
= flags
;
187 /* negated and abbreviated very much? */
188 if (!prefixcmp("no-", arg
)) {
193 if (strncmp(arg
, "no-", 3))
196 rest
= skip_prefix(arg
+ 3, options
->long_name
);
197 /* abbreviated and negated? */
198 if (!rest
&& !prefixcmp(options
->long_name
, arg
+ 3))
208 return get_value(p
, options
, flags
);
211 if (ambiguous_option
)
212 return error("Ambiguous option: %s "
213 "(could be --%s%s or --%s%s)",
215 (ambiguous_flags
& OPT_UNSET
) ? "no-" : "",
216 ambiguous_option
->long_name
,
217 (abbrev_flags
& OPT_UNSET
) ? "no-" : "",
218 abbrev_option
->long_name
);
220 return get_value(p
, abbrev_option
, abbrev_flags
);
224 void check_typos(const char *arg
, const struct option
*options
)
229 if (!prefixcmp(arg
, "no-")) {
230 error ("did you mean `--%s` (with two dashes ?)", arg
);
234 for (; options
->type
!= OPTION_END
; options
++) {
235 if (!options
->long_name
)
237 if (!prefixcmp(options
->long_name
, arg
)) {
238 error ("did you mean `--%s` (with two dashes ?)", arg
);
244 void parse_options_start(struct parse_opt_ctx_t
*ctx
,
245 int argc
, const char **argv
, int flags
)
247 memset(ctx
, 0, sizeof(*ctx
));
248 ctx
->argc
= argc
- 1;
249 ctx
->argv
= argv
+ 1;
254 static int usage_with_options_internal(const char * const *,
255 const struct option
*, int);
257 int parse_options_step(struct parse_opt_ctx_t
*ctx
,
258 const struct option
*options
,
259 const char * const usagestr
[])
261 /* we must reset ->opt, unknown short option leave it dangling */
264 for (; ctx
->argc
; ctx
->argc
--, ctx
->argv
++) {
265 const char *arg
= ctx
->argv
[0];
267 if (*arg
!= '-' || !arg
[1]) {
268 if (ctx
->flags
& PARSE_OPT_STOP_AT_NON_OPTION
)
270 ctx
->out
[ctx
->cpidx
++] = ctx
->argv
[0];
276 if (*ctx
->opt
== 'h')
277 return parse_options_usage(usagestr
, options
);
278 switch (parse_short_opt(ctx
, options
)) {
280 return parse_options_usage(usagestr
, options
);
282 return PARSE_OPT_UNKNOWN
;
285 check_typos(arg
+ 1, options
);
287 if (*ctx
->opt
== 'h')
288 return parse_options_usage(usagestr
, options
);
289 switch (parse_short_opt(ctx
, options
)) {
291 return parse_options_usage(usagestr
, options
);
293 /* fake a short option thing to hide the fact that we may have
294 * started to parse aggregated stuff
296 * This is leaky, too bad.
298 ctx
->argv
[0] = xstrdup(ctx
->opt
- 1);
299 *(char *)ctx
->argv
[0] = '-';
300 return PARSE_OPT_UNKNOWN
;
306 if (!arg
[2]) { /* "--" */
307 if (!(ctx
->flags
& PARSE_OPT_KEEP_DASHDASH
)) {
314 if (!strcmp(arg
+ 2, "help-all"))
315 return usage_with_options_internal(usagestr
, options
, 1);
316 if (!strcmp(arg
+ 2, "help"))
317 return parse_options_usage(usagestr
, options
);
318 switch (parse_long_opt(ctx
, arg
+ 2, options
)) {
320 return parse_options_usage(usagestr
, options
);
322 return PARSE_OPT_UNKNOWN
;
325 return PARSE_OPT_DONE
;
328 int parse_options_end(struct parse_opt_ctx_t
*ctx
)
330 memmove(ctx
->out
+ ctx
->cpidx
, ctx
->argv
, ctx
->argc
* sizeof(*ctx
->out
));
331 ctx
->out
[ctx
->cpidx
+ ctx
->argc
] = NULL
;
332 return ctx
->cpidx
+ ctx
->argc
;
335 int parse_options(int argc
, const char **argv
, const struct option
*options
,
336 const char * const usagestr
[], int flags
)
338 struct parse_opt_ctx_t ctx
;
340 parse_options_start(&ctx
, argc
, argv
, flags
);
341 switch (parse_options_step(&ctx
, options
, usagestr
)) {
346 default: /* PARSE_OPT_UNKNOWN */
347 if (ctx
.argv
[0][1] == '-') {
348 error("unknown option `%s'", ctx
.argv
[0] + 2);
350 error("unknown switch `%c'", *ctx
.opt
);
352 usage_with_options(usagestr
, options
);
355 return parse_options_end(&ctx
);
358 #define USAGE_OPTS_WIDTH 24
361 int usage_with_options_internal(const char * const *usagestr
,
362 const struct option
*opts
, int full
)
364 fprintf(stderr
, "usage: %s\n", *usagestr
++);
365 while (*usagestr
&& **usagestr
)
366 fprintf(stderr
, " or: %s\n", *usagestr
++);
368 fprintf(stderr
, "%s%s\n",
369 **usagestr
? " " : "",
374 if (opts
->type
!= OPTION_GROUP
)
377 for (; opts
->type
!= OPTION_END
; opts
++) {
381 if (opts
->type
== OPTION_GROUP
) {
384 fprintf(stderr
, "%s\n", opts
->help
);
387 if (!full
&& (opts
->flags
& PARSE_OPT_HIDDEN
))
390 pos
= fprintf(stderr
, " ");
391 if (opts
->short_name
)
392 pos
+= fprintf(stderr
, "-%c", opts
->short_name
);
393 if (opts
->long_name
&& opts
->short_name
)
394 pos
+= fprintf(stderr
, ", ");
396 pos
+= fprintf(stderr
, "--%s", opts
->long_name
);
398 switch (opts
->type
) {
399 case OPTION_ARGUMENT
:
402 if (opts
->flags
& PARSE_OPT_OPTARG
)
404 pos
+= fprintf(stderr
, "[=<n>]");
406 pos
+= fprintf(stderr
, "[<n>]");
408 pos
+= fprintf(stderr
, " <n>");
410 case OPTION_CALLBACK
:
411 if (opts
->flags
& PARSE_OPT_NOARG
)
416 if (opts
->flags
& PARSE_OPT_OPTARG
)
418 pos
+= fprintf(stderr
, "[=<%s>]", opts
->argh
);
420 pos
+= fprintf(stderr
, "[<%s>]", opts
->argh
);
422 pos
+= fprintf(stderr
, " <%s>", opts
->argh
);
424 if (opts
->flags
& PARSE_OPT_OPTARG
)
426 pos
+= fprintf(stderr
, "[=...]");
428 pos
+= fprintf(stderr
, "[...]");
430 pos
+= fprintf(stderr
, " ...");
433 default: /* OPTION_{BIT,BOOLEAN,SET_INT,SET_PTR} */
437 if (pos
<= USAGE_OPTS_WIDTH
)
438 pad
= USAGE_OPTS_WIDTH
- pos
;
441 pad
= USAGE_OPTS_WIDTH
;
443 fprintf(stderr
, "%*s%s\n", pad
+ USAGE_GAP
, "", opts
->help
);
447 return PARSE_OPT_HELP
;
450 void usage_with_options(const char * const *usagestr
,
451 const struct option
*opts
)
453 usage_with_options_internal(usagestr
, opts
, 0);
457 int parse_options_usage(const char * const *usagestr
,
458 const struct option
*opts
)
460 return usage_with_options_internal(usagestr
, opts
, 0);
464 /*----- some often used options -----*/
467 int parse_opt_abbrev_cb(const struct option
*opt
, const char *arg
, int unset
)
472 v
= unset
? 0 : DEFAULT_ABBREV
;
474 v
= strtol(arg
, (char **)&arg
, 10);
476 return opterror(opt
, "expects a numerical value", 0);
477 if (v
&& v
< MINIMUM_ABBREV
)
482 *(int *)(opt
->value
) = v
;
486 int parse_opt_approxidate_cb(const struct option
*opt
, const char *arg
,
489 *(unsigned long *)(opt
->value
) = approxidate(arg
);