Commit d47101a101793c1610cf0a0b8174eb6973ed9873

Authored by Jarryd Beck
1 parent ca6e9f70

Allow leading zeroes in integers

Fixes #101. Allows leading zeroes in the integer parser.
CHANGELOG.md
@@ -3,6 +3,12 @@ @@ -3,6 +3,12 @@
3 This is the changelog for `cxxopts`, a C++11 library for parsing command line 3 This is the changelog for `cxxopts`, a C++11 library for parsing command line
4 options. The project adheres to semantic versioning. 4 options. The project adheres to semantic versioning.
5 5
  6 +## 2.2
  7 +
  8 +### Changed
  9 +
  10 +* Allow integers to have leading zeroes.
  11 +
6 ## 2.1.1 12 ## 2.1.1
7 13
8 ### Bug Fixes 14 ### Bug Fixes
include/cxxopts.hpp
@@ -447,7 +447,7 @@ namespace cxxopts @@ -447,7 +447,7 @@ namespace cxxopts
447 namespace 447 namespace
448 { 448 {
449 std::basic_regex<char> integer_pattern 449 std::basic_regex<char> integer_pattern
450 - ("(-)?(0x)?([1-9a-zA-Z][0-9a-zA-Z]*)|((0x)?0)"); 450 + ("(-)?(0x)?([0-9a-zA-Z]+)|((0x)?0)");
451 std::basic_regex<char> truthy_pattern 451 std::basic_regex<char> truthy_pattern
452 ("(t|T)(rue)?"); 452 ("(t|T)(rue)?");
453 std::basic_regex<char> falsy_pattern 453 std::basic_regex<char> falsy_pattern
test/options.cpp
@@ -304,6 +304,30 @@ TEST_CASE(&quot;Integers&quot;, &quot;[options]&quot;) @@ -304,6 +304,30 @@ TEST_CASE(&quot;Integers&quot;, &quot;[options]&quot;)
304 CHECK(positional[6] == 0x0); 304 CHECK(positional[6] == 0x0);
305 } 305 }
306 306
  307 +TEST_CASE("Leading zero integers", "[options]")
  308 +{
  309 + cxxopts::Options options("parses_integers", "parses integers correctly");
  310 + options.add_options()
  311 + ("positional", "Integers", cxxopts::value<std::vector<int>>());
  312 +
  313 + Argv av({"ints", "--", "05", "06", "0x0ab", "0x0001"});
  314 +
  315 + char** argv = av.argv();
  316 + auto argc = av.argc();
  317 +
  318 + options.parse_positional("positional");
  319 + auto result = options.parse(argc, argv);
  320 +
  321 + REQUIRE(result.count("positional") == 4);
  322 +
  323 + auto& positional = result["positional"].as<std::vector<int>>();
  324 + REQUIRE(positional.size() == 4);
  325 + CHECK(positional[0] == 5);
  326 + CHECK(positional[1] == 6);
  327 + CHECK(positional[2] == 0xab);
  328 + CHECK(positional[3] == 0x1);
  329 +}
  330 +
307 TEST_CASE("Unsigned integers", "[options]") 331 TEST_CASE("Unsigned integers", "[options]")
308 { 332 {
309 cxxopts::Options options("parses_unsigned", "detects unsigned errors"); 333 cxxopts::Options options("parses_unsigned", "detects unsigned errors");
@@ -502,4 +526,4 @@ TEST_CASE(&quot;Unrecognised options&quot;, &quot;[options]&quot;) { @@ -502,4 +526,4 @@ TEST_CASE(&quot;Unrecognised options&quot;, &quot;[options]&quot;) {
502 REQUIRE(argc == 3); 526 REQUIRE(argc == 3);
503 CHECK_THAT(argv[1], Catch::Equals("--unknown")); 527 CHECK_THAT(argv[1], Catch::Equals("--unknown"));
504 } 528 }
505 -}  
506 \ No newline at end of file 529 \ No newline at end of file
  530 +}