From 4ebc7306daece2ece9088e6e027de506ed0d2653 Mon Sep 17 00:00:00 2001 From: Fabian Dellwing Date: Thu, 27 Apr 2023 12:58:52 +0200 Subject: [PATCH] LibCore: Implement integral ArgsParser positionals through a template This matches the behaviour of options. --- Userland/Libraries/LibCore/ArgsParser.cpp | 36 +++++++++++------------ Userland/Libraries/LibCore/ArgsParser.h | 4 +-- 2 files changed, 19 insertions(+), 21 deletions(-) diff --git a/Userland/Libraries/LibCore/ArgsParser.cpp b/Userland/Libraries/LibCore/ArgsParser.cpp index da6d9ead759..aa3d0be53d8 100644 --- a/Userland/Libraries/LibCore/ArgsParser.cpp +++ b/Userland/Libraries/LibCore/ArgsParser.cpp @@ -610,37 +610,35 @@ void ArgsParser::add_positional_argument(StringView& value, char const* help_str add_positional_argument(move(arg)); } -void ArgsParser::add_positional_argument(int& value, char const* help_string, char const* name, Required required) +template +void ArgsParser::add_positional_argument(I& value, char const* help_string, char const* name, Required required) { Arg arg { help_string, name, required == Required::Yes ? 1 : 0, 1, - [&value](StringView s) { - auto opt = s.to_int(); + [&value](StringView view) { + Optional opt; + if constexpr (IsSigned) + opt = view.to_int(); + else + opt = view.to_uint(); value = opt.value_or(0); return opt.has_value(); - } + }, }; add_positional_argument(move(arg)); } -void ArgsParser::add_positional_argument(unsigned& value, char const* help_string, char const* name, Required required) -{ - Arg arg { - help_string, - name, - required == Required::Yes ? 1 : 0, - 1, - [&value](StringView s) { - auto opt = s.to_uint(); - value = opt.value_or(0); - return opt.has_value(); - } - }; - add_positional_argument(move(arg)); -} +template void ArgsParser::add_positional_argument(int&, char const*, char const*, Required); +template void ArgsParser::add_positional_argument(long&, char const*, char const*, Required); +template void ArgsParser::add_positional_argument(long long&, char const*, char const*, Required); +template void ArgsParser::add_positional_argument(short&, char const*, char const*, Required); +template void ArgsParser::add_positional_argument(unsigned&, char const*, char const*, Required); +template void ArgsParser::add_positional_argument(unsigned long&, char const*, char const*, Required); +template void ArgsParser::add_positional_argument(unsigned long long&, char const*, char const*, Required); +template void ArgsParser::add_positional_argument(unsigned short&, char const*, char const*, Required); void ArgsParser::add_positional_argument(double& value, char const* help_string, char const* name, Required required) { diff --git a/Userland/Libraries/LibCore/ArgsParser.h b/Userland/Libraries/LibCore/ArgsParser.h index ba31e88847e..5c30ff68deb 100644 --- a/Userland/Libraries/LibCore/ArgsParser.h +++ b/Userland/Libraries/LibCore/ArgsParser.h @@ -102,8 +102,8 @@ public: void add_positional_argument(Arg&&); void add_positional_argument(DeprecatedString& value, char const* help_string, char const* name, Required required = Required::Yes); void add_positional_argument(StringView& value, char const* help_string, char const* name, Required required = Required::Yes); - void add_positional_argument(int& value, char const* help_string, char const* name, Required required = Required::Yes); - void add_positional_argument(unsigned& value, char const* help_string, char const* name, Required required = Required::Yes); + template + void add_positional_argument(I& value, char const* help_string, char const* name, Required required = Required::Yes); void add_positional_argument(double& value, char const* help_string, char const* name, Required required = Required::Yes); void add_positional_argument(Vector& value, char const* help_string, char const* name, Required required = Required::Yes); void add_positional_argument(Vector& value, char const* help_string, char const* name, Required required = Required::Yes);