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

[PATCH 2/2] allow mangling short options which take integer arguments

From
Clemens Buchacher <drizzd@aon.at>
Date
Oct 1, 2009, 20:23 UTC
Message-ID
<20091001202301.GB12175@localhost>
In-Reply-To
<20091001201648.GA12175@localhost>
This patch allows you to do things like for example
  $ git <cmd> -bn100
where -b is a boolean and -n is an integer option.
Signed-off-by: Clemens Buchacher <drizzd@aon.at>
---
 parse-options.c |    2 +-
 parse-options.h |    5 ++++-
 2 files changed, 5 insertions(+), 2 deletions(-)
diff --git a/parse-options.c b/parse-options.c
index 4f16f37..bfe01ee 100644
--- a/parse-options.c
+++ b/parse-options.c
@@ -48,7 +48,7 @@ static int get_value(struct parse_opt_ctx_t *p,
 	const int unset = flags & OPT_UNSET;
 	int err;
 
-	if (many && !(opt->flags & PARSE_OPT_NOARG))
+	if (many && !(opt->flags & (PARSE_OPT_NOARG | PARSE_OPT_MANY)))
 		return opterror(opt, "must not be mangled with other options", flags);
 	if (unset && p->opt)
 		return opterror(opt, "takes no value", flags);
diff --git a/parse-options.h b/parse-options.h
index f295a2c..33ce529 100644
--- a/parse-options.h
+++ b/parse-options.h
@@ -37,6 +37,7 @@ enum parse_opt_option_flags {
 	PARSE_OPT_NODASH = 32,
 	PARSE_OPT_LITERAL_ARGHELP = 64,
 	PARSE_OPT_NEGHELP = 128,
+	PARSE_OPT_MANY = 256,
 };
 
 struct option;
@@ -84,6 +85,8 @@ typedef int parse_opt_cb(const struct option *, const char *arg, int unset);
  *   PARSE_OPT_NEGHELP: says that the long option should always be shown with
  *				the --no prefix in the usage message. Sometimes
  *				useful for users of OPTION_NEGBIT.
+ *   PARSE_OPT_MANY: the short option may be mangled, despite a possible
+ *				argument.
  *
  * `callback`::
  *   pointer to the callback to use for OPTION_CALLBACK.
@@ -121,7 +124,7 @@ struct option {
 				      (h), PARSE_OPT_NOARG, NULL, (i) }
 #define OPT_SET_PTR(s, l, v, h, p)  { OPTION_SET_PTR, (s), (l), (v), NULL, \
 				      (h), PARSE_OPT_NOARG, NULL, (p) }
-#define OPT_INTEGER(s, l, v, h)     { OPTION_INTEGER, (s), (l), (v), "n", (h) }
+#define OPT_INTEGER(s, l, v, h)     { OPTION_INTEGER, (s), (l), (v), "n", (h), PARSE_OPT_MANY }
 #define OPT_STRING(s, l, v, a, h)   { OPTION_STRING,  (s), (l), (v), (a), (h) }
 #define OPT_DATE(s, l, v, h) \
 	{ OPTION_CALLBACK, (s), (l), (v), "time",(h), 0, \
-- 
1.6.5.rc1.214.g13c5a
Previous: Clemens BuchacherNext: Johannes Schindelin
Message 7 of 21 in “how optparse can go horribly wrong”
  1. Shawn O. PearceSep 25, 2009
  2. Nicolas SebrechtSep 26, 2009
  3. Sverre RabbelierSep 26, 2009
  4. Shawn O. PearceSep 26, 2009
  5. Clemens BuchacherSep 28, 2009
  6. 1/2 do not mangle short options which take argumentsClemens Buchacher, Oct 1, 2009
  7. 2/2 allow mangling short options which take integer argumentsClemens Buchacher, Oct 1, 2009
  8. Johannes SchindelinOct 1, 2009
  9. Clemens BuchacherOct 2, 2009
  10. Jeff KingOct 2, 2009
  11. Clemens BuchacherOct 2, 2009
  12. Johannes SchindelinOct 2, 2009
  13. Clemens BuchacherOct 3, 2009
  14. Johannes SchindelinOct 1, 2009
  15. Jeff KingOct 2, 2009
  16. Clemens BuchacherOct 2, 2009
  17. Paolo BonziniOct 2, 2009
  18. Jeff KingOct 2, 2009
  19. Johannes SchindelinOct 2, 2009
  20. Jeff KingOct 2, 2009
  21. Johannes SchindelinOct 2, 2009

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.