From 33f3f2c038364630fb8e0614a443949279c030e8 Mon Sep 17 00:00:00 2001 From: Enrico Seiler Date: Fri, 9 Feb 2024 14:11:31 +0100 Subject: [PATCH] resolve compatibility with old ctor --- include/sharg/parser.hpp | 13 ++++--- test/snippet/readme_sneak_peek.cpp | 34 +++++++++++++++++-- test/unit/parser/parser_design_error_test.cpp | 2 +- 3 files changed, 42 insertions(+), 7 deletions(-) diff --git a/include/sharg/parser.hpp b/include/sharg/parser.hpp index 9205365d..76ca6d5e 100644 --- a/include/sharg/parser.hpp +++ b/include/sharg/parser.hpp @@ -681,8 +681,9 @@ class parser */ parser_meta_data info; - /*!\brief Adds subcommands to the parser. - * \param[in] subcommands A list of subcommands. + /*!\brief Adds a subcommand to the parser. + * \param[in] subcommand The subcommand to add. + * \returns A pointer to the sub-parser if subcommand is encountered, nullptr otherwise. * \throws sharg::design_error if the subcommand name contains illegal characters. */ [[nodiscard]] parser * add_subcommand(std::string subcommand) @@ -701,7 +702,7 @@ class parser }; auto & parser_subcommands = this->subcommands; - parser_subcommands.emplace_back(std::move(subcommand)); + parser_subcommands.emplace_back(subcommand); std::ranges::sort(parser_subcommands); auto const [first, last] = std::ranges::unique(parser_subcommands); @@ -709,7 +710,11 @@ class parser init(); - return sub_parser.get(); + // If a subcommand was already added via the constructor, we need to check if the subcommand is the same. + if (sub_parser && sub_parser->info.app_name == info.app_name + '-' + subcommand) + return sub_parser.get(); + else + return nullptr; } private: diff --git a/test/snippet/readme_sneak_peek.cpp b/test/snippet/readme_sneak_peek.cpp index 11d169fb..ad6305c2 100644 --- a/test/snippet/readme_sneak_peek.cpp +++ b/test/snippet/readme_sneak_peek.cpp @@ -1,9 +1,16 @@ +// SPDX-FileCopyrightText: 2006-2024 Knut Reinert & Freie Universität Berlin +// SPDX-FileCopyrightText: 2016-2024 Knut Reinert & MPI für molekulare Genetik +// SPDX-License-Identifier: CC0-1.0 + +#define main test + #include +#if 0 int main(int argc, char ** argv) { - // sharg::parser git_parser{"git", argc, argv, sharg::update_notifications::on, {"pull", "push", "remote"}}; // NOT ALLOWED - sharg::parser git_parser{"git", argc, argv, sharg::update_notifications::on}; + // -------- Optional -------- + sharg::parser git_parser{"git", argc, argv, sharg::update_notifications::on, {"pull", "push", "remote"}}; if (auto pull_parser = git_parser.add_subcommand("pull")) { @@ -47,3 +54,26 @@ int main(int argc, char ** argv) return 0; } +#else +int main(int argc, char ** argv) +{ + int val{}; + + sharg::parser parser{"Eat-Me-App", argc, argv}; + parser.add_subsection("Eating Numbers"); + parser.add_option(val, sharg::config{.short_id = 'i', .long_id = "int", .description = "Desc."}); + parser.parse(); + + return 0; +} +#endif + +#undef main + +int main() +{ + std::string argv1{"./Eat-Me-App"}, argv2{"-h"}; + std::array argv{argv1.data(), argv2.data()}; + + return test(argv.size(), argv.data()); +} diff --git a/test/unit/parser/parser_design_error_test.cpp b/test/unit/parser/parser_design_error_test.cpp index 308a4826..ae8d0bbe 100644 --- a/test/unit/parser/parser_design_error_test.cpp +++ b/test/unit/parser/parser_design_error_test.cpp @@ -263,7 +263,7 @@ TEST(parse_test, subcommand_parser_error) EXPECT_NO_THROW(top_level_parser.parse()); EXPECT_EQ(true, flag_value); - EXPECT_THROW(top_level_parser.get_sub_parser(), sharg::design_error); + EXPECT_THROW(std::ignore = top_level_parser.get_sub_parser(), sharg::design_error); } // subcommand key word must only contain alpha numeric characters