Commit 51c32efb7c2bde913d202a3fa25b292d543a9063
Committed by
Henry Schreiner
1 parent
675e7536
Fix ExtrasError.
Showing
2 changed files
with
33 additions
and
9 deletions
include/CLI/App.hpp
| @@ -954,7 +954,7 @@ class App { | @@ -954,7 +954,7 @@ class App { | ||
| 954 | if(recurse) { | 954 | if(recurse) { |
| 955 | for(const App_p &sub : subcommands_) { | 955 | for(const App_p &sub : subcommands_) { |
| 956 | std::vector<std::string> output = sub->remaining(recurse); | 956 | std::vector<std::string> output = sub->remaining(recurse); |
| 957 | - miss_list.assign(std::begin(output), std::end(output)); | 957 | + std::copy(std::begin(output), std::end(output), std::back_inserter(miss_list)); |
| 958 | } | 958 | } |
| 959 | } | 959 | } |
| 960 | return miss_list; | 960 | return miss_list; |
| @@ -1114,15 +1114,14 @@ class App { | @@ -1114,15 +1114,14 @@ class App { | ||
| 1114 | throw RequiredError(std::to_string(require_subcommand_) + " subcommand(s) required"); | 1114 | throw RequiredError(std::to_string(require_subcommand_) + " subcommand(s) required"); |
| 1115 | 1115 | ||
| 1116 | // Convert missing (pairs) to extras (string only) | 1116 | // Convert missing (pairs) to extras (string only) |
| 1117 | - if(parent_ == nullptr) { | ||
| 1118 | - args = remaining(true); | ||
| 1119 | - std::reverse(std::begin(args), std::end(args)); | 1117 | + if(!(allow_extras_ || prefix_command_)) { |
| 1118 | + size_t num_left_over = remaining_size(); | ||
| 1119 | + if(num_left_over > 0) { | ||
| 1120 | + args = remaining(false); | ||
| 1121 | + std::reverse(std::begin(args), std::end(args)); | ||
| 1122 | + throw ExtrasError("[" + detail::rjoin(args, " ") + "]"); | ||
| 1123 | + } | ||
| 1120 | } | 1124 | } |
| 1121 | - | ||
| 1122 | - size_t num_left_over = remaining_size(); | ||
| 1123 | - | ||
| 1124 | - if(num_left_over > 0 && !(allow_extras_ || prefix_command_)) | ||
| 1125 | - throw ExtrasError("[" + detail::rjoin(args, " ") + "]"); | ||
| 1126 | } | 1125 | } |
| 1127 | 1126 | ||
| 1128 | /// Parse one ini param, return false if not found in any subcommand, remove if it is | 1127 | /// Parse one ini param, return false if not found in any subcommand, remove if it is |
tests/SubcommandTest.cpp
| @@ -32,6 +32,23 @@ TEST_F(TApp, BasicSubcommands) { | @@ -32,6 +32,23 @@ TEST_F(TApp, BasicSubcommands) { | ||
| 32 | app.reset(); | 32 | app.reset(); |
| 33 | args = {"SUb2"}; | 33 | args = {"SUb2"}; |
| 34 | EXPECT_THROW(run(), CLI::ExtrasError); | 34 | EXPECT_THROW(run(), CLI::ExtrasError); |
| 35 | + | ||
| 36 | + app.reset(); | ||
| 37 | + args = {"SUb2"}; | ||
| 38 | + try { | ||
| 39 | + run(); | ||
| 40 | + } catch(const CLI::ExtrasError &e) { | ||
| 41 | + EXPECT_THAT(e.what(), HasSubstr("SUb2")); | ||
| 42 | + } | ||
| 43 | + | ||
| 44 | + app.reset(); | ||
| 45 | + args = {"sub1", "extra"}; | ||
| 46 | + try { | ||
| 47 | + run(); | ||
| 48 | + } catch(const CLI::ExtrasError &e) { | ||
| 49 | + EXPECT_THAT(e.what(), HasSubstr("extra")); | ||
| 50 | + } | ||
| 51 | + | ||
| 35 | } | 52 | } |
| 36 | 53 | ||
| 37 | TEST_F(TApp, MultiSubFallthrough) { | 54 | TEST_F(TApp, MultiSubFallthrough) { |
| @@ -339,6 +356,14 @@ TEST_F(TApp, SubComExtras) { | @@ -339,6 +356,14 @@ TEST_F(TApp, SubComExtras) { | ||
| 339 | run(); | 356 | run(); |
| 340 | EXPECT_EQ(app.remaining(), std::vector<std::string>()); | 357 | EXPECT_EQ(app.remaining(), std::vector<std::string>()); |
| 341 | EXPECT_EQ(sub->remaining(), std::vector<std::string>({"extra1", "extra2"})); | 358 | EXPECT_EQ(sub->remaining(), std::vector<std::string>({"extra1", "extra2"})); |
| 359 | + | ||
| 360 | + app.reset(); | ||
| 361 | + | ||
| 362 | + args = {"extra1", "extra2", "sub", "extra3", "extra4"}; | ||
| 363 | + run(); | ||
| 364 | + EXPECT_EQ(app.remaining(), std::vector<std::string>({"extra1", "extra2"})); | ||
| 365 | + EXPECT_EQ(app.remaining(true), std::vector<std::string>({"extra1", "extra2", "extra3", "extra4"})); | ||
| 366 | + EXPECT_EQ(sub->remaining(), std::vector<std::string>({"extra3", "extra4"})); | ||
| 342 | } | 367 | } |
| 343 | 368 | ||
| 344 | TEST_F(TApp, Required1SubCom) { | 369 | TEST_F(TApp, Required1SubCom) { |