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"cxx . . $ `<file>/ext/argparse/argparse.h`/
14
15 #define OPT_UNSETcxx . . $ `ext/argparse/argparse.cpp:15:9`! 1
16 #define OPT_LONGcxx . . $ `ext/argparse/argparse.cpp:16:9`! (1 << 1)
17
18 static const char *
19 prefix_skipcxx . . $ prefix_skip(1320557f0feba62c).(const char *str, const char *prefix)
20 {
21 size_t len = strlen(prefix);
22 return strncmp(str, prefix, len) ? NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`! : str + len;
23 }
24
25 static int
26 prefix_cmpcxx . . $ prefix_cmp(5ae29d726ae57f88).(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_errorcxx . . $ argparse_error(858be8501a2d4911).(struct argparse *self, const struct argparse_option *opt,
38 const char *reason, int flags)
39 {
40 (void)self;
41 if (flags & OPT_LONGcxx . . $ `ext/argparse/argparse.cpp:16:9`!) {
42 fprintf(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "error: option `--%s` %s\n", opt->long_name, reason);
43 } else {
44 fprintf(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "error: option `-%c` %s\n", opt->short_name, reason);
45 }
46 exitcxx . . $ exit(3ca292c338df9bb3).(EXIT_FAILUREcxx . . $ `<external>/10696550936717951914/stdlib.h:92:9`!);
47 }
48
49 static int
50 argparse_getvaluecxx . . $ argparse_getvalue(bac026afe5a9a558).(struct argparse *self, const struct argparse_option *opt,
51 int flags)
52 {
53 const char *s = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!;
54 if (!opt->value)
55 goto skipped;
56 switch (opt->type) {
57 case ARGPARSE_OPT_BOOLEAN:
58 if (flags & OPT_UNSETcxx . . $ `ext/argparse/argparse.cpp:15:9`!) {
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_UNSETcxx . . $ `ext/argparse/argparse.cpp:15:9`!) {
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 = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!;
78 } else if (self->argc > 1) {
79 self->argc--;
80 *(const char **)opt->value = *++self->argv;
81 } else {
82 argparse_errorcxx . . $ argparse_error(858be8501a2d4911).(self, opt, "requires a value", flags);
83 }
84 break;
85 case ARGPARSE_OPT_INTEGER:
86 errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! = 0;
87 if (self->optvalue) {
88 *(int *)opt->value = strtolcxx . . $ strtol(44bbb87609b212fc).(self->optvalue, (char **)&s, 0);
89 self->optvalue = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!;
90 } else if (self->argc > 1) {
91 self->argc--;
92 *(int *)opt->value = strtolcxx . . $ strtol(44bbb87609b212fc).(*++self->argv, (char **)&s, 0);
93 } else {
94 argparse_errorcxx . . $ argparse_error(858be8501a2d4911).(self, opt, "requires a value", flags);
95 }
96 if (errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! == ERANGEcxx . . $ `<external>/8987895939074593727/errno-base.h:38:9`!)
97 argparse_errorcxx . . $ argparse_error(858be8501a2d4911).(self, opt, "numerical result out of range", flags);
98 if (s[0] != '\0') // no digits or contains invalid characters
99 argparse_errorcxx . . $ argparse_error(858be8501a2d4911).(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 assertcxx . . $ `<external>/4398592474888995393/assert.h:102:11`!(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_checkcxx . . $ argparse_options_check(2132a1dc2f909d18).(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(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "wrong option type: %d", options->type);
148 break;
149 }
150 }
151 }
152
153 static int
154 argparse_short_optcxx . . $ argparse_short_opt(167e068defbc3e3b).(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 : NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!;
159 return argparse_getvaluecxx . . $ argparse_getvalue(bac026afe5a9a558).(self, options, 0);
160 }
161 }
162 return -2;
163 }
164
165 static int
166 argparse_long_optcxx . . $ argparse_long_opt(167e068defbc3e3b).(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_skipcxx . . $ prefix_skip(1320557f0feba62c).(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_cmpcxx . . $ prefix_cmp(5ae29d726ae57f88).(self->argv[0] + 2, "no-")) {
187 continue;
188 }
189 rest = prefix_skipcxx . . $ prefix_skip(1320557f0feba62c).(self->argv[0] + 2 + 3, options->long_name);
190 if (!rest)
191 continue;
192 opt_flags |= OPT_UNSETcxx . . $ `ext/argparse/argparse.cpp:15:9`!;
193 }
194 if (*rest) {
195 if (*rest != '=')
196 continue;
197 self->optvalue = rest + 1;
198 }
199 return argparse_getvaluecxx . . $ argparse_getvalue(bac026afe5a9a558).(self, options, opt_flags | OPT_LONGcxx . . $ `ext/argparse/argparse.cpp:16:9`!);
200 }
201 return -2;
202 }
203
204 int
205 argparse_initcxx . . $ argparse_init(627cb765b488dd45).(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 = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!;
213 self->epilog = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!;
214 return 0;
215 }
216
217 void
218 argparse_describecxx . . $ argparse_describe(e0fafad7e4aef4).(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_parsecxx . . $ argparse_parse(a29334178e91b968).(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_checkcxx . . $ argparse_options_check(2132a1dc2f909d18).(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_optcxx . . $ argparse_short_opt(167e068defbc3e3b).(self, self->options)) {
248 case -1:
249 break;
250 case -2:
251 goto unknown;
252 }
253 while (self->optvalue) {
254 switch (argparse_short_optcxx . . $ argparse_short_opt(167e068defbc3e3b).(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_optcxx . . $ argparse_long_opt(167e068defbc3e3b).(self, self->options)) {
271 case -1:
272 break;
273 case -2:
274 goto unknown;
275 }
276 continue;
277
278 unknown:
279 fprintf(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "error: unknown option `%s`\n", self->argv[0]);
280 argparse_usage(self);
281 if (!(self->flags & ARGPARSE_IGNORE_UNKNOWN_ARGS)) {
282 exitcxx . . $ exit(3ca292c338df9bb3).(EXIT_FAILUREcxx . . $ `<external>/10696550936717951914/stdlib.h:92:9`!);
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] = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!;
290
291 return self->cpidx + self->argc;
292 }
293
294 void
295 argparse_usagecxx . . $ argparse_usage(2c54ce3b9ca6f83c).(struct argparse *self)
296 {
297 if (self->usages) {
298 fprintf(stdoutcxx . . $ `<external>/232824719582829767/stdio.h:154:9`!, "Usage: %s\n", *self->usages++);
299 while (*self->usages && **self->usages)
300 fprintf(stdoutcxx . . $ `<external>/232824719582829767/stdio.h:154:9`!, " or: %s\n", *self->usages++);
301 } else {
302 fprintf(stdoutcxx . . $ `<external>/232824719582829767/stdio.h:154:9`!, "Usage:\n");
303 }
304
305 // print description
306 if (self->description)
307 fprintf(stdoutcxx . . $ `<external>/232824719582829767/stdio.h:154:9`!, "%s\n", self->description);
308
309 fputc('\n', stdoutcxx . . $ `<external>/232824719582829767/stdio.h:154:9`!);
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', stdoutcxx . . $ `<external>/232824719582829767/stdio.h:154:9`!);
350 fprintf(stdoutcxx . . $ `<external>/232824719582829767/stdio.h:154:9`!, "%s", options->help);
351 fputc('\n', stdoutcxx . . $ `<external>/232824719582829767/stdio.h:154:9`!);
352 continue;
353 }
354 pos = fprintf(stdoutcxx . . $ `<external>/232824719582829767/stdio.h:154:9`!, " ");
355 if (options->short_name) {
356 pos += fprintf(stdoutcxx . . $ `<external>/232824719582829767/stdio.h:154:9`!, "-%c", options->short_name);
357 }
358 if (options->long_name && options->short_name) {
359 pos += fprintf(stdoutcxx . . $ `<external>/232824719582829767/stdio.h:154:9`!, ", ");
360 }
361 if (options->long_name) {
362 pos += fprintf(stdoutcxx . . $ `<external>/232824719582829767/stdio.h:154:9`!, "--%s", options->long_name);
363 }
364 if (options->type == ARGPARSE_OPT_INTEGER) {
365 pos += fprintf(stdoutcxx . . $ `<external>/232824719582829767/stdio.h:154:9`!, "=<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(stdoutcxx . . $ `<external>/232824719582829767/stdio.h:154:9`!, "=<str>");
372 }
373 if (pos <= usage_opts_width) {
374 pad = usage_opts_width - pos;
375 } else {
376 fputc('\n', stdoutcxx . . $ `<external>/232824719582829767/stdio.h:154:9`!);
377 pad = usage_opts_width;
378 }
379 fprintf(stdoutcxx . . $ `<external>/232824719582829767/stdio.h:154:9`!, "%*s%s\n", (int)pad + 2, "", options->help);
380 }
381
382 // print epilog
383 if (self->epilog)
384 fprintf(stdoutcxx . . $ `<external>/232824719582829767/stdio.h:154:9`!, "%s\n", self->epilog);
385 }
386
387 int
388 argparse_help_cbcxx . . $ argparse_help_cb(167e068defbc3e3b).(struct argparse *self, const struct argparse_option *option)
389 {
390 (void)option;
391 argparse_usagecxx . . $ argparse_usage(2c54ce3b9ca6f83c).(self);
392 exitcxx . . $ exit(3ca292c338df9bb3).(EXIT_SUCCESScxx . . $ `<external>/10696550936717951914/stdlib.h:93:9`!);
393 }