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