Commit bd205738293d30c1e4ad6ec3ace3408831b424b3

Authored by Jean-Baptiste Bayle
Committed by jarro2783
1 parent d58271c5

Parse 0 and 1 into booleans (#177)

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