diff --git a/CHANGELOG.md b/CHANGELOG.md index 3a88207..76bba8a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,12 @@ This is the changelog for `cxxopts`, a C++11 library for parsing command line options. The project adheres to semantic versioning. +## 2.2 + +### Changed + +* Allow integers to have leading zeroes. + ## 2.1.1 ### Bug Fixes diff --git a/include/cxxopts.hpp b/include/cxxopts.hpp index 59a7178..206e1c7 100644 --- a/include/cxxopts.hpp +++ b/include/cxxopts.hpp @@ -447,7 +447,7 @@ namespace cxxopts namespace { std::basic_regex integer_pattern - ("(-)?(0x)?([1-9a-zA-Z][0-9a-zA-Z]*)|((0x)?0)"); + ("(-)?(0x)?([0-9a-zA-Z]+)|((0x)?0)"); std::basic_regex truthy_pattern ("(t|T)(rue)?"); std::basic_regex falsy_pattern diff --git a/test/options.cpp b/test/options.cpp index bfaa635..044d42b 100644 --- a/test/options.cpp +++ b/test/options.cpp @@ -304,6 +304,30 @@ TEST_CASE("Integers", "[options]") CHECK(positional[6] == 0x0); } +TEST_CASE("Leading zero integers", "[options]") +{ + cxxopts::Options options("parses_integers", "parses integers correctly"); + options.add_options() + ("positional", "Integers", cxxopts::value>()); + + Argv av({"ints", "--", "05", "06", "0x0ab", "0x0001"}); + + char** argv = av.argv(); + auto argc = av.argc(); + + options.parse_positional("positional"); + auto result = options.parse(argc, argv); + + REQUIRE(result.count("positional") == 4); + + auto& positional = result["positional"].as>(); + REQUIRE(positional.size() == 4); + CHECK(positional[0] == 5); + CHECK(positional[1] == 6); + CHECK(positional[2] == 0xab); + CHECK(positional[3] == 0x1); +} + TEST_CASE("Unsigned integers", "[options]") { cxxopts::Options options("parses_unsigned", "detects unsigned errors"); @@ -502,4 +526,4 @@ TEST_CASE("Unrecognised options", "[options]") { REQUIRE(argc == 3); CHECK_THAT(argv[1], Catch::Equals("--unknown")); } -} \ No newline at end of file +}