Commit cb7c3ff85a2f26e855f23599981d417856e20b1a
Committed by
Henry Schreiner
1 parent
fc35014d
Move to using remaining, drop return from parse
Showing
5 changed files
with
29 additions
and
28 deletions
examples/prefix_command.cpp
| @@ -8,13 +8,14 @@ int main(int argc, char **argv) { | @@ -8,13 +8,14 @@ int main(int argc, char **argv) { | ||
| 8 | std::vector<int> vals; | 8 | std::vector<int> vals; |
| 9 | app.add_option("--vals,-v", vals)->expected(1); | 9 | app.add_option("--vals,-v", vals)->expected(1); |
| 10 | 10 | ||
| 11 | - std::vector<std::string> more_comms; | ||
| 12 | try { | 11 | try { |
| 13 | - more_comms = app.parse(argc, argv); | 12 | + app.parse(argc, argv); |
| 14 | } catch(const CLI::ParseError &e) { | 13 | } catch(const CLI::ParseError &e) { |
| 15 | return app.exit(e); | 14 | return app.exit(e); |
| 16 | } | 15 | } |
| 17 | 16 | ||
| 17 | + std::vector<std::string> more_comms = app.remaining(); | ||
| 18 | + | ||
| 18 | std::cout << "Prefix:"; | 19 | std::cout << "Prefix:"; |
| 19 | for(int v : vals) | 20 | for(int v : vals) |
| 20 | std::cout << v << ":"; | 21 | std::cout << v << ":"; |
include/CLI/App.hpp
| @@ -626,21 +626,20 @@ class App { | @@ -626,21 +626,20 @@ class App { | ||
| 626 | 626 | ||
| 627 | /// Parses the command line - throws errors | 627 | /// Parses the command line - throws errors |
| 628 | /// This must be called after the options are in but before the rest of the program. | 628 | /// This must be called after the options are in but before the rest of the program. |
| 629 | - std::vector<std::string> parse(int argc, char **argv) { | 629 | + void parse(int argc, char **argv) { |
| 630 | name_ = argv[0]; | 630 | name_ = argv[0]; |
| 631 | std::vector<std::string> args; | 631 | std::vector<std::string> args; |
| 632 | for(int i = argc - 1; i > 0; i--) | 632 | for(int i = argc - 1; i > 0; i--) |
| 633 | args.emplace_back(argv[i]); | 633 | args.emplace_back(argv[i]); |
| 634 | - return parse(args); | 634 | + parse(args); |
| 635 | } | 635 | } |
| 636 | 636 | ||
| 637 | /// The real work is done here. Expects a reversed vector. | 637 | /// The real work is done here. Expects a reversed vector. |
| 638 | /// Changes the vector to the remaining options. | 638 | /// Changes the vector to the remaining options. |
| 639 | - std::vector<std::string> &parse(std::vector<std::string> &args) { | 639 | + void parse(std::vector<std::string> &args) { |
| 640 | _validate(); | 640 | _validate(); |
| 641 | _parse(args); | 641 | _parse(args); |
| 642 | run_callback(); | 642 | run_callback(); |
| 643 | - return args; | ||
| 644 | } | 643 | } |
| 645 | 644 | ||
| 646 | /// Print a nice error message and return the exit code | 645 | /// Print a nice error message and return the exit code |
| @@ -872,6 +871,15 @@ class App { | @@ -872,6 +871,15 @@ class App { | ||
| 872 | return miss_list; | 871 | return miss_list; |
| 873 | } | 872 | } |
| 874 | 873 | ||
| 874 | + /// This returns the number of remaining options, minus the -- seperator | ||
| 875 | + size_t remaining_size() const { | ||
| 876 | + return std::count_if( | ||
| 877 | + std::begin(missing_), std::end(missing_), | ||
| 878 | + [](const std::pair<detail::Classifer, std::string> &val) { | ||
| 879 | + return val.first != detail::Classifer::POSITIONAL_MARK; | ||
| 880 | + }); | ||
| 881 | + } | ||
| 882 | + | ||
| 875 | ///@} | 883 | ///@} |
| 876 | 884 | ||
| 877 | protected: | 885 | protected: |
| @@ -1020,17 +1028,10 @@ class App { | @@ -1020,17 +1028,10 @@ class App { | ||
| 1020 | 1028 | ||
| 1021 | // Convert missing (pairs) to extras (string only) | 1029 | // Convert missing (pairs) to extras (string only) |
| 1022 | if(parent_ == nullptr) { | 1030 | if(parent_ == nullptr) { |
| 1023 | - args.resize(missing()->size()); | ||
| 1024 | - std::transform(std::begin(*missing()), | ||
| 1025 | - std::end(*missing()), | ||
| 1026 | - std::begin(args), | ||
| 1027 | - [](const std::pair<detail::Classifer, std::string> &val) { return val.second; }); | 1031 | + args = remaining(); |
| 1028 | std::reverse(std::begin(args), std::end(args)); | 1032 | std::reverse(std::begin(args), std::end(args)); |
| 1029 | 1033 | ||
| 1030 | - size_t num_left_over = std::count_if( | ||
| 1031 | - std::begin(*missing()), std::end(*missing()), [](std::pair<detail::Classifer, std::string> &val) { | ||
| 1032 | - return val.first != detail::Classifer::POSITIONAL_MARK; | ||
| 1033 | - }); | 1034 | + size_t num_left_over = remaining_size(); |
| 1034 | 1035 | ||
| 1035 | if(num_left_over > 0 && !(allow_extras_ || prefix_command_)) | 1036 | if(num_left_over > 0 && !(allow_extras_ || prefix_command_)) |
| 1036 | throw ExtrasError("[" + detail::rjoin(args, " ") + "]"); | 1037 | throw ExtrasError("[" + detail::rjoin(args, " ") + "]"); |
tests/AppTest.cpp
| @@ -917,10 +917,10 @@ TEST_F(TApp, AllowExtras) { | @@ -917,10 +917,10 @@ TEST_F(TApp, AllowExtras) { | ||
| 917 | EXPECT_FALSE(val); | 917 | EXPECT_FALSE(val); |
| 918 | 918 | ||
| 919 | args = {"-x", "-f"}; | 919 | args = {"-x", "-f"}; |
| 920 | - std::vector<std::string> left_over; | ||
| 921 | - EXPECT_NO_THROW({ left_over = run(); }); | 920 | + |
| 921 | + EXPECT_NO_THROW(run()); | ||
| 922 | EXPECT_TRUE(val); | 922 | EXPECT_TRUE(val); |
| 923 | - EXPECT_EQ(std::vector<std::string>({"-x"}), left_over); | 923 | + EXPECT_EQ(app.remaining(), std::vector<std::string>({"-x"})); |
| 924 | } | 924 | } |
| 925 | 925 | ||
| 926 | TEST_F(TApp, AllowExtrasOrder) { | 926 | TEST_F(TApp, AllowExtrasOrder) { |
| @@ -928,14 +928,13 @@ TEST_F(TApp, AllowExtrasOrder) { | @@ -928,14 +928,13 @@ TEST_F(TApp, AllowExtrasOrder) { | ||
| 928 | app.allow_extras(); | 928 | app.allow_extras(); |
| 929 | 929 | ||
| 930 | args = {"-x", "-f"}; | 930 | args = {"-x", "-f"}; |
| 931 | - std::vector<std::string> left_over; | ||
| 932 | - EXPECT_NO_THROW({ left_over = run(); }); | ||
| 933 | - EXPECT_EQ(std::vector<std::string>({"-f", "-x"}), left_over); | 931 | + EXPECT_NO_THROW(run()); |
| 932 | + EXPECT_EQ(app.remaining(), std::vector<std::string>({"-x", "-f"})); | ||
| 934 | app.reset(); | 933 | app.reset(); |
| 935 | 934 | ||
| 936 | - std::vector<std::string> left_over_2; | ||
| 937 | - left_over_2 = app.parse(left_over); | ||
| 938 | - EXPECT_EQ(left_over, left_over_2); | 935 | + std::vector<std::string> left_over = app.remaining(); |
| 936 | + app.parse(left_over); | ||
| 937 | + EXPECT_EQ(app.remaining(), left_over); | ||
| 939 | } | 938 | } |
| 940 | 939 | ||
| 941 | // Test horrible error | 940 | // Test horrible error |
tests/SubcommandTest.cpp
| @@ -339,9 +339,9 @@ TEST_F(TApp, PrefixProgram) { | @@ -339,9 +339,9 @@ TEST_F(TApp, PrefixProgram) { | ||
| 339 | app.add_flag("--simple"); | 339 | app.add_flag("--simple"); |
| 340 | 340 | ||
| 341 | args = {"--simple", "other", "--simple", "--mine"}; | 341 | args = {"--simple", "other", "--simple", "--mine"}; |
| 342 | - auto ret_args = run(); | 342 | + run(); |
| 343 | 343 | ||
| 344 | - EXPECT_EQ(ret_args, std::vector<std::string>({"--mine", "--simple", "other"})); | 344 | + EXPECT_EQ(app.remaining(), std::vector<std::string>({"other", "--simple", "--mine"})); |
| 345 | } | 345 | } |
| 346 | 346 | ||
| 347 | struct SubcommandProgram : public TApp { | 347 | struct SubcommandProgram : public TApp { |
tests/app_helper.hpp
| @@ -15,10 +15,10 @@ struct TApp : public ::testing::Test { | @@ -15,10 +15,10 @@ struct TApp : public ::testing::Test { | ||
| 15 | CLI::App app{"My Test Program"}; | 15 | CLI::App app{"My Test Program"}; |
| 16 | input_t args; | 16 | input_t args; |
| 17 | 17 | ||
| 18 | - std::vector<std::string> run() { | 18 | + void run() { |
| 19 | input_t newargs = args; | 19 | input_t newargs = args; |
| 20 | std::reverse(std::begin(newargs), std::end(newargs)); | 20 | std::reverse(std::begin(newargs), std::end(newargs)); |
| 21 | - return app.parse(newargs); | 21 | + app.parse(newargs); |
| 22 | } | 22 | } |
| 23 | }; | 23 | }; |
| 24 | 24 |