Commit 30a91b4a72fbeec06aa260a8d69e8f2de4e74bf0

Authored by Henry Fredrick Schreiner
1 parent 5f3e20a9

Adding positional handling

include/CLI.hpp
... ... @@ -541,6 +541,9 @@ public:
541 541 throw CallForHelp();
542 542 }
543 543  
  544 + // Positionals end up being inserted in the correct order, but we want to pop them off the back end
  545 + std::reverse(std::begin(positionals), std::end(positionals));
  546 +
544 547 for(Option& opt : options) {
545 548 while (opt.positional() && opt.count() < opt.expected() && positionals.size() > 0) {
546 549 opt.get_new();
... ...
tests/CLITest.cpp
... ... @@ -143,6 +143,40 @@ TEST_F(TApp, ShortOpts) {
143 143 EXPECT_EQ("zyz", someopt);
144 144 }
145 145  
  146 +TEST_F(TApp, Positionals) {
  147 +
  148 + std::string posit1;
  149 + std::string posit2;
  150 + app.add_option("posit1", posit1, "", CLI::POSITIONAL);
  151 + app.add_option("posit2", posit2, "", CLI::POSITIONAL);
  152 +
  153 + args = {"thing1","thing2"};
  154 +
  155 + run();
  156 +
  157 + EXPECT_EQ(1, app.count("posit1"));
  158 + EXPECT_EQ(1, app.count("posit2"));
  159 + EXPECT_EQ("thing1", posit1);
  160 + EXPECT_EQ("thing2", posit2);
  161 +}
  162 +
  163 +TEST_F(TApp, MixedPositionals) {
  164 +
  165 + int positional_int;
  166 + std::string positional_string;
  167 + app.add_option("posit1", positional_int, "", CLI::POSITIONAL);
  168 + app.add_option("posit2", positional_string, "", CLI::POSITIONAL);
  169 +
  170 + args = {"--posit2","thing2","7"};
  171 +
  172 + run();
  173 +
  174 + EXPECT_EQ(1, app.count("posit2"));
  175 + EXPECT_EQ(1, app.count("posit1"));
  176 + EXPECT_EQ(7, positional_int);
  177 + EXPECT_EQ("thing2", positional_string);
  178 +}
  179 +
146 180 TEST_F(TApp, Reset) {
147 181  
148 182 app.add_flag("simple");
... ... @@ -241,4 +275,4 @@ TEST_F(SubcommandProgram, SpareSub) {
241 275 // TODO: Add default/type info to help
242 276 // TODO: Add set checking
243 277 // TODO: Try all of the options together
244   -
  278 +// TODO: Add make_option alternative with type
... ...