git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 2/7] parse-options: allow callbacks to ignore arguments they don't need to use.

From
Pierre Habouzit <madcoder@debian.org>
Date
Dec 17, 2007, 18:23 UTC
Message-ID
<1197915797-30679-3-git-send-email-madcoder@debian.org>
In-Reply-To
<1197915797-30679-2-git-send-email-madcoder@debian.org>
Signed-off-by: Pierre Habouzit <madcoder@debian.org>
---
 parse-options.c |   22 +++++++++++++++-------
 parse-options.h |    7 +++++++
 2 files changed, 22 insertions(+), 7 deletions(-)
diff --git a/parse-options.c b/parse-options.c
index 8f70e5d..d716ccc 100644
--- a/parse-options.c
+++ b/parse-options.c
@@ -38,7 +38,10 @@ static int get_value(struct optparse_t *p,
 {
 	const char *s, *arg;
 	const int unset = flags & PARSE_OPT_UNSET;
+	int may_ign = 0, res;
 
+	if (!unset && !p->opt && (opt->flags & PARSE_OPT_OPTARG))
+		may_ign = PARSE_OPT_MAY_IGN;
 	if (unset && p->opt)
 		return opterror(opt, "takes no value", flags);
 	if (unset && (opt->flags & PARSE_OPT_NONEG))
@@ -86,7 +89,7 @@ static int get_value(struct optparse_t *p,
 			*(const char **)opt->value = NULL;
 			return 0;
 		}
-		if (opt->flags & PARSE_OPT_OPTARG && (!arg || *arg == '-')) {
+		if (may_ign && (!arg || *arg == '-')) {
 			*(const char **)opt->value = (const char *)opt->defval;
 			return 0;
 		}
@@ -98,18 +101,23 @@ static int get_value(struct optparse_t *p,
 	case OPTION_CALLBACK:
 		if (unset || (opt->flags & PARSE_OPT_NOARG))
 			return (*opt->callback)(opt, NULL, flags);
-		if (opt->flags & PARSE_OPT_OPTARG && (!arg || *arg == '-'))
-			return (*opt->callback)(opt, NULL, flags);
-		if (!arg)
+		if (!may_ign && !arg)
 			return opterror(opt, "requires a value", flags);
-		return (*opt->callback)(opt, get_arg(p), flags);
+		if (may_ign && arg && arg[0] == '-' && arg[1])
+			return (*opt->callback)(opt, NULL, flags);
+		res = (*opt->callback)(opt, arg, flags);
+		if (!may_ign && res == PARSE_OPT_IGNORE)
+			die("should not happen: MAY_IGN unset, but arg was IGNOREd");
+		if (res == PARSE_OPT_IGNORE)
+			get_arg(p);
+		return res;
 
 	case OPTION_INTEGER:
 		if (unset) {
 			*(int *)opt->value = 0;
 			return 0;
 		}
-		if (opt->flags & PARSE_OPT_OPTARG && (!arg || !isdigit(*arg))) {
+		if (may_ign && (!arg || !isdigit(*arg))) {
 			*(int *)opt->value = opt->defval;
 			return 0;
 		}
@@ -251,7 +259,7 @@ int parse_options(int argc, const char **argv, const struct option *options,
 			usage_with_options_internal(usagestr, options, 1);
 		if (!strcmp(arg + 2, "help"))
 			usage_with_options(usagestr, options);
-		if (parse_long_opt(&args, arg + 2, options))
+		if (parse_long_opt(&args, arg + 2, options) < 0)
 			usage_with_options(usagestr, options);
 	}
 
diff --git a/parse-options.h b/parse-options.h
index ae6b3ca..eeb40a4 100644
--- a/parse-options.h
+++ b/parse-options.h
@@ -27,9 +27,16 @@ enum parse_opt_option_flags {
 	PARSE_OPT_HIDDEN  = 8,
 };
 
+enum parse_opt_cbres {
+	PARSE_OPT_ERR     = -1,
+	PARSE_OPT_OK      =  0,
+	PARSE_OPT_IGNORE  =  1,
+};
+
 enum parse_opt_cbflags {
 	PARSE_OPT_SHORT   = 1,
 	PARSE_OPT_UNSET   = 2,
+	PARSE_OPT_MAY_IGN = 4,
 };
 
 struct option;
-- 
1.5.4.rc0.1148.ga3ab1-dirty
Previous: Pierre HabouzitNext: Pierre Habouzit
Message 3 of 13 in “[proposal] make parse-options nicer wrt optional arguments (supersedes all my recent posts on the matter)”
  1. Pierre HabouzitDec 17, 2007
  2. 1/7 parse-options: Make callbacks take flags instead of boolean `unset`Pierre Habouzit, Dec 17, 2007
  3. 2/7 parse-options: allow callbacks to ignore arguments they don't need to use.Pierre Habouzit, Dec 17, 2007
  4. 3/7 parse-options: Let the integer/string cases be callbacks as well.Pierre Habouzit, Dec 17, 2007
  5. 4/7 parse-options: let OPT__ABBREV ignore arguments.Pierre Habouzit, Dec 17, 2007
  6. 5/7 parse-options: Add a gitcli(5) man page.Pierre Habouzit, Dec 17, 2007
  7. 6/7 parse-options: have a `use default value` wildcard.Pierre Habouzit, Dec 17, 2007
  8. 7/7 git-tag: fix -l switch handling regression.Pierre Habouzit, Dec 17, 2007
  9. Pierre HabouzitDec 17, 2007
  10. Pierre HabouzitDec 17, 2007
  11. Junio C HamanoDec 17, 2007
  12. Wayne DavisonDec 18, 2007
  13. Pierre HabouzitDec 17, 2007

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.