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

[PATCH 6/4] parseopt: prevent KEEP_UNKNOWN and STOP_AT_NON_OPTION from being used together

From
René Scharfe <rene.scharfe@lsrfire.ath.cx>
Date
Mar 9, 2009, 20:57 UTC
Message-ID
<49B582C2.2080803@lsrfire.ath.cx>
In-Reply-To
<7vvdqjkbka.fsf@gitster.siamese.dyndns.org>

As suggested by Junio, disallow the flags PARSE_OPT_KEEP_UNKNOWN and PARSE_OPT_STOP_AT_NON_OPTION to be turned on at the same time, as a value of an unknown option could be mistakenly classified as a non-option, stopping the parser early. E.g.:

	git cmd --known --unknown value arg0 arg1

The parser should have stopped at "arg0", but it already stops at "value".

This patch makes parse_options() die if the two flags are used in combination.

Signed-off-by: Rene Scharfe <rene.scharfe@lsrfire.ath.cx>
---
 Documentation/technical/api-parse-options.txt |    3 ++-
 parse-options.c                               |    3 +++
 2 files changed, 5 insertions(+), 1 deletions(-)
diff --git a/Documentation/technical/api-parse-options.txt b/Documentation/technical/api-parse-options.txt
index dc72987..f5d4ac1 100644
--- a/Documentation/technical/api-parse-options.txt
+++ b/Documentation/technical/api-parse-options.txt
@@ -96,7 +96,8 @@ Flags are the bitwise-or of:
 	`PARSE_OPT_STOP_AT_NON_OPTION` is set, the second argument in
 	`--unknown value` will be mistakenly interpreted as a
 	non-option, not as a value belonging to the option, stopping
-	the parser early.
+	the parser early.  That's why parse_options() errors out if
+	both options are set.
 
 `PARSE_OPT_NO_INTERNAL_HELP`::
 	By default, parse_options() handles `-h`, `--help` and
diff --git a/parse-options.c b/parse-options.c
index 51e804b..cf71bcf 100644
--- a/parse-options.c
+++ b/parse-options.c
@@ -244,6 +244,9 @@ void parse_options_start(struct parse_opt_ctx_t *ctx,
 	ctx->out  = argv;
 	ctx->cpidx = ((flags & PARSE_OPT_KEEP_ARGV0) != 0);
 	ctx->flags = flags;
+	if ((flags & PARSE_OPT_KEEP_UNKNOWN) &&
+	    (flags & PARSE_OPT_STOP_AT_NON_OPTION))
+		die("STOP_AT_NON_OPTION and KEEP_UNKNOWN don't go together");
 }
 
 static int usage_with_options_internal(const char * const *,
-- 
1.6.2
Previous: René ScharfeNext: René Scharfe
Message 7 of 13 in “What's in git.git (Mar 2009, #02; Thu, 05)”
  1. Junio C HamanoMar 5, 2009
  2. René ScharfeMar 7, 2009
  3. 1/4 parseopt: add PARSE_OPT_KEEP_UNKNOWNRené Scharfe, Mar 8, 2009
  4. Junio C HamanoMar 8, 2009
  5. Junio C HamanoMar 8, 2009
  6. 5/4 parseopt: document KEEP_ARGV0, KEEP_UNKNOWN, NO_INTERNAL_HELPRené Scharfe, Mar 9, 2009
  7. 6/4 parseopt: prevent KEEP_UNKNOWN and STOP_AT_NON_OPTION from being used togetherRené Scharfe, Mar 9, 2009
  8. 2/4 parseopt: add PARSE_OPT_NO_INTERNAL_HELPRené Scharfe, Mar 8, 2009
  9. 3/4 parseopt: make usage optionalRené Scharfe, Mar 8, 2009
  10. Junio C HamanoMar 8, 2009
  11. René ScharfeMar 9, 2009
  12. 4/4 archive: use parseopt for local-only optionsRené Scharfe, Mar 8, 2009
  13. Junio C HamanoMar 8, 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.