1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 09:38:11 +00:00

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.
This commit is contained in:
Diego Iastrubni 2020-07-25 20:23:05 +03:00 committed by Andreas Kling
parent 305e2ef69c
commit 64c15798e7
2 changed files with 23 additions and 0 deletions

View file

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