@matheusbsbs / matheus-bagliano / commits / 0d260f9a09
Fork 0

parseopt: prevent KEEP_UNKNOWN and STOP_AT_NON_OPTION from being used together

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> Signed-off-by: Junio C Hamano <gitster@pobox.com>

René Scharfe committed Mar 9, 2009 at 21:57 UTC 0d260f9a09a2febeb86fdada7224d271a76d2e3c
2 files changed +5 -1
Documentation/technical/api-parse-options.txt
+2 -1
@@ -96,7 +96,8 @@ Flags are the bitwise-or of:
96 `PARSE_OPT_STOP_AT_NON_OPTION` is set, the second argument in
97 `--unknown value` will be mistakenly interpreted as a
98 non-option, not as a value belonging to the unknown option,
99 - stopping the parser early.
99 + the parser early. That's why parse_options() errors out if
100 + both options are set.
101
102 `PARSE_OPT_NO_INTERNAL_HELP`::
103 By default, parse_options() handles `-h`, `--help` and
parse-options.c
+3
@@ -244,6 +244,9 @@ void parse_options_start(struct parse_opt_ctx_t *ctx,
244 ctx->out = argv;
245 ctx->cpidx = ((flags & PARSE_OPT_KEEP_ARGV0) != 0);
246 ctx->flags = flags;
247 + if ((flags & PARSE_OPT_KEEP_UNKNOWN) &&
248 + (flags & PARSE_OPT_STOP_AT_NON_OPTION))
249 + die("STOP_AT_NON_OPTION and KEEP_UNKNOWN don't go together");
250 }
251
252 static int usage_with_options_internal(const char * const *,