parse-options: Allow abbreviated options when unambiguous
[git/platforms/storm.git] / parse-options.c
blobb4a3b63e9f33b9f2af40fd8cf17dd2aeff312a35
1 #include "git-compat-util.h"
2 #include "parse-options.h"
4 #define OPT_SHORT 1
5 #define OPT_UNSET 2
7 struct optparse_t {
8 const char **argv;
9 int argc;
10 const char *opt;
13 static inline const char *get_arg(struct optparse_t *p)
15 if (p->opt) {
16 const char *res = p->opt;
17 p->opt = NULL;
18 return res;
20 p->argc--;
21 return *++p->argv;
24 static inline const char *skip_prefix(const char *str, const char *prefix)
26 size_t len = strlen(prefix);
27 return strncmp(str, prefix, len) ? NULL : str + len;
30 static int opterror(const struct option *opt, const char *reason, int flags)
32 if (flags & OPT_SHORT)
33 return error("switch `%c' %s", opt->short_name, reason);
34 if (flags & OPT_UNSET)
35 return error("option `no-%s' %s", opt->long_name, reason);
36 return error("option `%s' %s", opt->long_name, reason);
39 static int get_value(struct optparse_t *p,
40 const struct option *opt, int flags)
42 const char *s, *arg;
43 arg = p->opt ? p->opt : (p->argc > 1 ? p->argv[1] : NULL);
45 if (p->opt && (flags & OPT_UNSET))
46 return opterror(opt, "takes no value", flags);
48 switch (opt->type) {
49 case OPTION_BOOLEAN:
50 if (!(flags & OPT_SHORT) && p->opt)
51 return opterror(opt, "takes no value", flags);
52 if (flags & OPT_UNSET)
53 *(int *)opt->value = 0;
54 else
55 (*(int *)opt->value)++;
56 return 0;
58 case OPTION_STRING:
59 if (flags & OPT_UNSET) {
60 *(const char **)opt->value = (const char *)NULL;
61 return 0;
63 if (opt->flags & PARSE_OPT_OPTARG && (!arg || *arg == '-')) {
64 *(const char **)opt->value = (const char *)opt->defval;
65 return 0;
67 if (!arg)
68 return opterror(opt, "requires a value", flags);
69 *(const char **)opt->value = get_arg(p);
70 return 0;
72 case OPTION_CALLBACK:
73 if (flags & OPT_UNSET)
74 return (*opt->callback)(opt, NULL, 1);
75 if (opt->flags & PARSE_OPT_OPTARG && (!arg || *arg == '-'))
76 return (*opt->callback)(opt, NULL, 0);
77 if (!arg)
78 return opterror(opt, "requires a value", flags);
79 return (*opt->callback)(opt, get_arg(p), 0);
81 case OPTION_INTEGER:
82 if (flags & OPT_UNSET) {
83 *(int *)opt->value = 0;
84 return 0;
86 if (opt->flags & PARSE_OPT_OPTARG && (!arg || !isdigit(*arg))) {
87 *(int *)opt->value = opt->defval;
88 return 0;
90 if (!arg)
91 return opterror(opt, "requires a value", flags);
92 *(int *)opt->value = strtol(get_arg(p), (char **)&s, 10);
93 if (*s)
94 return opterror(opt, "expects a numerical value", flags);
95 return 0;
97 default:
98 die("should not happen, someone must be hit on the forehead");
102 static int parse_short_opt(struct optparse_t *p, const struct option *options)
104 for (; options->type != OPTION_END; options++) {
105 if (options->short_name == *p->opt) {
106 p->opt = p->opt[1] ? p->opt + 1 : NULL;
107 return get_value(p, options, OPT_SHORT);
110 return error("unknown switch `%c'", *p->opt);
113 static int parse_long_opt(struct optparse_t *p, const char *arg,
114 const struct option *options)
116 const char *arg_end = strchr(arg, '=');
117 const struct option *abbrev_option = NULL;
118 int abbrev_flags = 0;
120 if (!arg_end)
121 arg_end = arg + strlen(arg);
123 for (; options->type != OPTION_END; options++) {
124 const char *rest;
125 int flags = 0;
127 if (!options->long_name)
128 continue;
130 rest = skip_prefix(arg, options->long_name);
131 if (!rest) {
132 /* abbreviated? */
133 if (!strncmp(options->long_name, arg, arg_end - arg)) {
134 is_abbreviated:
135 if (abbrev_option)
136 return error("Ambiguous option: %s "
137 "(could be --%s%s or --%s%s)",
138 arg,
139 (flags & OPT_UNSET) ?
140 "no-" : "",
141 options->long_name,
142 (abbrev_flags & OPT_UNSET) ?
143 "no-" : "",
144 abbrev_option->long_name);
145 if (!(flags & OPT_UNSET) && *arg_end)
146 p->opt = arg_end + 1;
147 abbrev_option = options;
148 abbrev_flags = flags;
149 continue;
151 /* negated and abbreviated very much? */
152 if (!prefixcmp("no-", arg)) {
153 flags |= OPT_UNSET;
154 goto is_abbreviated;
156 /* negated? */
157 if (strncmp(arg, "no-", 3))
158 continue;
159 flags |= OPT_UNSET;
160 rest = skip_prefix(arg + 3, options->long_name);
161 /* abbreviated and negated? */
162 if (!rest && !prefixcmp(options->long_name, arg + 3))
163 goto is_abbreviated;
164 if (!rest)
165 continue;
167 if (*rest) {
168 if (*rest != '=')
169 continue;
170 p->opt = rest + 1;
172 return get_value(p, options, flags);
174 if (abbrev_option)
175 return get_value(p, abbrev_option, abbrev_flags);
176 return error("unknown option `%s'", arg);
179 int parse_options(int argc, const char **argv, const struct option *options,
180 const char * const usagestr[], int flags)
182 struct optparse_t args = { argv + 1, argc - 1, NULL };
183 int j = 0;
185 for (; args.argc; args.argc--, args.argv++) {
186 const char *arg = args.argv[0];
188 if (*arg != '-' || !arg[1]) {
189 argv[j++] = args.argv[0];
190 continue;
193 if (arg[1] != '-') {
194 args.opt = arg + 1;
195 do {
196 if (*args.opt == 'h')
197 usage_with_options(usagestr, options);
198 if (parse_short_opt(&args, options) < 0)
199 usage_with_options(usagestr, options);
200 } while (args.opt);
201 continue;
204 if (!arg[2]) { /* "--" */
205 if (!(flags & PARSE_OPT_KEEP_DASHDASH)) {
206 args.argc--;
207 args.argv++;
209 break;
212 if (!strcmp(arg + 2, "help"))
213 usage_with_options(usagestr, options);
214 if (parse_long_opt(&args, arg + 2, options))
215 usage_with_options(usagestr, options);
218 memmove(argv + j, args.argv, args.argc * sizeof(*argv));
219 argv[j + args.argc] = NULL;
220 return j + args.argc;
223 #define USAGE_OPTS_WIDTH 24
224 #define USAGE_GAP 2
226 void usage_with_options(const char * const *usagestr,
227 const struct option *opts)
229 fprintf(stderr, "usage: %s\n", *usagestr++);
230 while (*usagestr && **usagestr)
231 fprintf(stderr, " or: %s\n", *usagestr++);
232 while (*usagestr)
233 fprintf(stderr, " %s\n", *usagestr++);
235 if (opts->type != OPTION_GROUP)
236 fputc('\n', stderr);
238 for (; opts->type != OPTION_END; opts++) {
239 size_t pos;
240 int pad;
242 if (opts->type == OPTION_GROUP) {
243 fputc('\n', stderr);
244 if (*opts->help)
245 fprintf(stderr, "%s\n", opts->help);
246 continue;
249 pos = fprintf(stderr, " ");
250 if (opts->short_name)
251 pos += fprintf(stderr, "-%c", opts->short_name);
252 if (opts->long_name && opts->short_name)
253 pos += fprintf(stderr, ", ");
254 if (opts->long_name)
255 pos += fprintf(stderr, "--%s", opts->long_name);
257 switch (opts->type) {
258 case OPTION_INTEGER:
259 if (opts->flags & PARSE_OPT_OPTARG)
260 pos += fprintf(stderr, " [<n>]");
261 else
262 pos += fprintf(stderr, " <n>");
263 break;
264 case OPTION_STRING:
265 case OPTION_CALLBACK:
266 if (opts->argh) {
267 if (opts->flags & PARSE_OPT_OPTARG)
268 pos += fprintf(stderr, " [<%s>]", opts->argh);
269 else
270 pos += fprintf(stderr, " <%s>", opts->argh);
271 } else {
272 if (opts->flags & PARSE_OPT_OPTARG)
273 pos += fprintf(stderr, " [...]");
274 else
275 pos += fprintf(stderr, " ...");
277 break;
278 default:
279 break;
282 if (pos <= USAGE_OPTS_WIDTH)
283 pad = USAGE_OPTS_WIDTH - pos;
284 else {
285 fputc('\n', stderr);
286 pad = USAGE_OPTS_WIDTH;
288 fprintf(stderr, "%*s%s\n", pad + USAGE_GAP, "", opts->help);
290 fputc('\n', stderr);
292 exit(129);
295 /*----- some often used options -----*/
296 #include "cache.h"
297 int parse_opt_abbrev_cb(const struct option *opt, const char *arg, int unset)
299 int v;
301 if (!arg) {
302 v = unset ? 0 : DEFAULT_ABBREV;
303 } else {
304 v = strtol(arg, (char **)&arg, 10);
305 if (*arg)
306 return opterror(opt, "expects a numerical value", 0);
307 if (v && v < MINIMUM_ABBREV)
308 v = MINIMUM_ABBREV;
309 else if (v > 40)
310 v = 40;
312 *(int *)(opt->value) = v;
313 return 0;