Skip to content

Commit

Permalink
parse-options: allow callbacks to take no arguments at all.
Browse files Browse the repository at this point in the history
Signed-off-by: Pierre Habouzit <madcoder@debian.org>
Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
  • Loading branch information
Pierre Habouzit authored and Junio C Hamano committed Oct 30, 2007
1 parent 7f275b9 commit f481e22
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
10 changes: 9 additions & 1 deletion parse-options.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ static int get_value(struct optparse_t *p,
case OPTION_CALLBACK:
if (flags & OPT_UNSET)
return (*opt->callback)(opt, NULL, 1);
if (opt->flags & PARSE_OPT_NOARG) {
if (p->opt && !(flags & OPT_SHORT))
return opterror(opt, "takes no value", flags);
return (*opt->callback)(opt, NULL, 0);
}
if (opt->flags & PARSE_OPT_OPTARG && (!arg || *arg == '-'))
return (*opt->callback)(opt, NULL, 0);
if (!arg)
Expand Down Expand Up @@ -261,8 +266,11 @@ void usage_with_options(const char * const *usagestr,
else
pos += fprintf(stderr, " <n>");
break;
case OPTION_STRING:
case OPTION_CALLBACK:
if (opts->flags & PARSE_OPT_NOARG)
break;
/* FALLTHROUGH */
case OPTION_STRING:
if (opts->argh) {
if (opts->flags & PARSE_OPT_OPTARG)
pos += fprintf(stderr, " [<%s>]", opts->argh);
Expand Down
1 change: 1 addition & 0 deletions parse-options.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ enum parse_opt_flags {

enum parse_opt_option_flags {
PARSE_OPT_OPTARG = 1,
PARSE_OPT_NOARG = 2,
};

struct option;
Expand Down

0 comments on commit f481e22

Please sign in to comment.