@matheusbsbs / matheus-bagliano / commits / 1cc6985ca7
Fork 0

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>

Pierre Habouzit committed Jul 8, 2008 at 12:34 UTC 1cc6985ca7dd3aaab0617ec0fd00d4eb0b424465
2 files changed +28 -28
parse-options.c
+27 -28
@@ -5,17 +5,6 @@
5 #define OPT_SHORT 1
6 #define OPT_UNSET 2
7
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 -
8 static inline const char *skip_prefix(const char *str, const char *prefix)
9 {
10 size_t len = strlen(prefix);
@@ -31,8 +20,24 @@ static int opterror(const struct option *opt, const char *reason, int flags)
20 return error("option `%s' %s", opt->long_name, reason);
21 }
22
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 +
39 static int get_value(struct parse_opt_ctx_t *p,
35 - const struct option *opt, int flags)
40 + const struct option *opt, int flags)
41 {
42 const char *s, *arg;
43 const int unset = flags & OPT_UNSET;
@@ -58,7 +63,6 @@ static int get_value(struct parse_opt_ctx_t *p,
63 }
64 }
65
61 - arg = p->opt ? p->opt : (p->argc > 1 ? p->argv[1] : NULL);
66 switch (opt->type) {
67 case OPTION_BIT:
68 if (unset)
@@ -80,17 +84,12 @@ static int get_value(struct parse_opt_ctx_t *p,
84 return 0;
85
86 case OPTION_STRING:
83 - if (unset) {
87 + if (unset)
88 *(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)
90 *(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);
93 return 0;
94
95 case OPTION_CALLBACK:
@@ -100,9 +99,9 @@ static int get_value(struct parse_opt_ctx_t *p,
99 return (*opt->callback)(opt, NULL, 0) ? (-1) : 0;
100 if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
101 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;
105
106 case OPTION_INTEGER:
107 if (unset) {
@@ -113,9 +112,9 @@ static int get_value(struct parse_opt_ctx_t *p,
112 *(int *)opt->value = opt->defval;
113 return 0;
114 }
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);
118 if (*s)
119 return opterror(opt, "expects a numerical value", flags);
120 return 0;
parse-options.h
+1
@@ -28,6 +28,7 @@ enum parse_opt_option_flags {
28 PARSE_OPT_NOARG = 2,
29 PARSE_OPT_NONEG = 4,
30 PARSE_OPT_HIDDEN = 8,
31 + PARSE_OPT_LASTARG_DEFAULT = 16,
32 };
33
34 struct option;