Debian Codegraph @

ext/argparse/argparse.cpp

raw symbols SCIP
1 /**
2 * Copyright (C) 2012-2015 Yecheng Fu <cofyc.jackson at gmail dot com>
3 * All rights reserved.
4 *
5 * Use of this source code is governed by a MIT-style license that can be found
6 * in the LICENSE file.
7 */
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <assert.h>
12 #include <errno.h>
13 #include "argparse.h"
14
15 #define OPT_UNSET 1
16 #define OPT_LONG (1 << 1)
17
18 static const char *
19 prefix_skip(const char *str, const char *prefix)
20 {
21 size_t len = strlen(prefix);
22 return strncmp(str, prefix, len) ? NULL : str + len;
23 }
24
25 static int
26 prefix_cmp(const char *str, const char *prefix)
27 {
28 for (;; str++, prefix++)
29 if (!*prefix) {
30 return 0;
31 } else if (*str != *prefix) {
32 return (unsigned char)*prefix - (unsigned char)*str;
33 }
34 }
35
36 static void
37 argparse_error(struct argparse *self, const struct argparse_option *opt,
38 const char *reason, int flags)
39 {
40 (void)self;
41 if (flags & OPT_LONG) {
42 fprintf(stderr, "error: option `--%s` %s\n", opt->long_name, reason);
43 } else {
44 fprintf(stderr, "error: option `-%c` %s\n", opt->short_name, reason);
45 }
46 exit(EXIT_FAILURE);
47 }
48
49 static int
50 argparse_getvalue(struct argparse *self, const struct argparse_option *opt,
51 int flags)
52 {
53 const char *s = NULL;
54 if (!opt->value)
55 goto skipped;
56 switch (opt->type) {
57 case ARGPARSE_OPT_BOOLEAN:
58 if (flags & OPT_UNSET) {
59 *(int *)opt->value = *(int *)opt->value - 1;
60 } else {
61 *(int *)opt->value = *(int *)opt->value + 1;
62 }
63 if (*(int *)opt->value < 0) {
64 *(int *)opt->value = 0;
65 }
66 break;
67 case ARGPARSE_OPT_BIT:
68 if (flags & OPT_UNSET) {
69 *(int *)opt->value &= ~opt->data;
70 } else {
71 *(int *)opt->value |= opt->data;
72 }
73 break;
74 case ARGPARSE_OPT_STRING:
75 if (self->optvalue) {
76 *(const char **)opt->value = self->optvalue;
77 self->optvalue = NULL;
78 } else if (self->argc > 1) {
79 self->argc--;
80 *(const char **)opt->value = *++self->argv;
81 } else {
82 argparse_error(self, opt, "requires a value", flags);
83 }
84 break;
85 case ARGPARSE_OPT_INTEGER:
86 errno = 0;
87 if (self->optvalue) {
88 *(int *)opt->value = strtol(self->optvalue, (char **)&s, 0);
89 self->optvalue = NULL;
90 } else if (self->argc > 1) {
91 self->argc--;
92 *(int *)opt->value = strtol(*++self->argv, (char **)&s, 0);
93 } else {
94 argparse_error(self, opt, "requires a value", flags);
95 }
96 if (errno == ERANGE)
97 argparse_error(self, opt, "numerical result out of range", flags);
98 if (s[0] != '\0') // no digits or contains invalid characters
99 argparse_error(self, opt, "expects an integer value", flags);
100 break;
101 #ifdef ARGPARSE_HAVE_FLOAT
102 case ARGPARSE_OPT_FLOAT:
103 errno = 0;
104 if (self->optvalue) {
105 *(float *)opt->value = strtof(self->optvalue, (char **)&s);
106 self->optvalue = NULL;
107 } else if (self->argc > 1) {
108 self->argc--;
109 *(float *)opt->value = strtof(*++self->argv, (char **)&s);
110 } else {
111 argparse_error(self, opt, "requires a value", flags);
112 }
113 if (errno == ERANGE)
114 argparse_error(self, opt, "numerical result out of range", flags);
115 if (s[0] != '\0') // no digits or contains invalid characters
116 argparse_error(self, opt, "expects a numerical value", flags);
117 break;
118 #endif
119 default:
120 assert(0);
121 }
122
123 skipped:
124 if (opt->callback) {
125 return opt->callback(self, opt);
126 }
127
128 return 0;
129 }
130
131 static void
132 argparse_options_check(const struct argparse_option *options)
133 {
134 for (; options->type != ARGPARSE_OPT_END; options++) {
135 switch (options->type) {
136 case ARGPARSE_OPT_END:
137 case ARGPARSE_OPT_BOOLEAN:
138 case ARGPARSE_OPT_BIT:
139 case ARGPARSE_OPT_INTEGER:
140 #ifdef ARGPARSE_HAVE_FLOAT
141 case ARGPARSE_OPT_FLOAT:
142 #endif
143 case ARGPARSE_OPT_STRING:
144 case ARGPARSE_OPT_GROUP:
145 continue;
146 default:
147 fprintf(stderr, "wrong option type: %d", options->type);
148 break;
149 }
150 }
151 }
152
153 static int
154 argparse_short_opt(struct argparse *self, const struct argparse_option *options)
155 {
156 for (; options->type != ARGPARSE_OPT_END; options++) {
157 if (options->short_name == *self->optvalue) {
158 self->optvalue = self->optvalue[1] ? self->optvalue + 1 : NULL;
159 return argparse_getvalue(self, options, 0);
160 }
161 }
162 return -2;
163 }
164
165 static int
166 argparse_long_opt(struct argparse *self, const struct argparse_option *options)
167 {
168 for (; options->type != ARGPARSE_OPT_END; options++) {
169 const char *rest;
170 int opt_flags = 0;
171 if (!options->long_name)
172 continue;
173
174 rest = prefix_skip(self->argv[0] + 2, options->long_name);
175 if (!rest) {
176 // negation disabled?
177 if (options->flags & OPT_NONEG) {
178 continue;
179 }
180 // only OPT_BOOLEAN/OPT_BIT supports negation
181 if (options->type != ARGPARSE_OPT_BOOLEAN && options->type !=
182 ARGPARSE_OPT_BIT) {
183 continue;
184 }
185
186 if (prefix_cmp(self->argv[0] + 2, "no-")) {
187 continue;
188 }
189 rest = prefix_skip(self->argv[0] + 2 + 3, options->long_name);
190 if (!rest)
191 continue;
192 opt_flags |= OPT_UNSET;
193 }
194 if (*rest) {
195 if (*rest != '=')
196 continue;
197 self->optvalue = rest + 1;
198 }
199 return argparse_getvalue(self, options, opt_flags | OPT_LONG);
200 }
201 return -2;
202 }
203
204 int
205 argparse_init(struct argparse *self, struct argparse_option *options,
206 const char *const *usages, int flags)
207 {
208 memset(self, 0, sizeof(*self));
209 self->options = options;
210 self->usages = usages;
211 self->flags = flags;
212 self->description = NULL;
213 self->epilog = NULL;
214 return 0;
215 }
216
217 void
218 argparse_describe(struct argparse *self, const char *description,
219 const char *epilog)
220 {
221 self->description = description;
222 self->epilog = epilog;
223 }
224
225 int
226 argparse_parse(struct argparse *self, int argc, char **argv)
227 {
228 self->argc = argc - 1;
229 self->argv = argv + 1;
230 self->out = argv;
231
232 argparse_options_check(self->options);
233
234 for (; self->argc; self->argc--, self->argv++) {
235 const char *arg = self->argv[0];
236 if (arg[0] != '-' || !arg[1]) {
237 if (self->flags & ARGPARSE_STOP_AT_NON_OPTION) {
238 goto end;
239 }
240 // if it's not option or is a single char '-', copy verbatim
241 self->out[self->cpidx++] = self->argv[0];
242 continue;
243 }
244 // short option
245 if (arg[1] != '-') {
246 self->optvalue = arg + 1;
247 switch (argparse_short_opt(self, self->options)) {
248 case -1:
249 break;
250 case -2:
251 goto unknown;
252 }
253 while (self->optvalue) {
254 switch (argparse_short_opt(self, self->options)) {
255 case -1:
256 break;
257 case -2:
258 goto unknown;
259 }
260 }
261 continue;
262 }
263 // if '--' presents
264 if (!arg[2]) {
265 self->argc--;
266 self->argv++;
267 break;
268 }
269 // long option
270 switch (argparse_long_opt(self, self->options)) {
271 case -1:
272 break;
273 case -2:
274 goto unknown;
275 }
276 continue;
277
278 unknown:
279 fprintf(stderr, "error: unknown option `%s`\n", self->argv[0]);
280 argparse_usage(self);
281 if (!(self->flags & ARGPARSE_IGNORE_UNKNOWN_ARGS)) {
282 exit(EXIT_FAILURE);
283 }
284 }
285
286 end:
287 memmove(self->out + self->cpidx, self->argv,
288 self->argc * sizeof(*self->out));
289 self->out[self->cpidx + self->argc] = NULL;
290
291 return self->cpidx + self->argc;
292 }
293
294 void
295 argparse_usage(struct argparse *self)
296 {
297 if (self->usages) {
298 fprintf(stdout, "Usage: %s\n", *self->usages++);
299 while (*self->usages && **self->usages)
300 fprintf(stdout, " or: %s\n", *self->usages++);
301 } else {
302 fprintf(stdout, "Usage:\n");
303 }
304
305 // print description
306 if (self->description)
307 fprintf(stdout, "%s\n", self->description);
308
309 fputc('\n', stdout);
310
311 const struct argparse_option *options;
312
313 // figure out best width
314 size_t usage_opts_width = 0;
315 size_t len;
316 options = self->options;
317 for (; options->type != ARGPARSE_OPT_END; options++) {
318 len = 0;
319 if ((options)->short_name) {
320 len += 2;
321 }
322 if ((options)->short_name && (options)->long_name) {
323 len += 2; // separator ", "
324 }
325 if ((options)->long_name) {
326 len += strlen((options)->long_name) + 2;
327 }
328 if (options->type == ARGPARSE_OPT_INTEGER) {
329 len += strlen("=<int>");
330 #ifdef ARGPARSE_HAVE_FLOAT
331 } else if (options->type == ARGPARSE_OPT_FLOAT) {
332 len += strlen("=<flt>");
333 #endif
334 } else if (options->type == ARGPARSE_OPT_STRING) {
335 len += strlen("=<str>");
336 }
337 len = (len + 3) - ((len + 3) & 3);
338 if (usage_opts_width < len) {
339 usage_opts_width = len;
340 }
341 }
342 usage_opts_width += 4; // 4 spaces prefix
343
344 options = self->options;
345 for (; options->type != ARGPARSE_OPT_END; options++) {
346 size_t pos = 0;
347 size_t pad = 0;
348 if (options->type == ARGPARSE_OPT_GROUP) {
349 fputc('\n', stdout);
350 fprintf(stdout, "%s", options->help);
351 fputc('\n', stdout);
352 continue;
353 }
354 pos = fprintf(stdout, " ");
355 if (options->short_name) {
356 pos += fprintf(stdout, "-%c", options->short_name);
357 }
358 if (options->long_name && options->short_name) {
359 pos += fprintf(stdout, ", ");
360 }
361 if (options->long_name) {
362 pos += fprintf(stdout, "--%s", options->long_name);
363 }
364 if (options->type == ARGPARSE_OPT_INTEGER) {
365 pos += fprintf(stdout, "=<int>");
366 #ifdef ARGPARSE_HAVE_FLOAT
367 } else if (options->type == ARGPARSE_OPT_FLOAT) {
368 pos += fprintf(stdout, "=<flt>");
369 #endif
370 } else if (options->type == ARGPARSE_OPT_STRING) {
371 pos += fprintf(stdout, "=<str>");
372 }
373 if (pos <= usage_opts_width) {
374 pad = usage_opts_width - pos;
375 } else {
376 fputc('\n', stdout);
377 pad = usage_opts_width;
378 }
379 fprintf(stdout, "%*s%s\n", (int)pad + 2, "", options->help);
380 }
381
382 // print epilog
383 if (self->epilog)
384 fprintf(stdout, "%s\n", self->epilog);
385 }
386
387 int
388 argparse_help_cb(struct argparse *self, const struct argparse_option *option)
389 {
390 (void)option;
391 argparse_usage(self);
392 exit(EXIT_SUCCESS);
393 }