Add a method to remove the implicit value of an option (#178)
This commit is contained in:
parent
4f3fda4bf9
commit
6b6af4f561
@ -314,6 +314,9 @@ namespace cxxopts
|
|||||||
virtual std::shared_ptr<Value>
|
virtual std::shared_ptr<Value>
|
||||||
implicit_value(const std::string& value) = 0;
|
implicit_value(const std::string& value) = 0;
|
||||||
|
|
||||||
|
virtual std::shared_ptr<Value>
|
||||||
|
no_implicit_value() = 0;
|
||||||
|
|
||||||
virtual bool
|
virtual bool
|
||||||
is_boolean() const = 0;
|
is_boolean() const = 0;
|
||||||
};
|
};
|
||||||
@ -834,6 +837,13 @@ namespace cxxopts
|
|||||||
return shared_from_this();
|
return shared_from_this();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::shared_ptr<Value>
|
||||||
|
no_implicit_value()
|
||||||
|
{
|
||||||
|
m_implicit = false;
|
||||||
|
return shared_from_this();
|
||||||
|
}
|
||||||
|
|
||||||
std::string
|
std::string
|
||||||
get_default_value() const
|
get_default_value() const
|
||||||
{
|
{
|
||||||
|
@ -250,6 +250,67 @@ TEST_CASE("Empty with implicit value", "[implicit]")
|
|||||||
REQUIRE(result["implicit"].as<std::string>() == "");
|
REQUIRE(result["implicit"].as<std::string>() == "");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_CASE("Boolean without implicit value", "[implicit]")
|
||||||
|
{
|
||||||
|
cxxopts::Options options("no_implicit", "bool without an implicit value");
|
||||||
|
options.add_options()
|
||||||
|
("bool", "Boolean without implicit", cxxopts::value<bool>()
|
||||||
|
->no_implicit_value());
|
||||||
|
|
||||||
|
SECTION("When no value provided") {
|
||||||
|
Argv av({"no_implicit", "--bool"});
|
||||||
|
|
||||||
|
char** argv = av.argv();
|
||||||
|
auto argc = av.argc();
|
||||||
|
|
||||||
|
CHECK_THROWS_AS(options.parse(argc, argv), cxxopts::missing_argument_exception&);
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("With equal-separated true") {
|
||||||
|
Argv av({"no_implicit", "--bool=true"});
|
||||||
|
|
||||||
|
char** argv = av.argv();
|
||||||
|
auto argc = av.argc();
|
||||||
|
|
||||||
|
auto result = options.parse(argc, argv);
|
||||||
|
CHECK(result.count("bool") == 1);
|
||||||
|
CHECK(result["bool"].as<bool>() == true);
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("With equal-separated false") {
|
||||||
|
Argv av({"no_implicit", "--bool=false"});
|
||||||
|
|
||||||
|
char** argv = av.argv();
|
||||||
|
auto argc = av.argc();
|
||||||
|
|
||||||
|
auto result = options.parse(argc, argv);
|
||||||
|
CHECK(result.count("bool") == 1);
|
||||||
|
CHECK(result["bool"].as<bool>() == false);
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("With space-separated true") {
|
||||||
|
Argv av({"no_implicit", "--bool", "true"});
|
||||||
|
|
||||||
|
char** argv = av.argv();
|
||||||
|
auto argc = av.argc();
|
||||||
|
|
||||||
|
auto result = options.parse(argc, argv);
|
||||||
|
CHECK(result.count("bool") == 1);
|
||||||
|
CHECK(result["bool"].as<bool>() == true);
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("With space-separated false") {
|
||||||
|
Argv av({"no_implicit", "--bool", "false"});
|
||||||
|
|
||||||
|
char** argv = av.argv();
|
||||||
|
auto argc = av.argc();
|
||||||
|
|
||||||
|
auto result = options.parse(argc, argv);
|
||||||
|
CHECK(result.count("bool") == 1);
|
||||||
|
CHECK(result["bool"].as<bool>() == false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
TEST_CASE("Default values", "[default]")
|
TEST_CASE("Default values", "[default]")
|
||||||
{
|
{
|
||||||
cxxopts::Options options("defaults", "has defaults");
|
cxxopts::Options options("defaults", "has defaults");
|
||||||
|
Loading…
Reference in New Issue
Block a user