From 30a91b4a72fbeec06aa260a8d69e8f2de4e74bf0 Mon Sep 17 00:00:00 2001 From: Henry Fredrick Schreiner Date: Sun, 29 Jan 2017 20:10:36 -0500 Subject: [PATCH] Adding positional handling --- include/CLI.hpp | 3 +++ tests/CLITest.cpp | 36 +++++++++++++++++++++++++++++++++++- 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/include/CLI.hpp b/include/CLI.hpp index 41c7c904..0abeb99a 100644 --- a/include/CLI.hpp +++ b/include/CLI.hpp @@ -541,6 +541,9 @@ public: throw CallForHelp(); } + // Positionals end up being inserted in the correct order, but we want to pop them off the back end + std::reverse(std::begin(positionals), std::end(positionals)); + for(Option& opt : options) { while (opt.positional() && opt.count() < opt.expected() && positionals.size() > 0) { opt.get_new(); diff --git a/tests/CLITest.cpp b/tests/CLITest.cpp index a22251a8..44607586 100644 --- a/tests/CLITest.cpp +++ b/tests/CLITest.cpp @@ -143,6 +143,40 @@ TEST_F(TApp, ShortOpts) { EXPECT_EQ("zyz", someopt); } +TEST_F(TApp, Positionals) { + + std::string posit1; + std::string posit2; + app.add_option("posit1", posit1, "", CLI::POSITIONAL); + app.add_option("posit2", posit2, "", CLI::POSITIONAL); + + args = {"thing1","thing2"}; + + run(); + + EXPECT_EQ(1, app.count("posit1")); + EXPECT_EQ(1, app.count("posit2")); + EXPECT_EQ("thing1", posit1); + EXPECT_EQ("thing2", posit2); +} + +TEST_F(TApp, MixedPositionals) { + + int positional_int; + std::string positional_string; + app.add_option("posit1", positional_int, "", CLI::POSITIONAL); + app.add_option("posit2", positional_string, "", CLI::POSITIONAL); + + args = {"--posit2","thing2","7"}; + + run(); + + EXPECT_EQ(1, app.count("posit2")); + EXPECT_EQ(1, app.count("posit1")); + EXPECT_EQ(7, positional_int); + EXPECT_EQ("thing2", positional_string); +} + TEST_F(TApp, Reset) { app.add_flag("simple"); @@ -241,4 +275,4 @@ TEST_F(SubcommandProgram, SpareSub) { // TODO: Add default/type info to help // TODO: Add set checking // TODO: Try all of the options together - +// TODO: Add make_option alternative with type