1 #include "git-compat-util.h"
2 #include "parse-options.h"
14 static inline const char *get_arg(struct optparse_t *p)
17 const char *res = p->opt;
25 static inline const char *skip_prefix(const char *str, const char *prefix)
27 size_t len = strlen(prefix);
28 return strncmp(str, prefix, len) ? NULL : str + len;
31 static int opterror(const struct option *opt, const char *reason, int flags)
33 if (flags & OPT_SHORT)
34 return error("switch `%c' %s", opt->short_name, reason);
35 if (flags & OPT_UNSET)
36 return error("option `no-%s' %s", opt->long_name, reason);
37 return error("option `%s' %s", opt->long_name, reason);
40 static int get_value(struct optparse_t *p,
41 const struct option *opt, int flags)
44 const int unset = flags & OPT_UNSET;
47 return opterror(opt, "takes no value", flags);
48 if (unset && (opt->flags & PARSE_OPT_NONEG))
49 return opterror(opt, "isn't available", flags);
51 if (!(flags & OPT_SHORT) && p->opt) {
54 if (!(opt->flags & PARSE_OPT_NOARG))
61 return opterror(opt, "takes no value", flags);
67 arg = p->opt ? p->opt : (p->argc > 1 ? p->argv[1] : NULL);
71 *(int *)opt->value &= ~opt->defval;
73 *(int *)opt->value |= opt->defval;
77 *(int *)opt->value = unset ? 0 : *(int *)opt->value + 1;
81 *(int *)opt->value = unset ? 0 : opt->defval;
85 *(void **)opt->value = unset ? NULL : (void *)opt->defval;
90 *(const char **)opt->value = NULL;
93 if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
94 *(const char **)opt->value = (const char *)opt->defval;
98 return opterror(opt, "requires a value", flags);
99 *(const char **)opt->value = get_arg(p);
102 case OPTION_CALLBACK:
104 return (*opt->callback)(opt, NULL, 1);
105 if (opt->flags & PARSE_OPT_NOARG)
106 return (*opt->callback)(opt, NULL, 0);
107 if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
108 return (*opt->callback)(opt, NULL, 0);
110 return opterror(opt, "requires a value", flags);
111 return (*opt->callback)(opt, get_arg(p), 0);
115 *(int *)opt->value = 0;
118 if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
119 *(int *)opt->value = opt->defval;
123 return opterror(opt, "requires a value", flags);
124 *(int *)opt->value = strtol(get_arg(p), (char **)&s, 10);
126 return opterror(opt, "expects a numerical value", flags);
130 die("should not happen, someone must be hit on the forehead");
134 static int parse_short_opt(struct optparse_t *p, const struct option *options)
136 for (; options->type != OPTION_END; options++) {
137 if (options->short_name == *p->opt) {
138 p->opt = p->opt[1] ? p->opt + 1 : NULL;
139 return get_value(p, options, OPT_SHORT);
142 return error("unknown switch `%c'", *p->opt);
145 static int parse_long_opt(struct optparse_t *p, const char *arg,
146 const struct option *options)
148 const char *arg_end = strchr(arg, '=');
149 const struct option *abbrev_option = NULL, *ambiguous_option = NULL;
150 int abbrev_flags = 0, ambiguous_flags = 0;
153 arg_end = arg + strlen(arg);
155 for (; options->type != OPTION_END; options++) {
159 if (!options->long_name)
162 rest = skip_prefix(arg, options->long_name);
163 if (options->type == OPTION_ARGUMENT) {
167 return opterror(options, "takes no value", flags);
170 p->out[p->cpidx++] = arg - 2;
175 if (!strncmp(options->long_name, arg, arg_end - arg)) {
179 * If this is abbreviated, it is
180 * ambiguous. So when there is no
181 * exact match later, we need to
184 ambiguous_option = abbrev_option;
185 ambiguous_flags = abbrev_flags;
187 if (!(flags & OPT_UNSET) && *arg_end)
188 p->opt = arg_end + 1;
189 abbrev_option = options;
190 abbrev_flags = flags;
193 /* negated and abbreviated very much? */
194 if (!prefixcmp("no-", arg)) {
199 if (strncmp(arg, "no-", 3))
202 rest = skip_prefix(arg + 3, options->long_name);
203 /* abbreviated and negated? */
204 if (!rest && !prefixcmp(options->long_name, arg + 3))
214 return get_value(p, options, flags);
217 if (ambiguous_option)
218 return error("Ambiguous option: %s "
219 "(could be --%s%s or --%s%s)",
221 (ambiguous_flags & OPT_UNSET) ? "no-" : "",
222 ambiguous_option->long_name,
223 (abbrev_flags & OPT_UNSET) ? "no-" : "",
224 abbrev_option->long_name);
226 return get_value(p, abbrev_option, abbrev_flags);
227 return error("unknown option `%s'", arg);
230 void check_typos(const char *arg, const struct option *options)
235 if (!prefixcmp(arg, "no-")) {
236 error ("did you mean `--%s` (with two dashes ?)", arg);
240 for (; options->type != OPTION_END; options++) {
241 if (!options->long_name)
243 if (!prefixcmp(options->long_name, arg)) {
244 error ("did you mean `--%s` (with two dashes ?)", arg);
250 static NORETURN void usage_with_options_internal(const char * const *,
251 const struct option *, int);
253 int parse_options(int argc, const char **argv, const struct option *options,
254 const char * const usagestr[], int flags)
256 struct optparse_t args = { argv + 1, argv, argc - 1, 0, NULL };
258 for (; args.argc; args.argc--, args.argv++) {
259 const char *arg = args.argv[0];
261 if (*arg != '-' || !arg[1]) {
262 if (flags & PARSE_OPT_STOP_AT_NON_OPTION)
264 args.out[args.cpidx++] = args.argv[0];
270 if (*args.opt == 'h')
271 usage_with_options(usagestr, options);
272 if (parse_short_opt(&args, options) < 0)
273 usage_with_options(usagestr, options);
275 check_typos(arg + 1, options);
277 if (*args.opt == 'h')
278 usage_with_options(usagestr, options);
279 if (parse_short_opt(&args, options) < 0)
280 usage_with_options(usagestr, options);
285 if (!arg[2]) { /* "--" */
286 if (!(flags & PARSE_OPT_KEEP_DASHDASH)) {
293 if (!strcmp(arg + 2, "help-all"))
294 usage_with_options_internal(usagestr, options, 1);
295 if (!strcmp(arg + 2, "help"))
296 usage_with_options(usagestr, options);
297 if (parse_long_opt(&args, arg + 2, options))
298 usage_with_options(usagestr, options);
301 memmove(args.out + args.cpidx, args.argv, args.argc * sizeof(*args.out));
302 args.out[args.cpidx + args.argc] = NULL;
303 return args.cpidx + args.argc;
306 #define USAGE_OPTS_WIDTH 24
309 void usage_with_options_internal(const char * const *usagestr,
310 const struct option *opts, int full)
312 fprintf(stderr, "usage: %s\n", *usagestr++);
313 while (*usagestr && **usagestr)
314 fprintf(stderr, " or: %s\n", *usagestr++);
316 fprintf(stderr, " %s\n", *usagestr++);
318 if (opts->type != OPTION_GROUP)
321 for (; opts->type != OPTION_END; opts++) {
325 if (opts->type == OPTION_GROUP) {
328 fprintf(stderr, "%s\n", opts->help);
331 if (!full && (opts->flags & PARSE_OPT_HIDDEN))
334 pos = fprintf(stderr, " ");
335 if (opts->short_name)
336 pos += fprintf(stderr, "-%c", opts->short_name);
337 if (opts->long_name && opts->short_name)
338 pos += fprintf(stderr, ", ");
340 pos += fprintf(stderr, "--%s", opts->long_name);
342 switch (opts->type) {
343 case OPTION_ARGUMENT:
346 if (opts->flags & PARSE_OPT_OPTARG)
347 pos += fprintf(stderr, " [<n>]");
349 pos += fprintf(stderr, " <n>");
351 case OPTION_CALLBACK:
352 if (opts->flags & PARSE_OPT_NOARG)
357 if (opts->flags & PARSE_OPT_OPTARG)
358 pos += fprintf(stderr, " [<%s>]", opts->argh);
360 pos += fprintf(stderr, " <%s>", opts->argh);
362 if (opts->flags & PARSE_OPT_OPTARG)
363 pos += fprintf(stderr, " [...]");
365 pos += fprintf(stderr, " ...");
368 default: /* OPTION_{BIT,BOOLEAN,SET_INT,SET_PTR} */
372 if (pos <= USAGE_OPTS_WIDTH)
373 pad = USAGE_OPTS_WIDTH - pos;
376 pad = USAGE_OPTS_WIDTH;
378 fprintf(stderr, "%*s%s\n", pad + USAGE_GAP, "", opts->help);
385 void usage_with_options(const char * const *usagestr,
386 const struct option *opts)
388 usage_with_options_internal(usagestr, opts, 0);
391 /*----- some often used options -----*/
394 int parse_opt_abbrev_cb(const struct option *opt, const char *arg, int unset)
399 v = unset ? 0 : DEFAULT_ABBREV;
401 v = strtol(arg, (char **)&arg, 10);
403 return opterror(opt, "expects a numerical value", 0);
404 if (v && v < MINIMUM_ABBREV)
409 *(int *)(opt->value) = v;