Add default "default" value to boolean options (#94)
* Add default "default" value of "false" to boolean options, therefore allowing to call result["boolOpt"].as<bool>() without throwing an exception.
This commit is contained in:
parent
24162899c9
commit
76717cb3dd
@ -855,13 +855,13 @@ namespace cxxopts
|
|||||||
|
|
||||||
standard_value()
|
standard_value()
|
||||||
{
|
{
|
||||||
set_implicit();
|
set_default_and_implicit();
|
||||||
}
|
}
|
||||||
|
|
||||||
standard_value(bool* b)
|
standard_value(bool* b)
|
||||||
: abstract_value(b)
|
: abstract_value(b)
|
||||||
{
|
{
|
||||||
set_implicit();
|
set_default_and_implicit();
|
||||||
}
|
}
|
||||||
|
|
||||||
std::shared_ptr<Value>
|
std::shared_ptr<Value>
|
||||||
@ -873,8 +873,10 @@ namespace cxxopts
|
|||||||
private:
|
private:
|
||||||
|
|
||||||
void
|
void
|
||||||
set_implicit()
|
set_default_and_implicit()
|
||||||
{
|
{
|
||||||
|
m_default = true;
|
||||||
|
m_default_value = "false";
|
||||||
m_implicit = true;
|
m_implicit = true;
|
||||||
m_implicit_value = "true";
|
m_implicit_value = "true";
|
||||||
}
|
}
|
||||||
|
|||||||
@ -423,6 +423,9 @@ TEST_CASE("Booleans", "[boolean]") {
|
|||||||
("bool", "A Boolean", cxxopts::value<bool>())
|
("bool", "A Boolean", cxxopts::value<bool>())
|
||||||
("debug", "Debugging", cxxopts::value<bool>())
|
("debug", "Debugging", cxxopts::value<bool>())
|
||||||
("timing", "Timing", cxxopts::value<bool>())
|
("timing", "Timing", cxxopts::value<bool>())
|
||||||
|
("noExplicitDefault", "No Explicit Default", cxxopts::value<bool>())
|
||||||
|
("defaultTrue", "Timing", cxxopts::value<bool>()->default_value("true"))
|
||||||
|
("defaultFalse", "Timing", cxxopts::value<bool>()->default_value("false"))
|
||||||
("others", "Other arguments", cxxopts::value<std::vector<std::string>>())
|
("others", "Other arguments", cxxopts::value<std::vector<std::string>>())
|
||||||
;
|
;
|
||||||
|
|
||||||
@ -438,10 +441,16 @@ TEST_CASE("Booleans", "[boolean]") {
|
|||||||
REQUIRE(result.count("bool") == 1);
|
REQUIRE(result.count("bool") == 1);
|
||||||
REQUIRE(result.count("debug") == 1);
|
REQUIRE(result.count("debug") == 1);
|
||||||
REQUIRE(result.count("timing") == 1);
|
REQUIRE(result.count("timing") == 1);
|
||||||
|
REQUIRE(result.count("noExplicitDefault") == 1);
|
||||||
|
REQUIRE(result.count("defaultTrue") == 1);
|
||||||
|
REQUIRE(result.count("defaultFalse") == 1);
|
||||||
|
|
||||||
CHECK(result["bool"].as<bool>() == false);
|
CHECK(result["bool"].as<bool>() == false);
|
||||||
CHECK(result["debug"].as<bool>() == true);
|
CHECK(result["debug"].as<bool>() == true);
|
||||||
CHECK(result["timing"].as<bool>() == true);
|
CHECK(result["timing"].as<bool>() == true);
|
||||||
|
CHECK(result["noExplicitDefault"].as<bool>() == false);
|
||||||
|
CHECK(result["defaultTrue"].as<bool>() == true);
|
||||||
|
CHECK(result["defaultFalse"].as<bool>() == false);
|
||||||
|
|
||||||
REQUIRE(result.count("others") == 1);
|
REQUIRE(result.count("others") == 1);
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user