Allow leading zeroes in integers

Fixes #101. Allows leading zeroes in the integer parser.
This commit is contained in:
Jarryd Beck 2018-05-07 18:18:04 +10:00
parent ca6e9f70eb
commit d47101a101
3 changed files with 32 additions and 2 deletions

View File

@ -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

View File

@ -447,7 +447,7 @@ namespace cxxopts
namespace
{
std::basic_regex<char> integer_pattern
("(-)?(0x)?([1-9a-zA-Z][0-9a-zA-Z]*)|((0x)?0)");
("(-)?(0x)?([0-9a-zA-Z]+)|((0x)?0)");
std::basic_regex<char> truthy_pattern
("(t|T)(rue)?");
std::basic_regex<char> falsy_pattern

View File

@ -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<std::vector<int>>());
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<std::vector<int>>();
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"));
}
}
}