1
0
mirror of https://github.com/CLIUtils/CLI11.git synced 2025-04-29 20:23:55 +00:00

Fix ExtrasError.

This commit is contained in:
Marcus Brinkmann 2017-11-21 03:30:31 +01:00 committed by Henry Schreiner
parent 675e753661
commit 51c32efb7c
2 changed files with 33 additions and 9 deletions

View File

@ -954,7 +954,7 @@ class App {
if(recurse) { if(recurse) {
for(const App_p &sub : subcommands_) { for(const App_p &sub : subcommands_) {
std::vector<std::string> output = sub->remaining(recurse); std::vector<std::string> 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; return miss_list;
@ -1114,15 +1114,14 @@ class App {
throw RequiredError(std::to_string(require_subcommand_) + " subcommand(s) required"); throw RequiredError(std::to_string(require_subcommand_) + " subcommand(s) required");
// Convert missing (pairs) to extras (string only) // Convert missing (pairs) to extras (string only)
if(parent_ == nullptr) { if(!(allow_extras_ || prefix_command_)) {
args = remaining(true); size_t num_left_over = remaining_size();
std::reverse(std::begin(args), std::end(args)); 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 /// Parse one ini param, return false if not found in any subcommand, remove if it is

View File

@ -32,6 +32,23 @@ TEST_F(TApp, BasicSubcommands) {
app.reset(); app.reset();
args = {"SUb2"}; args = {"SUb2"};
EXPECT_THROW(run(), CLI::ExtrasError); 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) { TEST_F(TApp, MultiSubFallthrough) {
@ -339,6 +356,14 @@ TEST_F(TApp, SubComExtras) {
run(); run();
EXPECT_EQ(app.remaining(), std::vector<std::string>()); EXPECT_EQ(app.remaining(), std::vector<std::string>());
EXPECT_EQ(sub->remaining(), std::vector<std::string>({"extra1", "extra2"})); EXPECT_EQ(sub->remaining(), std::vector<std::string>({"extra1", "extra2"}));
app.reset();
args = {"extra1", "extra2", "sub", "extra3", "extra4"};
run();
EXPECT_EQ(app.remaining(), std::vector<std::string>({"extra1", "extra2"}));
EXPECT_EQ(app.remaining(true), std::vector<std::string>({"extra1", "extra2", "extra3", "extra4"}));
EXPECT_EQ(sub->remaining(), std::vector<std::string>({"extra3", "extra4"}));
} }
TEST_F(TApp, Required1SubCom) { TEST_F(TApp, Required1SubCom) {