diff --git a/examples/prefix_command.cpp b/examples/prefix_command.cpp index dcf4f7ff..b716c2be 100644 --- a/examples/prefix_command.cpp +++ b/examples/prefix_command.cpp @@ -8,13 +8,14 @@ int main(int argc, char **argv) { std::vector vals; app.add_option("--vals,-v", vals)->expected(1); - std::vector more_comms; try { - more_comms = app.parse(argc, argv); + app.parse(argc, argv); } catch(const CLI::ParseError &e) { return app.exit(e); } + std::vector more_comms = app.remaining(); + std::cout << "Prefix:"; for(int v : vals) std::cout << v << ":"; diff --git a/include/CLI/App.hpp b/include/CLI/App.hpp index 9b0c340f..a78d7258 100644 --- a/include/CLI/App.hpp +++ b/include/CLI/App.hpp @@ -626,21 +626,20 @@ class App { /// Parses the command line - throws errors /// This must be called after the options are in but before the rest of the program. - std::vector parse(int argc, char **argv) { + void parse(int argc, char **argv) { name_ = argv[0]; std::vector args; for(int i = argc - 1; i > 0; i--) args.emplace_back(argv[i]); - return parse(args); + parse(args); } /// The real work is done here. Expects a reversed vector. /// Changes the vector to the remaining options. - std::vector &parse(std::vector &args) { + void parse(std::vector &args) { _validate(); _parse(args); run_callback(); - return args; } /// Print a nice error message and return the exit code @@ -872,6 +871,15 @@ class App { return miss_list; } + /// This returns the number of remaining options, minus the -- seperator + size_t remaining_size() const { + return std::count_if( + std::begin(missing_), std::end(missing_), + [](const std::pair &val) { + return val.first != detail::Classifer::POSITIONAL_MARK; + }); + } + ///@} protected: @@ -1020,17 +1028,10 @@ class App { // Convert missing (pairs) to extras (string only) if(parent_ == nullptr) { - args.resize(missing()->size()); - std::transform(std::begin(*missing()), - std::end(*missing()), - std::begin(args), - [](const std::pair &val) { return val.second; }); + args = remaining(); std::reverse(std::begin(args), std::end(args)); - size_t num_left_over = std::count_if( - std::begin(*missing()), std::end(*missing()), [](std::pair &val) { - return val.first != detail::Classifer::POSITIONAL_MARK; - }); + size_t num_left_over = remaining_size(); if(num_left_over > 0 && !(allow_extras_ || prefix_command_)) throw ExtrasError("[" + detail::rjoin(args, " ") + "]"); diff --git a/tests/AppTest.cpp b/tests/AppTest.cpp index 7a636a81..1a62a0f3 100644 --- a/tests/AppTest.cpp +++ b/tests/AppTest.cpp @@ -917,10 +917,10 @@ TEST_F(TApp, AllowExtras) { EXPECT_FALSE(val); args = {"-x", "-f"}; - std::vector left_over; - EXPECT_NO_THROW({ left_over = run(); }); + + EXPECT_NO_THROW(run()); EXPECT_TRUE(val); - EXPECT_EQ(std::vector({"-x"}), left_over); + EXPECT_EQ(app.remaining(), std::vector({"-x"})); } TEST_F(TApp, AllowExtrasOrder) { @@ -928,14 +928,13 @@ TEST_F(TApp, AllowExtrasOrder) { app.allow_extras(); args = {"-x", "-f"}; - std::vector left_over; - EXPECT_NO_THROW({ left_over = run(); }); - EXPECT_EQ(std::vector({"-f", "-x"}), left_over); + EXPECT_NO_THROW(run()); + EXPECT_EQ(app.remaining(), std::vector({"-x", "-f"})); app.reset(); - std::vector left_over_2; - left_over_2 = app.parse(left_over); - EXPECT_EQ(left_over, left_over_2); + std::vector left_over = app.remaining(); + app.parse(left_over); + EXPECT_EQ(app.remaining(), left_over); } // Test horrible error diff --git a/tests/SubcommandTest.cpp b/tests/SubcommandTest.cpp index 83591e7b..8fdc7ce6 100644 --- a/tests/SubcommandTest.cpp +++ b/tests/SubcommandTest.cpp @@ -339,9 +339,9 @@ TEST_F(TApp, PrefixProgram) { app.add_flag("--simple"); args = {"--simple", "other", "--simple", "--mine"}; - auto ret_args = run(); + run(); - EXPECT_EQ(ret_args, std::vector({"--mine", "--simple", "other"})); + EXPECT_EQ(app.remaining(), std::vector({"other", "--simple", "--mine"})); } struct SubcommandProgram : public TApp { diff --git a/tests/app_helper.hpp b/tests/app_helper.hpp index e3ed4d73..44d6b6a3 100644 --- a/tests/app_helper.hpp +++ b/tests/app_helper.hpp @@ -15,10 +15,10 @@ struct TApp : public ::testing::Test { CLI::App app{"My Test Program"}; input_t args; - std::vector run() { + void run() { input_t newargs = args; std::reverse(std::begin(newargs), std::end(newargs)); - return app.parse(newargs); + app.parse(newargs); } };