Skip to content
This repository was archived by the owner on Oct 28, 2021. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 additions & 3 deletions include/clara.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ namespace detail {

virtual auto isFlag() const -> bool = 0;

virtual auto isContainer() const -> bool { return false; }
virtual auto allowMulti() const -> bool { return false; }

virtual auto setValue(std::string const &arg) -> ParserResult = 0;

Expand Down Expand Up @@ -375,7 +375,7 @@ namespace detail {

explicit BoundRef(std::vector<T> &ref) : m_ref(ref) {}

auto isContainer() const -> bool override { return true; }
auto allowMulti() const -> bool override { return true; }

auto setValue(std::string const &arg) -> ParserResult override {
T temp;
Expand All @@ -397,6 +397,21 @@ namespace detail {
}
};

template<typename T>
struct BoundMultiFlagRef : BoundFlagRefBase {
T &m_ref;

explicit BoundMultiFlagRef(T &ref) : m_ref(ref) {}

auto allowMulti() const -> bool override { return true; }

auto setFlag(bool flag) -> ParserResult override {
if (flag)
++m_ref;
return ParserResult::ok(ParseResultType::Matched);
}
};

template<typename ReturnType>
struct LambdaInvoker {
static_assert(std::is_same<ReturnType, ParserResult>::value, "Lambda must return void or clara::ParserResult");
Expand Down Expand Up @@ -516,7 +531,7 @@ namespace detail {
}

auto cardinality() const -> size_t override {
if (m_ref->isContainer())
if (m_ref->allowMulti())
return 0;
else
return 1;
Expand Down Expand Up @@ -606,6 +621,9 @@ namespace detail {

explicit Opt( bool &ref ) : ParserRefImpl(std::make_shared<BoundFlagRef>(ref)) {}

template<typename T>
explicit Opt( T &ref ) : ParserRefImpl(std::make_shared<BoundMultiFlagRef<T>>(ref)) {}

template<typename LambdaT>
Opt( LambdaT const &ref, std::string const &hint ) : ParserRefImpl( ref, hint ) {}

Expand Down
9 changes: 8 additions & 1 deletion src/ClaraTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ struct Config {
std::vector<std::string> m_tests;
bool m_flag = false;
double m_value = 0;
int m_flagCount = 0;
};

TEST_CASE( "Combined parser" ) {
Expand All @@ -82,6 +83,9 @@ TEST_CASE( "Combined parser" ) {
+ Opt( [&]( double value ){ config.m_value = value; }, "number" )
["-d"]["--double"]
( "just some number" )
+ Opt( config.m_flagCount )
["-v"]
( "a flag to set multiple times" )
+ Arg( config.m_tests, "test name|tags|pattern" )
( "which test or tests to use" );

Expand All @@ -99,16 +103,19 @@ TEST_CASE( "Combined parser" ) {
" -n, --name <name> the name to use\n"
" -f, --flag a flag to set\n"
" -d, --double <number> just some number\n"
" -v a flag to set multiple times\n"
);
}
SECTION( "some args" ) {
auto result = parser.parse( Args{ "TestApp", "-n", "Bill", "-d:123.45", "-f", "test1", "test2" } );
auto result = parser.parse( Args{ "TestApp", "-n", "Bill", "-d:123.45", "-f", "-vvv", "test1", "test2" } );
CHECK( result );
CHECK( result.value().type() == ParseResultType::Matched );

REQUIRE( config.m_name == "Bill" );
REQUIRE( config.m_value == 123.45 );
REQUIRE( config.m_tests == std::vector<std::string> { "test1", "test2" } );
REQUIRE( config.m_flag );
REQUIRE( config.m_flagCount == 3 );
CHECK( showHelp == false );
}
SECTION( "help" ) {
Expand Down