Skip to content
This repository has been archived by the owner on Oct 28, 2021. It is now read-only.

Commit

Permalink
Add missing <optional> include when support is detected
Browse files Browse the repository at this point in the history
Also adds tests and closes #45
  • Loading branch information
horenmar committed Mar 8, 2018
1 parent adb5ec3 commit bd21f70
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
1 change: 1 addition & 0 deletions include/clara.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#ifndef CLARA_CONFIG_OPTIONAL_TYPE
# ifdef __has_include
# if __has_include(<optional>) && __cplusplus >= 201703L
# include <optional>
# define CLARA_CONFIG_OPTIONAL_TYPE std::optional
# endif
# endif
Expand Down
22 changes: 21 additions & 1 deletion src/ClaraTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ TEST_CASE( "cmdline" ) {

REQUIRE(config.flag == false);
}

SECTION( "arg before flag" )
{
auto result = cli.parse({ "TestApp", "-f", "something" });
Expand Down Expand Up @@ -540,3 +540,23 @@ TEST_CASE( "newlines in description" ) {

}
}

#if defined(CLARA_CONFIG_OPTIONAL_TYPE)
TEST_CASE("Reading into std::optional") {
CLARA_CONFIG_OPTIONAL_TYPE<std::string> name;
auto p = Opt(name, "name")
["-n"]["--name"]
("the name to use");
SECTION("Not set") {
auto result = p.parse(Args{ "TestApp", "-q", "Pixie" });
REQUIRE( result );
REQUIRE_FALSE( name.has_value() );
}
SECTION("Provided") {
auto result = p.parse(Args{ "TestApp", "-n", "Pixie" });
REQUIRE( result );
REQUIRE( name.has_value() );
REQUIRE( name.value() == "Pixie" );
}
}
#endif // CLARA_CONFIG_OPTIONAL_TYPE

0 comments on commit bd21f70

Please sign in to comment.