Skip to content

Commit

Permalink
parse-options: add PARSE_OPT_LASTARG_DEFAULT flag
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
Pierre Habouzit authored and Junio C Hamano committed Jul 9, 2008
1 parent 3f8d520 commit 1cc6985
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 28 deletions.
55 changes: 27 additions & 28 deletions parse-options.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,6 @@
#define OPT_SHORT 1
#define OPT_UNSET 2

static inline const char *get_arg(struct parse_opt_ctx_t *p)
{
if (p->opt) {
const char *res = p->opt;
p->opt = NULL;
return res;
}
p->argc--;
return *++p->argv;
}

static inline const char *skip_prefix(const char *str, const char *prefix)
{
size_t len = strlen(prefix);
Expand All @@ -31,8 +20,24 @@ static int opterror(const struct option *opt, const char *reason, int flags)
return error("option `%s' %s", opt->long_name, reason);
}

static int get_arg(struct parse_opt_ctx_t *p, const struct option *opt,
int flags, const char **arg)
{
if (p->opt) {
*arg = p->opt;
p->opt = NULL;
} else if (p->argc == 1 && (opt->flags & PARSE_OPT_LASTARG_DEFAULT)) {
*arg = (const char *)opt->defval;
} else if (p->argc) {
p->argc--;
*arg = *++p->argv;
} else
return opterror(opt, "requires a value", flags);
return 0;
}

static int get_value(struct parse_opt_ctx_t *p,
const struct option *opt, int flags)
const struct option *opt, int flags)
{
const char *s, *arg;
const int unset = flags & OPT_UNSET;
Expand All @@ -58,7 +63,6 @@ static int get_value(struct parse_opt_ctx_t *p,
}
}

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

case OPTION_STRING:
if (unset) {
if (unset)
*(const char **)opt->value = NULL;
return 0;
}
if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
else if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
*(const char **)opt->value = (const char *)opt->defval;
return 0;
}
if (!arg)
return opterror(opt, "requires a value", flags);
*(const char **)opt->value = get_arg(p);
else
return get_arg(p, opt, flags, (const char **)opt->value);
return 0;

case OPTION_CALLBACK:
Expand All @@ -100,9 +99,9 @@ static int get_value(struct parse_opt_ctx_t *p,
return (*opt->callback)(opt, NULL, 0) ? (-1) : 0;
if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
return (*opt->callback)(opt, NULL, 0) ? (-1) : 0;
if (!arg)
return opterror(opt, "requires a value", flags);
return (*opt->callback)(opt, get_arg(p), 0) ? (-1) : 0;
if (get_arg(p, opt, flags, &arg))
return -1;
return (*opt->callback)(opt, arg, 0) ? (-1) : 0;

case OPTION_INTEGER:
if (unset) {
Expand All @@ -113,9 +112,9 @@ static int get_value(struct parse_opt_ctx_t *p,
*(int *)opt->value = opt->defval;
return 0;
}
if (!arg)
return opterror(opt, "requires a value", flags);
*(int *)opt->value = strtol(get_arg(p), (char **)&s, 10);
if (get_arg(p, opt, flags, &arg))
return -1;
*(int *)opt->value = strtol(arg, (char **)&s, 10);
if (*s)
return opterror(opt, "expects a numerical value", flags);
return 0;
Expand Down
1 change: 1 addition & 0 deletions parse-options.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ enum parse_opt_option_flags {
PARSE_OPT_NOARG = 2,
PARSE_OPT_NONEG = 4,
PARSE_OPT_HIDDEN = 8,
PARSE_OPT_LASTARG_DEFAULT = 16,
};

struct option;
Expand Down

0 comments on commit 1cc6985

Please sign in to comment.