From b15dc2b2e817ae0be17f0100b87a7a2cf078e5fd Mon Sep 17 00:00:00 2001 From: Fabian Homborg Date: Tue, 19 Oct 2021 17:43:54 +0200 Subject: [PATCH] argparse: Prevent duplicate error trailer This was already printed by builtin_missing_argument/unknown_option. Since we need more control (because we add our own errors in other places), teach builtin_unknown_option to suppress the trailer, like missing_argument already could. And then use it. Fixes #8368. --- src/builtin.cpp | 6 ++++-- src/builtin.h | 2 +- src/builtin_argparse.cpp | 4 ++-- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/src/builtin.cpp b/src/builtin.cpp index 80c29895f..c99da80eb 100644 --- a/src/builtin.cpp +++ b/src/builtin.cpp @@ -165,9 +165,11 @@ void builtin_print_help(parser_t &parser, const io_streams_t &streams, const wch /// Perform error reporting for encounter with unknown option. void builtin_unknown_option(parser_t &parser, io_streams_t &streams, const wchar_t *cmd, - const wchar_t *opt) { + const wchar_t *opt, bool print_hints) { streams.err.append_format(BUILTIN_ERR_UNKNOWN, cmd, opt); - builtin_print_error_trailer(parser, streams.err, cmd); + if (print_hints) { + builtin_print_error_trailer(parser, streams.err, cmd); + } } /// Perform error reporting for encounter with missing argument. diff --git a/src/builtin.h b/src/builtin.h index c91e92e4f..c13568ceb 100644 --- a/src/builtin.h +++ b/src/builtin.h @@ -94,7 +94,7 @@ void builtin_print_help(parser_t &parser, const io_streams_t &streams, const wch int builtin_count_args(const wchar_t *const *argv); void builtin_unknown_option(parser_t &parser, io_streams_t &streams, const wchar_t *cmd, - const wchar_t *opt); + const wchar_t *opt, bool print_hints = true); void builtin_missing_argument(parser_t &parser, io_streams_t &streams, const wchar_t *cmd, const wchar_t *opt, bool print_hints = true); diff --git a/src/builtin_argparse.cpp b/src/builtin_argparse.cpp index 75543969e..9a5334cd0 100644 --- a/src/builtin_argparse.cpp +++ b/src/builtin_argparse.cpp @@ -411,11 +411,11 @@ static int parse_cmd_opts(argparse_cmd_opts_t &opts, int *optind, //!OCLINT(hig break; } case ':': { - builtin_missing_argument(parser, streams, cmd, argv[w.woptind - 1]); + builtin_missing_argument(parser, streams, cmd, argv[w.woptind - 1], /* print_hints */false); return STATUS_INVALID_ARGS; } case '?': { - builtin_unknown_option(parser, streams, cmd, argv[w.woptind - 1]); + builtin_unknown_option(parser, streams, cmd, argv[w.woptind - 1], /* print_hints */false); return STATUS_INVALID_ARGS; } default: {