@matheusbsbs / matheus-bagliano / commits / f481e22a14
Fork 0

parse-options: allow callbacks to take no arguments at all.

Signed-off-by: Pierre Habouzit <madcoder@debian.org> Signed-off-by: Shawn O. Pearce <spearce@spearce.org>

Pierre Habouzit committed Oct 16, 2007 at 00:32 UTC f481e22a145ed17e85b68bf8ce2de4b80dfa4323
2 files changed +10 -1
parse-options.c
+9 -1
@@ -72,6 +72,11 @@ static int get_value(struct optparse_t *p,
72 case OPTION_CALLBACK:
73 if (flags & OPT_UNSET)
74 return (*opt->callback)(opt, NULL, 1);
75 + if (opt->flags & PARSE_OPT_NOARG) {
76 + if (p->opt && !(flags & OPT_SHORT))
77 + return opterror(opt, "takes no value", flags);
78 + return (*opt->callback)(opt, NULL, 0);
79 + }
80 if (opt->flags & PARSE_OPT_OPTARG && (!arg || *arg == '-'))
81 return (*opt->callback)(opt, NULL, 0);
82 if (!arg)
@@ -261,8 +266,11 @@ void usage_with_options(const char * const *usagestr,
266 else
267 pos += fprintf(stderr, " <n>");
268 break;
264 - case OPTION_STRING:
269 case OPTION_CALLBACK:
270 + if (opts->flags & PARSE_OPT_NOARG)
271 + break;
272 + /* FALLTHROUGH */
273 + case OPTION_STRING:
274 if (opts->argh) {
275 if (opts->flags & PARSE_OPT_OPTARG)
276 pos += fprintf(stderr, " [<%s>]", opts->argh);
parse-options.h
+1
@@ -16,6 +16,7 @@ enum parse_opt_flags {
16
17 enum parse_opt_option_flags {
18 PARSE_OPT_OPTARG = 1,
19 + PARSE_OPT_NOARG = 2,
20 };
21
22 struct option;