[PATCH 08/25] parse-options: allow callbacks to take no arguments at all.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Signed-off-by: Pierre Habouzit <madcoder@xxxxxxxxxx>
---
 parse-options.c |   10 +++++++++-
 parse-options.h |    1 +
 2 files changed, 10 insertions(+), 1 deletions(-)

diff --git a/parse-options.c b/parse-options.c
index f4ba9ce..531c8a1 100644
--- a/parse-options.c
+++ b/parse-options.c
@@ -73,6 +73,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)
@@ -268,8 +273,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);
diff --git a/parse-options.h b/parse-options.h
index 1f07d33..d3fb622 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;
-- 
1.5.3.4.1231.g62b9a

-
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html

[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]

  Powered by Linux