Merge branch 'ps/contains-id-error-message'

"git tag --contains no-such-commit" gave a full list of options
after giving an error message.

* ps/contains-id-error-message:
  parse-options: do not show usage upon invalid option value
This commit is contained in:
Junio C Hamano
2018-04-10 16:28:20 +09:00
8 changed files with 125 additions and 14 deletions

View File

@ -317,14 +317,16 @@ is_abbreviated:
return get_value(p, options, all_opts, flags ^ opt_flags);
}
if (ambiguous_option)
return error("Ambiguous option: %s "
if (ambiguous_option) {
error("Ambiguous option: %s "
"(could be --%s%s or --%s%s)",
arg,
(ambiguous_flags & OPT_UNSET) ? "no-" : "",
ambiguous_option->long_name,
(abbrev_flags & OPT_UNSET) ? "no-" : "",
abbrev_option->long_name);
return -3;
}
if (abbrev_option)
return get_value(p, abbrev_option, all_opts, abbrev_flags);
return -2;
@ -476,7 +478,6 @@ int parse_options_step(struct parse_opt_ctx_t *ctx,
const char * const usagestr[])
{
int internal_help = !(ctx->flags & PARSE_OPT_NO_INTERNAL_HELP);
int err = 0;
/* we must reset ->opt, unknown short option leave it dangling */
ctx->opt = NULL;
@ -505,7 +506,7 @@ int parse_options_step(struct parse_opt_ctx_t *ctx,
ctx->opt = arg + 1;
switch (parse_short_opt(ctx, options)) {
case -1:
goto show_usage_error;
return PARSE_OPT_ERROR;
case -2:
if (ctx->opt)
check_typos(arg + 1, options);
@ -518,7 +519,7 @@ int parse_options_step(struct parse_opt_ctx_t *ctx,
while (ctx->opt) {
switch (parse_short_opt(ctx, options)) {
case -1:
goto show_usage_error;
return PARSE_OPT_ERROR;
case -2:
if (internal_help && *ctx->opt == 'h')
goto show_usage;
@ -550,9 +551,11 @@ int parse_options_step(struct parse_opt_ctx_t *ctx,
goto show_usage;
switch (parse_long_opt(ctx, arg + 2, options)) {
case -1:
goto show_usage_error;
return PARSE_OPT_ERROR;
case -2:
goto unknown;
case -3:
goto show_usage;
}
continue;
unknown:
@ -563,10 +566,8 @@ unknown:
}
return PARSE_OPT_DONE;
show_usage_error:
err = 1;
show_usage:
return usage_with_options_internal(ctx, usagestr, options, 0, err);
return usage_with_options_internal(ctx, usagestr, options, 0, 0);
}
int parse_options_end(struct parse_opt_ctx_t *ctx)
@ -585,6 +586,7 @@ int parse_options(int argc, const char **argv, const char *prefix,
parse_options_start(&ctx, argc, argv, prefix, options, flags);
switch (parse_options_step(&ctx, options, usagestr)) {
case PARSE_OPT_HELP:
case PARSE_OPT_ERROR:
exit(129);
case PARSE_OPT_NON_OPTION:
case PARSE_OPT_DONE: