From af79da5d843421de2460dc221ed285cf56d61ad6 Mon Sep 17 00:00:00 2001 From: Henry Fredrick Schreiner Date: Thu, 9 Mar 2017 09:41:10 -0500 Subject: [PATCH] Fix unlimited positionals --- include/CLI/App.hpp | 3 ++- tests/AppTest.cpp | 20 ++++++++++++++++++-- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/include/CLI/App.hpp b/include/CLI/App.hpp index 686e3ccf..7131f25e 100644 --- a/include/CLI/App.hpp +++ b/include/CLI/App.hpp @@ -895,7 +895,8 @@ protected: for(const Option_p& opt : options_) { // Eat options, one by one, until done if ( opt->get_positional() - && opt->count() < opt->get_expected() + && (opt->count() < opt->get_expected() + || opt->get_expected() < 0) ) { opt->add_result(positional); diff --git a/tests/AppTest.cpp b/tests/AppTest.cpp index fea1d282..e4e822d3 100644 --- a/tests/AppTest.cpp +++ b/tests/AppTest.cpp @@ -248,7 +248,7 @@ TEST_F(TApp, Positionals) { TEST_F(TApp, ForcedPositional) { std::vector posit; auto one = app.add_flag("--one"); - auto pos = app.add_option("posit", posit)->expected(2); // Expected -1 broken? + app.add_option("posit", posit); args = {"--one", "two", "three"}; run(); @@ -260,7 +260,6 @@ TEST_F(TApp, ForcedPositional) { args = {"--", "--one", "two", "three"}; std::vector answers2 = {"--one", "two", "three"}; - pos->expected(3); run(); EXPECT_FALSE(one->count()); @@ -285,6 +284,23 @@ TEST_F(TApp, MixedPositionals) { EXPECT_EQ("thing2", positional_string); } +TEST_F(TApp, BigPositional) { + std::vector vec; + app.add_option("pos", vec); + + args = {"one"}; + + run(); + EXPECT_EQ(args, vec); + + app.reset(); + + args = {"one", "two"}; + run(); + + EXPECT_EQ(args, vec); +} + TEST_F(TApp, Reset) { app.add_flag("--simple");