Skip to content

Commit 4a59fd1

Browse files
MadCodergitster
authored andcommitted
Add a simple option parser.
The option parser takes argc, argv, an array of struct option and a usage string. Each of the struct option elements in the array describes a valid option, its type and a pointer to the location where the value is written. The entry point is parse_options(), which scans through the given argv, and matches each option there against the list of valid options. During the scan, argv is rewritten to only contain the non-option command line arguments and the number of these is returned. Aggregation of single switches is allowed: -rC0 is the same as -r -C 0 (supposing that -C wants an arg). Every long option automatically support the option with the same name, prefixed with 'no-' to unset the switch. It assumes that initial value for strings are "NULL" and for integers is "0". Long options are supported either with '=' or without: --some-option=foo is the same as --some-option foo Acked-by: Kristian Høgsberg <[email protected]> Signed-off-by: Pierre Habouzit <[email protected]> Signed-off-by: Shawn O. Pearce <[email protected]>
1 parent 09149c7 commit 4a59fd1

File tree

3 files changed

+204
-2
lines changed

3 files changed

+204
-2
lines changed

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ LIB_H = \
290290
run-command.h strbuf.h tag.h tree.h git-compat-util.h revision.h \
291291
tree-walk.h log-tree.h dir.h path-list.h unpack-trees.h builtin.h \
292292
utf8.h reflog-walk.h patch-ids.h attr.h decorate.h progress.h \
293-
mailmap.h remote.h transport.h
293+
mailmap.h remote.h parse-options.h transport.h
294294

295295
DIFF_OBJS = \
296296
diff.o diff-lib.o diffcore-break.o diffcore-order.o \
@@ -313,7 +313,7 @@ LIB_OBJS = \
313313
alloc.o merge-file.o path-list.o help.o unpack-trees.o $(DIFF_OBJS) \
314314
color.o wt-status.o archive-zip.o archive-tar.o shallow.o utf8.o \
315315
convert.o attr.o decorate.o progress.o mailmap.o symlinks.o remote.o \
316-
transport.o bundle.o walker.o
316+
transport.o bundle.o walker.o parse-options.o
317317

318318
BUILTIN_OBJS = \
319319
builtin-add.o \

