Skip to content

Commit

Permalink
parseopt: prevent KEEP_UNKNOWN and STOP_AT_NON_OPTION from being used…
Browse files Browse the repository at this point in the history
… 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>
  • Loading branch information
René Scharfe authored and Junio C Hamano committed Mar 9, 2009
1 parent 9ad7e6e commit 0d260f9
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 1 deletion.
3 changes: 2 additions & 1 deletion Documentation/technical/api-parse-options.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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 unknown 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
Expand Down
3 changes: 3 additions & 0 deletions parse-options.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 *,
Expand Down

0 comments on commit 0d260f9

Please sign in to comment.