Commit d47101a101793c1610cf0a0b8174eb6973ed9873
1 parent
ca6e9f70
Allow leading zeroes in integers
Fixes #101. Allows leading zeroes in the integer parser.
Showing
3 changed files
with
32 additions
and
2 deletions
CHANGELOG.md
include/cxxopts.hpp
| ... | ... | @@ -447,7 +447,7 @@ namespace cxxopts |
| 447 | 447 | namespace |
| 448 | 448 | { |
| 449 | 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 | 451 | std::basic_regex<char> truthy_pattern |
| 452 | 452 | ("(t|T)(rue)?"); |
| 453 | 453 | std::basic_regex<char> falsy_pattern | ... | ... |
test/options.cpp
| ... | ... | @@ -304,6 +304,30 @@ TEST_CASE("Integers", "[options]") |
| 304 | 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 | 331 | TEST_CASE("Unsigned integers", "[options]") |
| 308 | 332 | { |
| 309 | 333 | cxxopts::Options options("parses_unsigned", "detects unsigned errors"); |
| ... | ... | @@ -502,4 +526,4 @@ TEST_CASE("Unrecognised options", "[options]") { |
| 502 | 526 | REQUIRE(argc == 3); |
| 503 | 527 | CHECK_THAT(argv[1], Catch::Equals("--unknown")); |
| 504 | 528 | } |
| 505 | -} | |
| 506 | 529 | \ No newline at end of file |
| 530 | +} | ... | ... |