From f481e22a145ed17e85b68bf8ce2de4b80dfa4323 Mon Sep 17 00:00:00 2001 From: Pierre Habouzit Date: Tue, 16 Oct 2007 00:32:38 +0200 Subject: [PATCH] parse-options: allow callbacks to take no arguments at all. Signed-off-by: Pierre Habouzit Signed-off-by: Shawn O. Pearce --- parse-options.c | 10 +++++++++- parse-options.h | 1 + 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/parse-options.c b/parse-options.c index b4a3b63e9..cc09c98ec 100644 --- a/parse-options.c +++ b/parse-options.c @@ -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) @@ -261,8 +266,11 @@ void usage_with_options(const char * const *usagestr, else pos += fprintf(stderr, " "); 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); diff --git a/parse-options.h b/parse-options.h index 2558e00a7..3a470e5eb 100644 --- a/parse-options.h +++ b/parse-options.h @@ -16,6 +16,7 @@ enum parse_opt_flags { enum parse_opt_option_flags { PARSE_OPT_OPTARG = 1, + PARSE_OPT_NOARG = 2, }; struct option; -- 2.30.2