LibCore: Implement integral ArgsParser positionals through a template

This matches the behaviour of options.
This commit is contained in:
Fabian Dellwing 2023-04-27 12:58:52 +02:00 committed by Ali Mohammad Pur
parent f4ed9d5d83
commit 4ebc7306da
Notes: sideshowbarker 2024-07-17 02:05:41 +09:00
2 changed files with 19 additions and 21 deletions

View File

@ -610,37 +610,35 @@ void ArgsParser::add_positional_argument(StringView& value, char const* help_str
add_positional_argument(move(arg)); add_positional_argument(move(arg));
} }
void ArgsParser::add_positional_argument(int& value, char const* help_string, char const* name, Required required) template<Integral I>
void ArgsParser::add_positional_argument(I& value, char const* help_string, char const* name, Required required)
{ {
Arg arg { Arg arg {
help_string, help_string,
name, name,
required == Required::Yes ? 1 : 0, required == Required::Yes ? 1 : 0,
1, 1,
[&value](StringView s) { [&value](StringView view) {
auto opt = s.to_int(); Optional<I> opt;
if constexpr (IsSigned<I>)
opt = view.to_int<I>();
else
opt = view.to_uint<I>();
value = opt.value_or(0); value = opt.value_or(0);
return opt.has_value(); return opt.has_value();
} },
}; };
add_positional_argument(move(arg)); add_positional_argument(move(arg));
} }
void ArgsParser::add_positional_argument(unsigned& value, char const* help_string, char const* name, Required required) template void ArgsParser::add_positional_argument(int&, char const*, char const*, Required);
{ template void ArgsParser::add_positional_argument(long&, char const*, char const*, Required);
Arg arg { template void ArgsParser::add_positional_argument(long long&, char const*, char const*, Required);
help_string, template void ArgsParser::add_positional_argument(short&, char const*, char const*, Required);
name, template void ArgsParser::add_positional_argument(unsigned&, char const*, char const*, Required);
required == Required::Yes ? 1 : 0, template void ArgsParser::add_positional_argument(unsigned long&, char const*, char const*, Required);
1, template void ArgsParser::add_positional_argument(unsigned long long&, char const*, char const*, Required);
[&value](StringView s) { template void ArgsParser::add_positional_argument(unsigned short&, char const*, char const*, Required);
auto opt = s.to_uint();
value = opt.value_or(0);
return opt.has_value();
}
};
add_positional_argument(move(arg));
}
void ArgsParser::add_positional_argument(double& value, char const* help_string, char const* name, Required required) void ArgsParser::add_positional_argument(double& value, char const* help_string, char const* name, Required required)
{ {

View File

@ -102,8 +102,8 @@ public:
void add_positional_argument(Arg&&); 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(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(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); template<Integral I>
void add_positional_argument(unsigned& value, char const* help_string, char const* name, Required required = Required::Yes); 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(double& value, char const* help_string, char const* name, Required required = Required::Yes);
void add_positional_argument(Vector<DeprecatedString>& value, char const* help_string, char const* name, Required required = Required::Yes); void add_positional_argument(Vector<DeprecatedString>& value, char const* help_string, char const* name, Required required = Required::Yes);
void add_positional_argument(Vector<StringView>& value, char const* help_string, char const* name, Required required = Required::Yes); void add_positional_argument(Vector<StringView>& value, char const* help_string, char const* name, Required required = Required::Yes);