Commit bd205738293d30c1e4ad6ec3ace3408831b424b3
Committed by
jarro2783
1 parent
d58271c5
Parse 0 and 1 into booleans (#177)
* Parse 1 as "true" and 0 as "false" for boolean options.
Showing
2 changed files
with
9 additions
and
3 deletions
include/cxxopts.hpp
| @@ -466,9 +466,9 @@ namespace cxxopts | @@ -466,9 +466,9 @@ namespace cxxopts | ||
| 466 | std::basic_regex<char> integer_pattern | 466 | std::basic_regex<char> integer_pattern |
| 467 | ("(-)?(0x)?([0-9a-zA-Z]+)|((0x)?0)"); | 467 | ("(-)?(0x)?([0-9a-zA-Z]+)|((0x)?0)"); |
| 468 | std::basic_regex<char> truthy_pattern | 468 | std::basic_regex<char> truthy_pattern |
| 469 | - ("(t|T)(rue)?"); | 469 | + ("(t|T)(rue)?|1"); |
| 470 | std::basic_regex<char> falsy_pattern | 470 | std::basic_regex<char> falsy_pattern |
| 471 | - ("((f|F)(alse)?)?"); | 471 | + ("(f|F)(alse)?|0"); |
| 472 | } | 472 | } |
| 473 | 473 | ||
| 474 | namespace detail | 474 | namespace detail |
test/options.cpp
| @@ -468,6 +468,8 @@ TEST_CASE("Booleans", "[boolean]") { | @@ -468,6 +468,8 @@ TEST_CASE("Booleans", "[boolean]") { | ||
| 468 | ("bool", "A Boolean", cxxopts::value<bool>()) | 468 | ("bool", "A Boolean", cxxopts::value<bool>()) |
| 469 | ("debug", "Debugging", cxxopts::value<bool>()) | 469 | ("debug", "Debugging", cxxopts::value<bool>()) |
| 470 | ("timing", "Timing", cxxopts::value<bool>()) | 470 | ("timing", "Timing", cxxopts::value<bool>()) |
| 471 | + ("verbose", "Verbose", cxxopts::value<bool>()) | ||
| 472 | + ("dry-run", "Dry Run", cxxopts::value<bool>()) | ||
| 471 | ("noExplicitDefault", "No Explicit Default", cxxopts::value<bool>()) | 473 | ("noExplicitDefault", "No Explicit Default", cxxopts::value<bool>()) |
| 472 | ("defaultTrue", "Timing", cxxopts::value<bool>()->default_value("true")) | 474 | ("defaultTrue", "Timing", cxxopts::value<bool>()->default_value("true")) |
| 473 | ("defaultFalse", "Timing", cxxopts::value<bool>()->default_value("false")) | 475 | ("defaultFalse", "Timing", cxxopts::value<bool>()->default_value("false")) |
| @@ -476,7 +478,7 @@ TEST_CASE("Booleans", "[boolean]") { | @@ -476,7 +478,7 @@ TEST_CASE("Booleans", "[boolean]") { | ||
| 476 | 478 | ||
| 477 | options.parse_positional("others"); | 479 | options.parse_positional("others"); |
| 478 | 480 | ||
| 479 | - Argv av({"booleans", "--bool=false", "--debug=true", "--timing", "extra"}); | 481 | + Argv av({"booleans", "--bool=false", "--debug=true", "--timing", "--verbose=1", "--dry-run=0", "extra"}); |
| 480 | 482 | ||
| 481 | char** argv = av.argv(); | 483 | char** argv = av.argv(); |
| 482 | auto argc = av.argc(); | 484 | auto argc = av.argc(); |
| @@ -486,6 +488,8 @@ TEST_CASE("Booleans", "[boolean]") { | @@ -486,6 +488,8 @@ TEST_CASE("Booleans", "[boolean]") { | ||
| 486 | REQUIRE(result.count("bool") == 1); | 488 | REQUIRE(result.count("bool") == 1); |
| 487 | REQUIRE(result.count("debug") == 1); | 489 | REQUIRE(result.count("debug") == 1); |
| 488 | REQUIRE(result.count("timing") == 1); | 490 | REQUIRE(result.count("timing") == 1); |
| 491 | + REQUIRE(result.count("verbose") == 1); | ||
| 492 | + REQUIRE(result.count("dry-run") == 1); | ||
| 489 | REQUIRE(result.count("noExplicitDefault") == 0); | 493 | REQUIRE(result.count("noExplicitDefault") == 0); |
| 490 | REQUIRE(result.count("defaultTrue") == 0); | 494 | REQUIRE(result.count("defaultTrue") == 0); |
| 491 | REQUIRE(result.count("defaultFalse") == 0); | 495 | REQUIRE(result.count("defaultFalse") == 0); |
| @@ -493,6 +497,8 @@ TEST_CASE("Booleans", "[boolean]") { | @@ -493,6 +497,8 @@ TEST_CASE("Booleans", "[boolean]") { | ||
| 493 | CHECK(result["bool"].as<bool>() == false); | 497 | CHECK(result["bool"].as<bool>() == false); |
| 494 | CHECK(result["debug"].as<bool>() == true); | 498 | CHECK(result["debug"].as<bool>() == true); |
| 495 | CHECK(result["timing"].as<bool>() == true); | 499 | CHECK(result["timing"].as<bool>() == true); |
| 500 | + CHECK(result["verbose"].as<bool>() == true); | ||
| 501 | + CHECK(result["dry-run"].as<bool>() == false); | ||
| 496 | CHECK(result["noExplicitDefault"].as<bool>() == false); | 502 | CHECK(result["noExplicitDefault"].as<bool>() == false); |
| 497 | CHECK(result["defaultTrue"].as<bool>() == true); | 503 | CHECK(result["defaultTrue"].as<bool>() == true); |
| 498 | CHECK(result["defaultFalse"].as<bool>() == false); | 504 | CHECK(result["defaultFalse"].as<bool>() == false); |