Skip to content

Commit 1cc6985

Browse files
MadCodergitster
authored andcommitted
parse-options: add PARSE_OPT_LASTARG_DEFAULT flag
If you set this for a given option, and the optoin appears without an argument on the command line, then the `defval' is used as its argument. Note that this flag is meaningless in presence of OPTARG or NOARG flags. (in the current implementation it will be ignored, but don't rely on it). Signed-off-by: Pierre Habouzit <madcoder@debian.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 3f8d520 commit 1cc6985

File tree

2 files changed

+28
-28
lines changed

2 files changed

+28
-28
lines changed

parse-options.c

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,6 @@
55
#define OPT_SHORT 1
66
#define OPT_UNSET 2
77

8-
static inline const char *get_arg(struct parse_opt_ctx_t *p)
9-
{
10-
if (p->opt) {
11-
const char *res = p->opt;
12-
p->opt = NULL;
13-
return res;
14-
}
15-
p->argc--;
16-
return *++p->argv;
17-
}
18-
198
static inline const char *skip_prefix(const char *str, const char *prefix)
209
{
2110
size_t len = strlen(prefix);
@@ -31,8 +20,24 @@ static int opterror(const struct option *opt, const char *reason, int flags)
3120
return error("option `%s' %s", opt->long_name, reason);
3221
}
3322

23+
static int get_arg(struct parse_opt_ctx_t *p, const struct option *opt,
24+
int flags, const char **arg)
25+
{
26+
if (p->opt) {
27+
*arg = p->opt;
28+
p->opt = NULL;
29+
} else if (p->argc == 1 && (opt->flags & PARSE_OPT_LASTARG_DEFAULT)) {
30+
*arg = (const char *)opt->defval;
31+
} else if (p->argc) {
32+
p->argc--;
33+
*arg = *++p->argv;
34+
} else
35+
return opterror(opt, "requires a value", flags);
36+
return 0;
37+
}
38+
3439
static int get_value(struct parse_opt_ctx_t *p,
35-
const struct option *opt, int flags)
40+
const struct option *opt, int flags)
3641
{
3742
const char *s, *arg;
3843
const int unset = flags & OPT_UNSET;
@@ -58,7 +63,6 @@ static int get_value(struct parse_opt_ctx_t *p,
5863
}
5964
}
6065

61-
arg = p->opt ? p->opt : (p->argc > 1 ? p->argv[1] : NULL);
6266
switch (opt->type) {
6367
case OPTION_BIT:
6468
if (unset)
@@ -80,17 +84,12 @@ static int get_value(struct parse_opt_ctx_t *p,
8084
return 0;
8185

8286
case OPTION_STRING:
83-
if (unset) {
87+
if (unset)
8488
*(const char **)opt->value = NULL;
85-
return 0;
86-
}
87-
if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
89+
else if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
8890
*(const char **)opt->value = (const char *)opt->defval;
89-
return 0;
90-
}
91-
if (!arg)
92-
return opterror(opt, "requires a value", flags);
93-
*(const char **)opt->value = get_arg(p);
91+
else
92+
return get_arg(p, opt, flags, (const char **)opt->value);
9493
return 0;
9594

9695
case OPTION_CALLBACK:
@@ -100,9 +99,9 @@ static int get_value(struct parse_opt_ctx_t *p,
10099
return (*opt->callback)(opt, NULL, 0) ? (-1) : 0;
101100
if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
102101
return (*opt->callback)(opt, NULL, 0) ? (-1) : 0;
103-
if (!arg)
104-
return opterror(opt, "requires a value", flags);
105-
return (*opt->callback)(opt, get_arg(p), 0) ? (-1) : 0;
102+
if (get_arg(p, opt, flags, &arg))
103+
return -1;
104+
return (*opt->callback)(opt, arg, 0) ? (-1) : 0;
106105

107106
case OPTION_INTEGER:
108107
if (unset) {
@@ -113,9 +112,9 @@ static int get_value(struct parse_opt_ctx_t *p,
113112
*(int *)opt->value = opt->defval;
114113
return 0;
115114
}
116-
if (!arg)
117-
return opterror(opt, "requires a value", flags);
118-
*(int *)opt->value = strtol(get_arg(p), (char **)&s, 10);
115+
if (get_arg(p, opt, flags, &arg))
116+
return -1;
117+
*(int *)opt->value = strtol(arg, (char **)&s, 10);
119118
if (*s)
120119
return opterror(opt, "expects a numerical value", flags);
121120
return 0;

parse-options.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ enum parse_opt_option_flags {
2828
PARSE_OPT_NOARG = 2,
2929
PARSE_OPT_NONEG = 4,
3030
PARSE_OPT_HIDDEN = 8,
31+
PARSE_OPT_LASTARG_DEFAULT = 16,
3132
};
3233

3334
struct option;

0 commit comments

Comments
 (0)