提交 f481e22a 编写于 作者: P Pierre Habouzit 提交者: Junio C Hamano

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

Signed-off-by: NPierre Habouzit <madcoder@debian.org>
Signed-off-by: NShawn O. Pearce <spearce@spearce.org>
上级 7f275b91
......@@ -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, " <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);
......
......@@ -16,6 +16,7 @@ enum parse_opt_flags {
enum parse_opt_option_flags {
PARSE_OPT_OPTARG = 1,
PARSE_OPT_NOARG = 2,
};
struct option;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册