Rework make_usage to print the usage message immediately
[git/git.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;
43
44 if (p->opt && (flags & OPT_UNSET))
45 return opterror(opt, "takes no value", flags);
46
47 switch (opt->type) {
48 case OPTION_BOOLEAN:
49 if (!(flags & OPT_SHORT) && p->opt)
50 return opterror(opt, "takes no value", flags);
51 if (flags & OPT_UNSET)
52 *(int *)opt->value = 0;
53 else
54 (*(int *)opt->value)++;
55 return 0;
56
57 case OPTION_STRING:
58 if (flags & OPT_UNSET) {
59 *(const char **)opt->value = (const char *)NULL;
60 return 0;
61 }
62 if (!p->opt && p->argc <= 1)
63 return opterror(opt, "requires a value", flags);
64 *(const char **)opt->value = get_arg(p);
65 return 0;
66
67 case OPTION_INTEGER:
68 if (flags & OPT_UNSET) {
69 *(int *)opt->value = 0;
70 return 0;
71 }
72 if (!p->opt && p->argc <= 1)
73 return opterror(opt, "requires a value", flags);
74 *(int *)opt->value = strtol(get_arg(p), (char **)&s, 10);
75 if (*s)
76 return opterror(opt, "expects a numerical value", flags);
77 return 0;
78
79 default:
80 die("should not happen, someone must be hit on the forehead");
81 }
82 }
83
84 static int parse_short_opt(struct optparse_t *p, const struct option *options)
85 {
86 for (; options->type != OPTION_END; options++) {
87 if (options->short_name == *p->opt) {
88 p->opt = p->opt[1] ? p->opt + 1 : NULL;
89 return get_value(p, options, OPT_SHORT);
90 }
91 }
92 return error("unknown switch `%c'", *p->opt);
93 }
94
95 static int parse_long_opt(struct optparse_t *p, const char *arg,
96 const struct option *options)
97 {
98 for (; options->type != OPTION_END; options++) {
99 const char *rest;
100 int flags = 0;
101
102 if (!options->long_name)
103 continue;
104
105 rest = skip_prefix(arg, options->long_name);
106 if (!rest) {
107 if (strncmp(arg, "no-", 3))
108 continue;
109 flags |= OPT_UNSET;
110 rest = skip_prefix(arg + 3, options->long_name);
111 if (!rest)
112 continue;
113 }
114 if (*rest) {
115 if (*rest != '=')
116 continue;
117 p->opt = rest + 1;
118 }
119 return get_value(p, options, flags);
120 }
121 return error("unknown option `%s'", arg);
122 }
123
124 int parse_options(int argc, const char **argv, const struct option *options,
125 const char * const usagestr[], int flags)
126 {
127 struct optparse_t args = { argv + 1, argc - 1, NULL };
128 int j = 0;
129
130 for (; args.argc; args.argc--, args.argv++) {
131 const char *arg = args.argv[0];
132
133 if (*arg != '-' || !arg[1]) {
134 argv[j++] = args.argv[0];
135 continue;
136 }
137
138 if (arg[1] != '-') {
139 args.opt = arg + 1;
140 do {
141 if (*args.opt == 'h')
142 usage_with_options(usagestr, options);
143 if (parse_short_opt(&args, options) < 0)
144 usage_with_options(usagestr, options);
145 } while (args.opt);
146 continue;
147 }
148
149 if (!arg[2]) { /* "--" */
150 if (!(flags & PARSE_OPT_KEEP_DASHDASH)) {
151 args.argc--;
152 args.argv++;
153 }
154 break;
155 }
156
157 if (!strcmp(arg + 2, "help"))
158 usage_with_options(usagestr, options);
159 if (parse_long_opt(&args, arg + 2, options))
160 usage_with_options(usagestr, options);
161 }
162
163 memmove(argv + j, args.argv, args.argc * sizeof(*argv));
164 argv[j + args.argc] = NULL;
165 return j + args.argc;
166 }
167
168 #define USAGE_OPTS_WIDTH 24
169 #define USAGE_GAP 2
170
171 void usage_with_options(const char * const *usagestr,
172 const struct option *opts)
173 {
174 fprintf(stderr, "usage: %s\n", *usagestr++);
175 while (*usagestr && **usagestr)
176 fprintf(stderr, " or: %s\n", *usagestr++);
177 while (*usagestr)
178 fprintf(stderr, " %s\n", *usagestr++);
179
180 if (opts->type != OPTION_GROUP)
181 fputc('\n', stderr);
182
183 for (; opts->type != OPTION_END; opts++) {
184 size_t pos;
185 int pad;
186
187 if (opts->type == OPTION_GROUP) {
188 fputc('\n', stderr);
189 if (*opts->help)
190 fprintf(stderr, "%s\n", opts->help);
191 continue;
192 }
193
194 pos = fprintf(stderr, " ");
195 if (opts->short_name)
196 pos += fprintf(stderr, "-%c", opts->short_name);
197 if (opts->long_name && opts->short_name)
198 pos += fprintf(stderr, ", ");
199 if (opts->long_name)
200 pos += fprintf(stderr, "--%s", opts->long_name);
201
202 switch (opts->type) {
203 case OPTION_INTEGER:
204 pos += fprintf(stderr, " <n>");
205 break;
206 case OPTION_STRING:
207 if (opts->argh)
208 pos += fprintf(stderr, " <%s>", opts->argh);
209 else
210 pos += fprintf(stderr, " ...");
211 break;
212 default:
213 break;
214 }
215
216 if (pos <= USAGE_OPTS_WIDTH)
217 pad = USAGE_OPTS_WIDTH - pos;
218 else {
219 fputc('\n', stderr);
220 pad = USAGE_OPTS_WIDTH;
221 }
222 fprintf(stderr, "%*s%s\n", pad + USAGE_GAP, "", opts->help);
223 }
224 fputc('\n', stderr);
225
226 exit(129);
227 }