summaryrefslogtreecommitdiff
path: root/Libraries/LibCore/ArgsParser.cpp
diff options
context:
space:
mode:
authorDiego Iastrubni <diegoiast@gmail.com>2020-07-25 20:23:05 +0300
committerAndreas Kling <kling@serenityos.org>2020-08-12 13:57:06 +0200
commit64c15798e74ca1097c8553fc7b3d0603856aa136 (patch)
tree34eb3612a8e8d0c0f1319d4fe838c96f27b52a08 /Libraries/LibCore/ArgsParser.cpp
parent305e2ef69cae0ed43dfddb95ec22c6d5a37c5dd6 (diff)
downloadserenity-64c15798e74ca1097c8553fc7b3d0603856aa136.zip
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.
Diffstat (limited to 'Libraries/LibCore/ArgsParser.cpp')
-rw-r--r--Libraries/LibCore/ArgsParser.cpp22
1 files changed, 22 insertions, 0 deletions
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<const char*>& values, const char* help_string, const char* name, Required required)
{
Arg arg {