Add shortcuts for very often used options.
[git/git.git] / parse-options.c
CommitLineData
4a59fd13
PH
1#include "git-compat-util.h"
2#include "parse-options.h"
4a59fd13
PH
3
4#define OPT_SHORT 1
5#define OPT_UNSET 2
6
7struct optparse_t {
8 const char **argv;
9 int argc;
10 const char *opt;
11};
12
13static 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
24static 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
30static 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
39static int get_value(struct optparse_t *p,
40 const struct option *opt, int flags)
41{
ffe659f9
PH
42 const char *s, *arg;
43 arg = p->opt ? p->opt : (p->argc > 1 ? p->argv[1] : NULL);
4a59fd13
PH
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 }
ffe659f9
PH
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)
4a59fd13
PH
68 return opterror(opt, "requires a value", flags);
69 *(const char **)opt->value = get_arg(p);
70 return 0;
71
ffe659f9
PH
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);
80
4a59fd13
PH
81 case OPTION_INTEGER:
82 if (flags & OPT_UNSET) {
83 *(int *)opt->value = 0;
84 return 0;
85 }
ffe659f9
PH
86 if (opt->flags & PARSE_OPT_OPTARG && (!arg || !isdigit(*arg))) {
87 *(int *)opt->value = opt->defval;
88 return 0;
89 }
90 if (!arg)
4a59fd13
PH
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;
96
97 default:
98 die("should not happen, someone must be hit on the forehead");
99 }
100}
101
102static int parse_short_opt(struct optparse_t *p, const struct option *options)
103{
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);
108 }
109 }
110 return error("unknown switch `%c'", *p->opt);
111}
112
113static int parse_long_opt(struct optparse_t *p, const char *arg,
114 const struct option *options)
115{
116 for (; options->type != OPTION_END; options++) {
117 const char *rest;
118 int flags = 0;
119
120 if (!options->long_name)
121 continue;
122
123 rest = skip_prefix(arg, options->long_name);
124 if (!rest) {
125 if (strncmp(arg, "no-", 3))
126 continue;
127 flags |= OPT_UNSET;
128 rest = skip_prefix(arg + 3, options->long_name);
129 if (!rest)
130 continue;
131 }
132 if (*rest) {
133 if (*rest != '=')
134 continue;
135 p->opt = rest + 1;
136 }
137 return get_value(p, options, flags);
138 }
139 return error("unknown option `%s'", arg);
140}
141
142int parse_options(int argc, const char **argv, const struct option *options,
d7a38c54 143 const char * const usagestr[], int flags)
4a59fd13
PH
144{
145 struct optparse_t args = { argv + 1, argc - 1, NULL };
146 int j = 0;
147
148 for (; args.argc; args.argc--, args.argv++) {
149 const char *arg = args.argv[0];
150
151 if (*arg != '-' || !arg[1]) {
152 argv[j++] = args.argv[0];
153 continue;
154 }
155
156 if (arg[1] != '-') {
157 args.opt = arg + 1;
158 do {
159 if (*args.opt == 'h')
d7a38c54 160 usage_with_options(usagestr, options);
4a59fd13 161 if (parse_short_opt(&args, options) < 0)
d7a38c54 162 usage_with_options(usagestr, options);
4a59fd13
PH
163 } while (args.opt);
164 continue;
165 }
166
167 if (!arg[2]) { /* "--" */
168 if (!(flags & PARSE_OPT_KEEP_DASHDASH)) {
169 args.argc--;
170 args.argv++;
171 }
172 break;
173 }
174
175 if (!strcmp(arg + 2, "help"))
d7a38c54 176 usage_with_options(usagestr, options);
4a59fd13 177 if (parse_long_opt(&args, arg + 2, options))
d7a38c54 178 usage_with_options(usagestr, options);
4a59fd13
PH
179 }
180
181 memmove(argv + j, args.argv, args.argc * sizeof(*argv));
182 argv[j + args.argc] = NULL;
183 return j + args.argc;
184}
d7a38c54
PH
185
186#define USAGE_OPTS_WIDTH 24
187#define USAGE_GAP 2
188
189void usage_with_options(const char * const *usagestr,
190 const struct option *opts)
191{
f389c808
AR
192 fprintf(stderr, "usage: %s\n", *usagestr++);
193 while (*usagestr && **usagestr)
194 fprintf(stderr, " or: %s\n", *usagestr++);
195 while (*usagestr)
196 fprintf(stderr, " %s\n", *usagestr++);
d7a38c54
PH
197
198 if (opts->type != OPTION_GROUP)
f389c808 199 fputc('\n', stderr);
d7a38c54
PH
200
201 for (; opts->type != OPTION_END; opts++) {
202 size_t pos;
203 int pad;
204
205 if (opts->type == OPTION_GROUP) {
f389c808 206 fputc('\n', stderr);
d7a38c54 207 if (*opts->help)
f389c808 208 fprintf(stderr, "%s\n", opts->help);
d7a38c54
PH
209 continue;
210 }
211
f389c808 212 pos = fprintf(stderr, " ");
d7a38c54 213 if (opts->short_name)
f389c808 214 pos += fprintf(stderr, "-%c", opts->short_name);
d7a38c54 215 if (opts->long_name && opts->short_name)
f389c808 216 pos += fprintf(stderr, ", ");
d7a38c54 217 if (opts->long_name)
f389c808 218 pos += fprintf(stderr, "--%s", opts->long_name);
d7a38c54
PH
219
220 switch (opts->type) {
221 case OPTION_INTEGER:
ffe659f9
PH
222 if (opts->flags & PARSE_OPT_OPTARG)
223 pos += fprintf(stderr, " [<n>]");
224 else
225 pos += fprintf(stderr, " <n>");
d7a38c54
PH
226 break;
227 case OPTION_STRING:
ffe659f9
PH
228 case OPTION_CALLBACK:
229 if (opts->argh) {
230 if (opts->flags & PARSE_OPT_OPTARG)
231 pos += fprintf(stderr, " [<%s>]", opts->argh);
232 else
233 pos += fprintf(stderr, " <%s>", opts->argh);
234 } else {
235 if (opts->flags & PARSE_OPT_OPTARG)
236 pos += fprintf(stderr, " [...]");
237 else
238 pos += fprintf(stderr, " ...");
239 }
d7a38c54
PH
240 break;
241 default:
242 break;
243 }
244
f389c808
AR
245 if (pos <= USAGE_OPTS_WIDTH)
246 pad = USAGE_OPTS_WIDTH - pos;
d7a38c54 247 else {
f389c808 248 fputc('\n', stderr);
d7a38c54
PH
249 pad = USAGE_OPTS_WIDTH;
250 }
f389c808 251 fprintf(stderr, "%*s%s\n", pad + USAGE_GAP, "", opts->help);
d7a38c54 252 }
f389c808
AR
253 fputc('\n', stderr);
254
255 exit(129);
d7a38c54 256}
0ce865b1
PH
257
258/*----- some often used options -----*/
259#include "cache.h"
260int parse_opt_abbrev_cb(const struct option *opt, const char *arg, int unset)
261{
262 int v;
263
264 if (!arg) {
265 v = unset ? 0 : DEFAULT_ABBREV;
266 } else {
267 v = strtol(arg, (char **)&arg, 10);
268 if (*arg)
269 return opterror(opt, "expects a numerical value", 0);
270 if (v && v < MINIMUM_ABBREV)
271 v = MINIMUM_ABBREV;
272 else if (v > 40)
273 v = 40;
274 }
275 *(int *)(opt->value) = v;
276 return 0;
277}