1 #include "git-compat-util.h"
2 #include "parse-options.h"
7 static inline const char *get_arg(struct parse_opt_ctx_t
*p
)
10 const char *res
= p
->opt
;
18 static inline const char *skip_prefix(const char *str
, const char *prefix
)
20 size_t len
= strlen(prefix
);
21 return strncmp(str
, prefix
, len
) ? NULL
: str
+ len
;
24 static int opterror(const struct option
*opt
, const char *reason
, int flags
)
26 if (flags
& OPT_SHORT
)
27 return error("switch `%c' %s", opt
->short_name
, reason
);
28 if (flags
& OPT_UNSET
)
29 return error("option `no-%s' %s", opt
->long_name
, reason
);
30 return error("option `%s' %s", opt
->long_name
, reason
);
33 static int get_value(struct parse_opt_ctx_t
*p
,
34 const struct option
*opt
, int flags
)
37 const int unset
= flags
& OPT_UNSET
;
40 return opterror(opt
, "takes no value", flags
);
41 if (unset
&& (opt
->flags
& PARSE_OPT_NONEG
))
42 return opterror(opt
, "isn't available", flags
);
44 if (!(flags
& OPT_SHORT
) && p
->opt
) {
47 if (!(opt
->flags
& PARSE_OPT_NOARG
))
54 return opterror(opt
, "takes no value", flags
);
60 arg
= p
->opt
? p
->opt
: (p
->argc
> 1 ? p
->argv
[1] : NULL
);
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
;
86 if (opt
->flags
& PARSE_OPT_OPTARG
&& !p
->opt
) {
87 *(const char **)opt
->value
= (const char *)opt
->defval
;
91 return opterror(opt
, "requires a value", flags
);
92 *(const char **)opt
->value
= get_arg(p
);
97 return (*opt
->callback
)(opt
, NULL
, 1) ? (-1) : 0;
98 if (opt
->flags
& PARSE_OPT_NOARG
)
99 return (*opt
->callback
)(opt
, NULL
, 0) ? (-1) : 0;
100 if (opt
->flags
& PARSE_OPT_OPTARG
&& !p
->opt
)
101 return (*opt
->callback
)(opt
, NULL
, 0) ? (-1) : 0;
103 return opterror(opt
, "requires a value", flags
);
104 return (*opt
->callback
)(opt
, get_arg(p
), 0) ? (-1) : 0;
108 *(int *)opt
->value
= 0;
111 if (opt
->flags
& PARSE_OPT_OPTARG
&& !p
->opt
) {
112 *(int *)opt
->value
= opt
->defval
;
116 return opterror(opt
, "requires a value", flags
);
117 *(int *)opt
->value
= strtol(get_arg(p
), (char **)&s
, 10);
119 return opterror(opt
, "expects a numerical value", flags
);
123 die("should not happen, someone must be hit on the forehead");
127 static int parse_short_opt(struct parse_opt_ctx_t
*p
, const struct option
*options
)
129 for (; options
->type
!= OPTION_END
; options
++) {
130 if (options
->short_name
== *p
->opt
) {
131 p
->opt
= p
->opt
[1] ? p
->opt
+ 1 : NULL
;
132 return get_value(p
, options
, OPT_SHORT
);
138 static int parse_long_opt(struct parse_opt_ctx_t
*p
, const char *arg
,
139 const struct option
*options
)
141 const char *arg_end
= strchr(arg
, '=');
142 const struct option
*abbrev_option
= NULL
, *ambiguous_option
= NULL
;
143 int abbrev_flags
= 0, ambiguous_flags
= 0;
146 arg_end
= arg
+ strlen(arg
);
148 for (; options
->type
!= OPTION_END
; options
++) {
152 if (!options
->long_name
)
155 rest
= skip_prefix(arg
, options
->long_name
);
156 if (options
->type
== OPTION_ARGUMENT
) {
160 return opterror(options
, "takes no value", flags
);
163 p
->out
[p
->cpidx
++] = arg
- 2;
168 if (!strncmp(options
->long_name
, arg
, arg_end
- arg
)) {
172 * If this is abbreviated, it is
173 * ambiguous. So when there is no
174 * exact match later, we need to
177 ambiguous_option
= abbrev_option
;
178 ambiguous_flags
= abbrev_flags
;
180 if (!(flags
& OPT_UNSET
) && *arg_end
)
181 p
->opt
= arg_end
+ 1;
182 abbrev_option
= options
;
183 abbrev_flags
= flags
;
186 /* negated and abbreviated very much? */
187 if (!prefixcmp("no-", arg
)) {
192 if (strncmp(arg
, "no-", 3))
195 rest
= skip_prefix(arg
+ 3, options
->long_name
);
196 /* abbreviated and negated? */
197 if (!rest
&& !prefixcmp(options
->long_name
, arg
+ 3))
207 return get_value(p
, options
, flags
);
210 if (ambiguous_option
)
211 return error("Ambiguous option: %s "
212 "(could be --%s%s or --%s%s)",
214 (ambiguous_flags
& OPT_UNSET
) ? "no-" : "",
215 ambiguous_option
->long_name
,
216 (abbrev_flags
& OPT_UNSET
) ? "no-" : "",
217 abbrev_option
->long_name
);
219 return get_value(p
, abbrev_option
, abbrev_flags
);
223 void check_typos(const char *arg
, const struct option
*options
)
228 if (!prefixcmp(arg
, "no-")) {
229 error ("did you mean `--%s` (with two dashes ?)", arg
);
233 for (; options
->type
!= OPTION_END
; options
++) {
234 if (!options
->long_name
)
236 if (!prefixcmp(options
->long_name
, arg
)) {
237 error ("did you mean `--%s` (with two dashes ?)", arg
);
243 void parse_options_start(struct parse_opt_ctx_t
*ctx
,
244 int argc
, const char **argv
, int flags
)
246 memset(ctx
, 0, sizeof(*ctx
));
247 ctx
->argc
= argc
- 1;
248 ctx
->argv
= argv
+ 1;
253 static int usage_with_options_internal(const char * const *,
254 const struct option
*, int);
256 int parse_options_step(struct parse_opt_ctx_t
*ctx
,
257 const struct option
*options
,
258 const char * const usagestr
[])
260 for (; ctx
->argc
; ctx
->argc
--, ctx
->argv
++) {
261 const char *arg
= ctx
->argv
[0];
263 if (*arg
!= '-' || !arg
[1]) {
264 if (ctx
->flags
& PARSE_OPT_STOP_AT_NON_OPTION
)
266 ctx
->out
[ctx
->cpidx
++] = ctx
->argv
[0];
272 if (*ctx
->opt
== 'h')
273 return parse_options_usage(usagestr
, options
);
274 switch (parse_short_opt(ctx
, options
)) {
276 return parse_options_usage(usagestr
, options
);
278 return PARSE_OPT_UNKNOWN
;
281 check_typos(arg
+ 1, options
);
283 if (*ctx
->opt
== 'h')
284 return parse_options_usage(usagestr
, options
);
285 switch (parse_short_opt(ctx
, options
)) {
287 return parse_options_usage(usagestr
, options
);
289 return PARSE_OPT_UNKNOWN
;
295 if (!arg
[2]) { /* "--" */
296 if (!(ctx
->flags
& PARSE_OPT_KEEP_DASHDASH
)) {
303 if (!strcmp(arg
+ 2, "help-all"))
304 return usage_with_options_internal(usagestr
, options
, 1);
305 if (!strcmp(arg
+ 2, "help"))
306 return parse_options_usage(usagestr
, options
);
307 switch (parse_long_opt(ctx
, arg
+ 2, options
)) {
309 return parse_options_usage(usagestr
, options
);
311 return PARSE_OPT_UNKNOWN
;
314 return PARSE_OPT_DONE
;
317 int parse_options_end(struct parse_opt_ctx_t
*ctx
)
319 memmove(ctx
->out
+ ctx
->cpidx
, ctx
->argv
, ctx
->argc
* sizeof(*ctx
->out
));
320 ctx
->out
[ctx
->cpidx
+ ctx
->argc
] = NULL
;
321 return ctx
->cpidx
+ ctx
->argc
;
324 int parse_options(int argc
, const char **argv
, const struct option
*options
,
325 const char * const usagestr
[], int flags
)
327 struct parse_opt_ctx_t ctx
;
329 parse_options_start(&ctx
, argc
, argv
, flags
);
330 switch (parse_options_step(&ctx
, options
, usagestr
)) {
335 default: /* PARSE_OPT_UNKNOWN */
336 if (ctx
.argv
[0][1] == '-') {
337 error("unknown option `%s'", ctx
.argv
[0] + 2);
339 error("unknown switch `%c'", *ctx
.opt
);
341 usage_with_options(usagestr
, options
);
344 return parse_options_end(&ctx
);
347 #define USAGE_OPTS_WIDTH 24
350 int usage_with_options_internal(const char * const *usagestr
,
351 const struct option
*opts
, int full
)
353 fprintf(stderr
, "usage: %s\n", *usagestr
++);
354 while (*usagestr
&& **usagestr
)
355 fprintf(stderr
, " or: %s\n", *usagestr
++);
357 fprintf(stderr
, "%s%s\n",
358 **usagestr
? " " : "",
363 if (opts
->type
!= OPTION_GROUP
)
366 for (; opts
->type
!= OPTION_END
; opts
++) {
370 if (opts
->type
== OPTION_GROUP
) {
373 fprintf(stderr
, "%s\n", opts
->help
);
376 if (!full
&& (opts
->flags
& PARSE_OPT_HIDDEN
))
379 pos
= fprintf(stderr
, " ");
380 if (opts
->short_name
)
381 pos
+= fprintf(stderr
, "-%c", opts
->short_name
);
382 if (opts
->long_name
&& opts
->short_name
)
383 pos
+= fprintf(stderr
, ", ");
385 pos
+= fprintf(stderr
, "--%s", opts
->long_name
);
387 switch (opts
->type
) {
388 case OPTION_ARGUMENT
:
391 if (opts
->flags
& PARSE_OPT_OPTARG
)
393 pos
+= fprintf(stderr
, "[=<n>]");
395 pos
+= fprintf(stderr
, "[<n>]");
397 pos
+= fprintf(stderr
, " <n>");
399 case OPTION_CALLBACK
:
400 if (opts
->flags
& PARSE_OPT_NOARG
)
405 if (opts
->flags
& PARSE_OPT_OPTARG
)
407 pos
+= fprintf(stderr
, "[=<%s>]", opts
->argh
);
409 pos
+= fprintf(stderr
, "[<%s>]", opts
->argh
);
411 pos
+= fprintf(stderr
, " <%s>", opts
->argh
);
413 if (opts
->flags
& PARSE_OPT_OPTARG
)
415 pos
+= fprintf(stderr
, "[=...]");
417 pos
+= fprintf(stderr
, "[...]");
419 pos
+= fprintf(stderr
, " ...");
422 default: /* OPTION_{BIT,BOOLEAN,SET_INT,SET_PTR} */
426 if (pos
<= USAGE_OPTS_WIDTH
)
427 pad
= USAGE_OPTS_WIDTH
- pos
;
430 pad
= USAGE_OPTS_WIDTH
;
432 fprintf(stderr
, "%*s%s\n", pad
+ USAGE_GAP
, "", opts
->help
);
436 return PARSE_OPT_HELP
;
439 void usage_with_options(const char * const *usagestr
,
440 const struct option
*opts
)
442 usage_with_options_internal(usagestr
, opts
, 0);
446 int parse_options_usage(const char * const *usagestr
,
447 const struct option
*opts
)
449 return usage_with_options_internal(usagestr
, opts
, 0);
453 /*----- some often used options -----*/
456 int parse_opt_abbrev_cb(const struct option
*opt
, const char *arg
, int unset
)
461 v
= unset
? 0 : DEFAULT_ABBREV
;
463 v
= strtol(arg
, (char **)&arg
, 10);
465 return opterror(opt
, "expects a numerical value", 0);
466 if (v
&& v
< MINIMUM_ABBREV
)
471 *(int *)(opt
->value
) = v
;
475 int parse_opt_approxidate_cb(const struct option
*opt
, const char *arg
,
478 *(unsigned long *)(opt
->value
) = approxidate(arg
);