parse-options.c

Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
#include "git-compat-util.h"
2+
#include "parse-options.h"
3+
#include "strbuf.h"
4+
5+
#define OPT_SHORT 1
6+
#define OPT_UNSET 2
7+
8+
struct optparse_t {
9+
const char **argv;
10+
int argc;
11+
const char *opt;
12+
};
13+
14+
static inline const char *get_arg(struct optparse_t *p)
15+
{
16+
if (p->opt) {
17+
const char *res = p->opt;
18+
p->opt = NULL;
19+
return res;
20+
}
21+
p->argc--;
22+
return *++p->argv;
23+
}
24+
25+
static inline const char *skip_prefix(const char *str, const char *prefix)
26+
{
27+
size_t len = strlen(prefix);
28+
return strncmp(str, prefix, len) ? NULL : str + len;
29+
}
30+
31+
static int opterror(const struct option *opt, const char *reason, int flags)
32+
{
33+
if (flags & OPT_SHORT)
34+
return error("switch `%c' %s", opt->short_name, reason);
35+
if (flags & OPT_UNSET)
36+
return error("option `no-%s' %s", opt->long_name, reason);
37+
return error("option `%s' %s", opt->long_name, reason);
38+
}
39+
40+
static int get_value(struct optparse_t *p,
41+
const struct option *opt, int flags)
42+
{
43+
const char *s;
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+
}
63+
if (!p->opt && p->argc <= 1)
64+
return opterror(opt, "requires a value", flags);
65+
*(const char **)opt->value = get_arg(p);
66+
return 0;
67+
68+
case OPTION_INTEGER:
69+
if (flags & OPT_UNSET) {
70+
*(int *)opt->value = 0;
71+
return 0;
72+
}
73+
if (!p->opt && p->argc <= 1)
74+
return opterror(opt, "requires a value", flags);
75+
*(int *)opt->value = strtol(get_arg(p), (char **)&s, 10);
76+
if (*s)
77+
return opterror(opt, "expects a numerical value", flags);
78+
return 0;
79+
80+
default:
81+
die("should not happen, someone must be hit on the forehead");
82+
}
83+
}
84+
85+
static int parse_short_opt(struct optparse_t *p, const struct option *options)
86+
{
87+
for (; options->type != OPTION_END; options++) {
88+
if (options->short_name == *p->opt) {
89+
p->opt = p->opt[1] ? p->opt + 1 : NULL;
90+
return get_value(p, options, OPT_SHORT);
91+
}
92+
}
93+
return error("unknown switch `%c'", *p->opt);
94+
}
95+
96+
static int parse_long_opt(struct optparse_t *p, const char *arg,
97+
const struct option *options)
98+
{
99+
for (; options->type != OPTION_END; options++) {
100+
const char *rest;
101+
int flags = 0;
102+
103+
if (!options->long_name)
104+
continue;
105+
106+
rest = skip_prefix(arg, options->long_name);
107+
if (!rest) {
108+
if (strncmp(arg, "no-", 3))
109+
continue;
110+
flags |= OPT_UNSET;
111+
rest = skip_prefix(arg + 3, options->long_name);
112+
if (!rest)
113+
continue;
114+
}
115+
if (*rest) {
116+
if (*rest != '=')
117+
continue;
118+
p->opt = rest + 1;
119+
}
120+
return get_value(p, options, flags);
121+
}
122+
return error("unknown option `%s'", arg);
123+
}
124+
125+
int parse_options(int argc, const char **argv, const struct option *options,
126+
const char *usagestr, int flags)
127+
{
128+
struct optparse_t args = { argv + 1, argc - 1, NULL };
129+
int j = 0;
130+
131+
for (; args.argc; args.argc--, args.argv++) {
132+
const char *arg = args.argv[0];
133+
134+
if (*arg != '-' || !arg[1]) {
135+
argv[j++] = args.argv[0];
136+
continue;
137+
}
138+
139+
if (arg[1] != '-') {
140+
args.opt = arg + 1;
141+
do {
142+
if (*args.opt == 'h')
143+
usage(usagestr);
144+
if (parse_short_opt(&args, options) < 0)
145+
usage(usagestr);
146+
} while (args.opt);
147+
continue;
148+
}
149+
150+
if (!arg[2]) { /* "--" */
151+
if (!(flags & PARSE_OPT_KEEP_DASHDASH)) {
152+
args.argc--;
153+
args.argv++;
154+
}
155+
break;
156+
}
157+
158+
if (!strcmp(arg + 2, "help"))
159+
usage(usagestr);
160+
if (parse_long_opt(&args, arg + 2, options))
161+
usage(usagestr);
162+
}
163+
164+
memmove(argv + j, args.argv, args.argc * sizeof(*argv));
165+
argv[j + args.argc] = NULL;
166+
return j + args.argc;
167+
}

parse-options.h

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#ifndef PARSE_OPTIONS_H
2+
#define PARSE_OPTIONS_H
3+
4+
enum parse_opt_type {
5+
OPTION_END,
6+
OPTION_BOOLEAN,
7+
OPTION_STRING,
8+
OPTION_INTEGER,
9+
};
10+
11+
enum parse_opt_flags {
12+
PARSE_OPT_KEEP_DASHDASH = 1,
13+
};
14+
15+
struct option {
16+
enum parse_opt_type type;
17+
int short_name;
18+
const char *long_name;
19+
void *value;
20+
};
21+
22+
#define OPT_END() { OPTION_END }
23+
#define OPT_BOOLEAN(s, l, v, h) { OPTION_BOOLEAN, (s), (l), (v) }
24+
#define OPT_INTEGER(s, l, v, h) { OPTION_INTEGER, (s), (l), (v) }
25+
#define OPT_STRING(s, l, v, a, h) { OPTION_STRING, (s), (l), (v) }
26+
27+
/* parse_options() will filter out the processed options and leave the
28+
* non-option argments in argv[].
29+
* Returns the number of arguments left in argv[].
30+
*/
31+
extern int parse_options(int argc, const char **argv,
32+
const struct option *options,
33+
const char *usagestr, int flags);
34+
35+
#endif

0 commit comments

Comments
 (0)