From 64c15798e74ca1097c8553fc7b3d0603856aa136 Mon Sep 17 00:00:00 2001 From: Diego Iastrubni Date: Sat, 25 Jul 2020 20:23:05 +0300 Subject: LibCore: Add support for double on argparse Code is pretty trivial. If someone needs "float" support, a copy-paste will be in place. Build system was confused between math.h from rootfs, and toolchain. I fixed the problem caused by `math.h` by locally using the builtin `isnan()` from the compiler. It's ugly - but works. I am looking for other alternatives. --- Libraries/LibCore/ArgsParser.cpp | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) (limited to 'Libraries/LibCore/ArgsParser.cpp') diff --git a/Libraries/LibCore/ArgsParser.cpp b/Libraries/LibCore/ArgsParser.cpp index a256376b94..8ddbfdd883 100644 --- a/Libraries/LibCore/ArgsParser.cpp +++ b/Libraries/LibCore/ArgsParser.cpp @@ -324,6 +324,28 @@ void ArgsParser::add_positional_argument(int& value, const char* help_string, co add_positional_argument(move(arg)); } +static constexpr bool isnan(double __x) { return __builtin_isnan(__x); } + +void ArgsParser::add_positional_argument(double& value, const char* help_string, const char* name, Required required) +{ + Arg arg { + help_string, + name, + required == Required::Yes ? 1 : 0, + 1, + [&value](const char* s) { + char *p; + double v = strtod(s, &p); + bool valid_value = !isnan(v) && p != s; + if (valid_value) { + value = v; + } + return valid_value; + } + }; + add_positional_argument(move(arg)); +} + void ArgsParser::add_positional_argument(Vector& values, const char* help_string, const char* name, Required required) { Arg arg { -- cgit v1.2.3