Merge branch 'maint'
[git] / parse-options.c
1 #include "git-compat-util.h"
2 #include "parse-options.h"
3
4 #define OPT_SHORT 1
5 #define OPT_UNSET 2
6
7 struct optparse_t {
8         const char **argv;
9         int argc;
10         const char *opt;
11 };
12
13 static inline const char *get_arg(struct optparse_t *p)
14 {
15         if (p->opt) {
16                 const char *res = p->opt;
17                 p->opt = NULL;
18                 return res;
19         }
20         p->argc--;
21         return *++p->argv;
22 }
23
24 static inline const char *skip_prefix(const char *str, const char *prefix)
25 {
26         size_t len = strlen(prefix);
27         return strncmp(str, prefix, len) ? NULL : str + len;
28 }
29
30 static int opterror(const struct option *opt, const char *reason, int flags)
31 {
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);
37 }
38
39 static int get_value(struct optparse_t *p,
40                      const struct option *opt, int flags)
41 {
42         const char *s, *arg;
43         arg = p->opt ? p->opt : (p->argc > 1 ? p->argv[1] : NULL);
44
45         if (p->opt && (flags & OPT_UNSET))
46                 return opterror(opt, "takes no value", flags);
47
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;
57
58         case OPTION_STRING:
59                 if (flags & OPT_UNSET) {
60                         *(const char **)opt->value = (const char *)NULL;
61                         return 0;
62                 }
63                 if (opt->flags & PARSE_OPT_OPTARG && (!arg || *arg == '-')) {
64                         *(const char **)opt->value = (const char *)opt->defval;
65                         return 0;
66                 }
67                 if (!arg)
68                         return opterror(opt, "requires a value", flags);
69                 *(const char **)opt->value = get_arg(p);
70                 return 0;
71
72         case OPTION_CALLBACK:
73                 if (flags & OPT_UNSET)
74                         return (*opt->callback)(opt, NULL, 1);
75                 if (opt->flags & PARSE_OPT_NOARG) {
76                         if (p->opt && !(flags & OPT_SHORT))
77                                 return opterror(opt, "takes no value", flags);
78                         return (*opt->callback)(opt, NULL, 0);
79                 }
80                 if (opt->flags & PARSE_OPT_OPTARG && (!arg || *arg == '-'))
81                         return (*opt->callback)(opt, NULL, 0);
82                 if (!arg)
83                         return opterror(opt, "requires a value", flags);
84                 return (*opt->callback)(opt, get_arg(p), 0);
85
86         case OPTION_INTEGER:
87                 if (flags & OPT_UNSET) {
88                         *(int *)opt->value = 0;
89                         return 0;
90                 }
91                 if (opt->flags & PARSE_OPT_OPTARG && (!arg || !isdigit(*arg))) {
92                         *(int *)opt->value = opt->defval;
93                         return 0;
94                 }
95                 if (!arg)
96                         return opterror(opt, "requires a value", flags);
97                 *(int *)opt->value = strtol(get_arg(p), (char **)&s, 10);
98                 if (*s)
99                         return opterror(opt, "expects a numerical value", flags);
100                 return 0;
101
102         default:
103                 die("should not happen, someone must be hit on the forehead");
104         }
105 }
106
107 static int parse_short_opt(struct optparse_t *p, const struct option *options)
108 {
109         for (; options->type != OPTION_END; options++) {
110                 if (options->short_name == *p->opt) {
111                         p->opt = p->opt[1] ? p->opt + 1 : NULL;
112                         return get_value(p, options, OPT_SHORT);
113                 }
114         }
115         return error("unknown switch `%c'", *p->opt);
116 }
117
118 static int parse_long_opt(struct optparse_t *p, const char *arg,
119                           const struct option *options)
120 {
121         const char *arg_end = strchr(arg, '=');
122         const struct option *abbrev_option = NULL;
123         int abbrev_flags = 0;
124
125         if (!arg_end)
126                 arg_end = arg + strlen(arg);
127
128         for (; options->type != OPTION_END; options++) {
129                 const char *rest;
130                 int flags = 0;
131
132                 if (!options->long_name)
133                         continue;
134
135                 rest = skip_prefix(arg, options->long_name);
136                 if (!rest) {
137                         /* abbreviated? */
138                         if (!strncmp(options->long_name, arg, arg_end - arg)) {
139 is_abbreviated:
140                                 if (abbrev_option)
141                                         return error("Ambiguous option: %s "
142                                                 "(could be --%s%s or --%s%s)",
143                                                 arg,
144                                                 (flags & OPT_UNSET) ?
145                                                         "no-" : "",
146                                                 options->long_name,
147                                                 (abbrev_flags & OPT_UNSET) ?
148                                                         "no-" : "",
149                                                 abbrev_option->long_name);
150                                 if (!(flags & OPT_UNSET) && *arg_end)
151                                         p->opt = arg_end + 1;
152                                 abbrev_option = options;
153                                 abbrev_flags = flags;
154                                 continue;
155                         }
156                         /* negated and abbreviated very much? */
157                         if (!prefixcmp("no-", arg)) {
158                                 flags |= OPT_UNSET;
159                                 goto is_abbreviated;
160                         }
161                         /* negated? */
162                         if (strncmp(arg, "no-", 3))
163                                 continue;
164                         flags |= OPT_UNSET;
165                         rest = skip_prefix(arg + 3, options->long_name);
166                         /* abbreviated and negated? */
167                         if (!rest && !prefixcmp(options->long_name, arg + 3))
168                                 goto is_abbreviated;
169                         if (!rest)
170                                 continue;
171                 }
172                 if (*rest) {
173                         if (*rest != '=')
174                                 continue;
175                         p->opt = rest + 1;
176                 }
177                 return get_value(p, options, flags);
178         }
179         if (abbrev_option)
180                 return get_value(p, abbrev_option, abbrev_flags);
181         return error("unknown option `%s'", arg);
182 }
183
184 int parse_options(int argc, const char **argv, const struct option *options,
185                   const char * const usagestr[], int flags)
186 {
187         struct optparse_t args = { argv + 1, argc - 1, NULL };
188         int j = 0;
189
190         for (; args.argc; args.argc--, args.argv++) {
191                 const char *arg = args.argv[0];
192
193                 if (*arg != '-' || !arg[1]) {
194                         argv[j++] = args.argv[0];
195                         continue;
196                 }
197
198                 if (arg[1] != '-') {
199                         args.opt = arg + 1;
200                         do {
201                                 if (*args.opt == 'h')
202                                         usage_with_options(usagestr, options);
203                                 if (parse_short_opt(&args, options) < 0)
204                                         usage_with_options(usagestr, options);
205                         } while (args.opt);
206                         continue;
207                 }
208
209                 if (!arg[2]) { /* "--" */
210                         if (!(flags & PARSE_OPT_KEEP_DASHDASH)) {
211                                 args.argc--;
212                                 args.argv++;
213                         }
214                         break;
215                 }
216
217                 if (!strcmp(arg + 2, "help"))
218                         usage_with_options(usagestr, options);
219                 if (parse_long_opt(&args, arg + 2, options))
220                         usage_with_options(usagestr, options);
221         }
222
223         memmove(argv + j, args.argv, args.argc * sizeof(*argv));
224         argv[j + args.argc] = NULL;
225         return j + args.argc;
226 }
227
228 #define USAGE_OPTS_WIDTH 24
229 #define USAGE_GAP         2
230
231 void usage_with_options(const char * const *usagestr,
232                         const struct option *opts)
233 {
234         fprintf(stderr, "usage: %s\n", *usagestr++);
235         while (*usagestr && **usagestr)
236                 fprintf(stderr, "   or: %s\n", *usagestr++);
237         while (*usagestr)
238                 fprintf(stderr, "    %s\n", *usagestr++);
239
240         if (opts->type != OPTION_GROUP)
241                 fputc('\n', stderr);
242
243         for (; opts->type != OPTION_END; opts++) {
244                 size_t pos;
245                 int pad;
246
247                 if (opts->type == OPTION_GROUP) {
248                         fputc('\n', stderr);
249                         if (*opts->help)
250                                 fprintf(stderr, "%s\n", opts->help);
251                         continue;
252                 }
253
254                 pos = fprintf(stderr, "    ");
255                 if (opts->short_name)
256                         pos += fprintf(stderr, "-%c", opts->short_name);
257                 if (opts->long_name && opts->short_name)
258                         pos += fprintf(stderr, ", ");
259                 if (opts->long_name)
260                         pos += fprintf(stderr, "--%s", opts->long_name);
261
262                 switch (opts->type) {
263                 case OPTION_INTEGER:
264                         if (opts->flags & PARSE_OPT_OPTARG)
265                                 pos += fprintf(stderr, " [<n>]");
266                         else
267                                 pos += fprintf(stderr, " <n>");
268                         break;
269                 case OPTION_CALLBACK:
270                         if (opts->flags & PARSE_OPT_NOARG)
271                                 break;
272                         /* FALLTHROUGH */
273                 case OPTION_STRING:
274                         if (opts->argh) {
275                                 if (opts->flags & PARSE_OPT_OPTARG)
276                                         pos += fprintf(stderr, " [<%s>]", opts->argh);
277                                 else
278                                         pos += fprintf(stderr, " <%s>", opts->argh);
279                         } else {
280                                 if (opts->flags & PARSE_OPT_OPTARG)
281                                         pos += fprintf(stderr, " [...]");
282                                 else
283                                         pos += fprintf(stderr, " ...");
284                         }
285                         break;
286                 default:
287                         break;
288                 }
289
290                 if (pos <= USAGE_OPTS_WIDTH)
291                         pad = USAGE_OPTS_WIDTH - pos;
292                 else {
293                         fputc('\n', stderr);
294                         pad = USAGE_OPTS_WIDTH;
295                 }
296                 fprintf(stderr, "%*s%s\n", pad + USAGE_GAP, "", opts->help);
297         }
298         fputc('\n', stderr);
299
300         exit(129);
301 }
302
303 /*----- some often used options -----*/
304 #include "cache.h"
305 int parse_opt_abbrev_cb(const struct option *opt, const char *arg, int unset)
306 {
307         int v;
308
309         if (!arg) {
310                 v = unset ? 0 : DEFAULT_ABBREV;
311         } else {
312                 v = strtol(arg, (char **)&arg, 10);
313                 if (*arg)
314                         return opterror(opt, "expects a numerical value", 0);
315                 if (v && v < MINIMUM_ABBREV)
316                         v = MINIMUM_ABBREV;
317                 else if (v > 40)
318                         v = 40;
319         }
320         *(int *)(opt->value) = v;
321         return 0;
322 }