LibCore: Add StringView overloads for Core::ArgsParser

These allow you to get StringView wrappers around on-stack string
arguments, which seems pretty nice. :^)
This commit is contained in:
Andreas Kling 2021-06-01 09:01:31 +02:00
parent dd9b8ee7ef
commit 313e53dca0
Notes: sideshowbarker 2024-07-18 17:05:09 +09:00
2 changed files with 33 additions and 0 deletions

View file

@ -285,6 +285,22 @@ void ArgsParser::add_option(String& value, const char* help_string, const char*
add_option(move(option));
}
void ArgsParser::add_option(StringView& value, char const* help_string, char const* long_name, char short_name, char const* value_name)
{
Option option {
true,
help_string,
long_name,
short_name,
value_name,
[&value](const char* s) {
value = s;
return true;
}
};
add_option(move(option));
}
void ArgsParser::add_option(int& value, const char* help_string, const char* long_name, char short_name, const char* value_name)
{
Option option {
@ -354,6 +370,21 @@ void ArgsParser::add_positional_argument(String& value, const char* help_string,
add_positional_argument(move(arg));
}
void ArgsParser::add_positional_argument(StringView& value, char const* help_string, char const* name, Required required)
{
Arg arg {
help_string,
name,
required == Required::Yes ? 1 : 0,
1,
[&value](const char* s) {
value = s;
return true;
}
};
add_positional_argument(move(arg));
}
void ArgsParser::add_positional_argument(int& value, const char* help_string, const char* name, Required required)
{
Arg arg {

View file

@ -55,12 +55,14 @@ public:
void add_option(bool& value, const char* help_string, const char* long_name, char short_name);
void add_option(const char*& 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(int& 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(const char*& 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(int& 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);