mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 09:37:34 +00:00
LibCore: Add unsigned option to ArgsParser
Unsigned options are used to return underflowed values for negative inputs. With this change, we can verify that unsigned options only accept unsigned inputs as arguments.
This commit is contained in:
parent
9b40208e3b
commit
7db3e962f3
2 changed files with 35 additions and 0 deletions
|
@ -322,6 +322,23 @@ void ArgsParser::add_option(int& value, const char* help_string, const char* lon
|
||||||
add_option(move(option));
|
add_option(move(option));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void ArgsParser::add_option(unsigned& value, const char* help_string, const char* long_name, char short_name, const char* value_name)
|
||||||
|
{
|
||||||
|
Option option {
|
||||||
|
true,
|
||||||
|
help_string,
|
||||||
|
long_name,
|
||||||
|
short_name,
|
||||||
|
value_name,
|
||||||
|
[&value](const char* s) {
|
||||||
|
auto opt = StringView(s).to_uint();
|
||||||
|
value = opt.value_or(0);
|
||||||
|
return opt.has_value();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
add_option(move(option));
|
||||||
|
}
|
||||||
|
|
||||||
void ArgsParser::add_option(double& value, const char* help_string, const char* long_name, char short_name, const char* value_name)
|
void ArgsParser::add_option(double& value, const char* help_string, const char* long_name, char short_name, const char* value_name)
|
||||||
{
|
{
|
||||||
Option option {
|
Option option {
|
||||||
|
@ -405,6 +422,22 @@ void ArgsParser::add_positional_argument(int& value, const char* help_string, co
|
||||||
add_positional_argument(move(arg));
|
add_positional_argument(move(arg));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void ArgsParser::add_positional_argument(unsigned& 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) {
|
||||||
|
auto opt = StringView(s).to_uint();
|
||||||
|
value = opt.value_or(0);
|
||||||
|
return opt.has_value();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
add_positional_argument(move(arg));
|
||||||
|
}
|
||||||
|
|
||||||
void ArgsParser::add_positional_argument(double& value, const char* help_string, const char* name, Required required)
|
void ArgsParser::add_positional_argument(double& value, const char* help_string, const char* name, Required required)
|
||||||
{
|
{
|
||||||
Arg arg {
|
Arg arg {
|
||||||
|
|
|
@ -65,6 +65,7 @@ public:
|
||||||
void add_option(String& value, const char* help_string, const char* long_name, char short_name, const char* value_name);
|
void add_option(String& value, const char* help_string, const char* long_name, char short_name, const char* value_name);
|
||||||
void add_option(StringView& value, char const* help_string, char const* long_name, char short_name, char const* value_name);
|
void add_option(StringView& value, char const* help_string, char const* long_name, char short_name, char const* value_name);
|
||||||
void add_option(int& value, const char* help_string, const char* long_name, char short_name, const char* value_name);
|
void add_option(int& value, const char* help_string, const char* long_name, char short_name, const char* value_name);
|
||||||
|
void add_option(unsigned& value, const char* help_string, const char* long_name, char short_name, const char* value_name);
|
||||||
void add_option(double& value, const char* help_string, const char* long_name, char short_name, const char* value_name);
|
void add_option(double& value, const char* help_string, const char* long_name, char short_name, const char* value_name);
|
||||||
|
|
||||||
void add_positional_argument(Arg&&);
|
void add_positional_argument(Arg&&);
|
||||||
|
@ -72,6 +73,7 @@ public:
|
||||||
void add_positional_argument(String& value, const char* help_string, const char* name, Required required = Required::Yes);
|
void add_positional_argument(String& value, const char* help_string, const char* 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, const char* help_string, const char* name, Required required = Required::Yes);
|
void add_positional_argument(int& value, const char* help_string, const char* name, Required required = Required::Yes);
|
||||||
|
void add_positional_argument(unsigned& value, const char* help_string, const char* name, Required required = Required::Yes);
|
||||||
void add_positional_argument(double& value, const char* help_string, const char* name, Required required = Required::Yes);
|
void add_positional_argument(double& value, const char* help_string, const char* name, Required required = Required::Yes);
|
||||||
void add_positional_argument(Vector<const char*>& value, const char* help_string, const char* name, Required required = Required::Yes);
|
void add_positional_argument(Vector<const char*>& value, const char* help_string, const char* name, Required required = Required::Yes);
|
||||||
void add_positional_argument(Vector<String>& value, const char* help_string, const char* name, Required required = Required::Yes);
|
void add_positional_argument(Vector<String>& value, const char* help_string, const char* name, Required required = Required::Yes);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue