From abf35f5bd60a5b12be9b6e0b37bb252d4d9c5227 Mon Sep 17 00:00:00 2001 From: Shannon Booth Date: Sat, 2 Mar 2024 22:21:17 +1300 Subject: [PATCH] LibCore: Support Optional as an argument to ArgsParser While StringView does have a null state, we have been moving away from this in our other String classes. To represent a StringView not being given at all on the commandline, use an Optional. --- Userland/Libraries/LibCore/ArgsParser.cpp | 17 +++++++++++++++++ Userland/Libraries/LibCore/ArgsParser.h | 1 + 2 files changed, 18 insertions(+) diff --git a/Userland/Libraries/LibCore/ArgsParser.cpp b/Userland/Libraries/LibCore/ArgsParser.cpp index f8ad3940db3..b318178c58d 100644 --- a/Userland/Libraries/LibCore/ArgsParser.cpp +++ b/Userland/Libraries/LibCore/ArgsParser.cpp @@ -490,6 +490,23 @@ void ArgsParser::add_option(StringView& value, char const* help_string, char con add_option(move(option)); } +void ArgsParser::add_option(Optional& value, char const* help_string, char const* long_name, char short_name, char const* value_name, OptionHideMode hide_mode) +{ + Option option { + OptionArgumentMode::Required, + help_string, + long_name, + short_name, + value_name, + [&value](StringView s) -> ErrorOr { + value = s; + return true; + }, + hide_mode, + }; + add_option(move(option)); +} + void ArgsParser::add_option(double& value, char const* help_string, char const* long_name, char short_name, char const* value_name, OptionHideMode hide_mode) { Option option { diff --git a/Userland/Libraries/LibCore/ArgsParser.h b/Userland/Libraries/LibCore/ArgsParser.h index e84998a30d4..2a69c6f8b89 100644 --- a/Userland/Libraries/LibCore/ArgsParser.h +++ b/Userland/Libraries/LibCore/ArgsParser.h @@ -170,6 +170,7 @@ public: void add_option(ByteString& value, char const* help_string, char const* long_name, char short_name, char const* value_name, OptionHideMode hide_mode = OptionHideMode::None); void add_option(String& value, char const* help_string, char const* long_name, char short_name, char const* value_name, OptionHideMode hide_mode = OptionHideMode::None); void add_option(StringView& value, char const* help_string, char const* long_name, char short_name, char const* value_name, OptionHideMode hide_mode = OptionHideMode::None); + void add_option(Optional& value, char const* help_string, char const* long_name, char short_name, char const* value_name, OptionHideMode hide_mode = OptionHideMode::None); void add_option(double& value, char const* help_string, char const* long_name, char short_name, char const* value_name, OptionHideMode hide_mode = OptionHideMode::None); void add_option(Optional& value, char const* help_string, char const* long_name, char short_name, char const* value_name, OptionHideMode hide_mode = OptionHideMode::None); // Note: This option is being used when we expect the user to use the same option