1
0
mirror of https://github.com/CLIUtils/CLI11.git synced 2025-04-29 12:13:52 +00:00

Float parsing negative (#1140)

Fixes #1139 
Fix a bug relating to parsing negative floating point values with no
zero before decimal point in a context where an option could be
possible.

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
This commit is contained in:
Philip Top 2025-03-11 07:08:38 -07:00 committed by GitHub
parent f75fd22ba3
commit f871b625ee
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 28 additions and 1 deletions

View File

@ -1090,7 +1090,9 @@ CLI11_NODISCARD CLI11_INLINE detail::Classifier App::_recognize(const std::strin
if(detail::split_long(current, dummy1, dummy2))
return detail::Classifier::LONG;
if(detail::split_short(current, dummy1, dummy2)) {
if(dummy1[0] >= '0' && dummy1[0] <= '9') {
if((dummy1[0] >= '0' && dummy1[0] <= '9') ||
(dummy1[0] == '.' && !dummy2.empty() && (dummy2[0] >= '0' && dummy2[0] <= '9'))) {
// it looks like a number but check if it could be an option
if(get_option_no_throw(std::string{'-', dummy1[0]}) == nullptr) {
return detail::Classifier::NONE;
}

View File

@ -232,6 +232,9 @@ static const std::map<std::string, double> testValuesDouble{
{"-3.14159 ", -3.14159},
{"+1.0", 1.0},
{"-0.01", -0.01},
{"-.01", -0.01},
{"-.3251", -0.3251},
{"+.3251", 0.3251},
{"5e22", 5e22},
{" 5e22", 5e22},
{" 5e22 ", 5e22},
@ -822,6 +825,28 @@ TEST_CASE_METHOD(TApp, "floatPair", "[optiontype]") {
CHECK(2.7f == Approx(custom_opt.second));
}
// now with tuple support this is possible
TEST_CASE_METHOD(TApp, "doubleVector", "[optiontype]") {
std::vector<double> custom_opt;
app.add_option("--fp", custom_opt);
args = {"--fp", "12.7", "1.5"};
run();
CHECK(12.7 == Approx(custom_opt[0]));
CHECK(1.5 == Approx(custom_opt[1]));
args = {"--fp", "12.7", "-.5"};
run();
CHECK(12.7 == Approx(custom_opt[0]));
CHECK(-0.5 == Approx(custom_opt[1]));
args = {"--fp", "-.7", "+.5"};
run();
CHECK(-0.7 == Approx(custom_opt[0]));
CHECK(0.5 == Approx(custom_opt[1]));
}
// now with independent type sizes and expected this is possible
TEST_CASE_METHOD(TApp, "vectorPair", "[optiontype]") {