From bd5bb36881c5893d4aea6465c8698669a338662d Mon Sep 17 00:00:00 2001 From: Even Rouault Date: Fri, 8 Nov 2024 20:19:16 +0100 Subject: [PATCH] subparser: use full parser path instead of just parser name in usage() message --- include/argparse/argparse.hpp | 2 +- test/test_subparsers.cpp | 26 ++++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/include/argparse/argparse.hpp b/include/argparse/argparse.hpp index e7695e5c..c8c99b68 100644 --- a/include/argparse/argparse.hpp +++ b/include/argparse/argparse.hpp @@ -2067,7 +2067,7 @@ class ArgumentParser { std::stringstream stream; std::string curline("Usage: "); - curline += this->m_program_name; + curline += this->m_parser_path; const bool multiline_usage = this->m_usage_max_line_width < (std::numeric_limits::max)(); const size_t indent_size = curline.size(); diff --git a/test/test_subparsers.cpp b/test/test_subparsers.cpp index 532c9a7e..d2bd7863 100644 --- a/test/test_subparsers.cpp +++ b/test/test_subparsers.cpp @@ -280,3 +280,29 @@ TEST_CASE("Check set_suppress" * test_suite("subparsers")) { REQUIRE(contains(program.help().str(), "command_2") == true); } } + + +TEST_CASE("Help of subparsers" * test_suite("subparsers")) { + argparse::ArgumentParser program("test"); + + argparse::ArgumentParser command_1("add", "1.0", argparse::default_arguments::version); + + std::stringstream buffer; + command_1.add_argument("--help") + .action([&](const auto &) { buffer << command_1; }) + .default_value(false) + .implicit_value(true) + .nargs(0); + + program.add_subparser(command_1); + + REQUIRE(command_1.usage() == "Usage: test add [--version] [--help]"); + + REQUIRE(buffer.str().empty()); + program.parse_args({"test", "add", "--help"}); + REQUIRE(buffer.str() == "Usage: test add [--version] [--help]\n" + "\n" + "Optional arguments:\n" + " -v, --version prints version information and exits \n" + " --help \n"); +}