From 51c32efb7c2bde913d202a3fa25b292d543a9063 Mon Sep 17 00:00:00 2001 From: Marcus Brinkmann Date: Tue, 21 Nov 2017 03:30:31 +0100 Subject: [PATCH] Fix ExtrasError. --- include/CLI/App.hpp | 17 ++++++++--------- tests/SubcommandTest.cpp | 25 +++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 9 deletions(-) diff --git a/include/CLI/App.hpp b/include/CLI/App.hpp index 762a45b0..65a49960 100644 --- a/include/CLI/App.hpp +++ b/include/CLI/App.hpp @@ -954,7 +954,7 @@ class App { if(recurse) { for(const App_p &sub : subcommands_) { std::vector output = sub->remaining(recurse); - miss_list.assign(std::begin(output), std::end(output)); + std::copy(std::begin(output), std::end(output), std::back_inserter(miss_list)); } } return miss_list; @@ -1114,15 +1114,14 @@ class App { throw RequiredError(std::to_string(require_subcommand_) + " subcommand(s) required"); // Convert missing (pairs) to extras (string only) - if(parent_ == nullptr) { - args = remaining(true); - std::reverse(std::begin(args), std::end(args)); + if(!(allow_extras_ || prefix_command_)) { + size_t num_left_over = remaining_size(); + if(num_left_over > 0) { + args = remaining(false); + std::reverse(std::begin(args), std::end(args)); + throw ExtrasError("[" + detail::rjoin(args, " ") + "]"); + } } - - size_t num_left_over = remaining_size(); - - if(num_left_over > 0 && !(allow_extras_ || prefix_command_)) - throw ExtrasError("[" + detail::rjoin(args, " ") + "]"); } /// Parse one ini param, return false if not found in any subcommand, remove if it is diff --git a/tests/SubcommandTest.cpp b/tests/SubcommandTest.cpp index 52f7acff..40e0ba84 100644 --- a/tests/SubcommandTest.cpp +++ b/tests/SubcommandTest.cpp @@ -32,6 +32,23 @@ TEST_F(TApp, BasicSubcommands) { app.reset(); args = {"SUb2"}; EXPECT_THROW(run(), CLI::ExtrasError); + + app.reset(); + args = {"SUb2"}; + try { + run(); + } catch(const CLI::ExtrasError &e) { + EXPECT_THAT(e.what(), HasSubstr("SUb2")); + } + + app.reset(); + args = {"sub1", "extra"}; + try { + run(); + } catch(const CLI::ExtrasError &e) { + EXPECT_THAT(e.what(), HasSubstr("extra")); + } + } TEST_F(TApp, MultiSubFallthrough) { @@ -339,6 +356,14 @@ TEST_F(TApp, SubComExtras) { run(); EXPECT_EQ(app.remaining(), std::vector()); EXPECT_EQ(sub->remaining(), std::vector({"extra1", "extra2"})); + + app.reset(); + + args = {"extra1", "extra2", "sub", "extra3", "extra4"}; + run(); + EXPECT_EQ(app.remaining(), std::vector({"extra1", "extra2"})); + EXPECT_EQ(app.remaining(true), std::vector({"extra1", "extra2", "extra3", "extra4"})); + EXPECT_EQ(sub->remaining(), std::vector({"extra3", "extra4"})); } TEST_F(TApp, Required1SubCom) {