From f71d74ed65f22dfd871140b528061c7defbd62f9 Mon Sep 17 00:00:00 2001 From: Sam Atkins Date: Sun, 24 Sep 2023 16:13:00 +0100 Subject: [PATCH] LibCore: Add ArgsParser::add_option for setting enum values from a flag Previously, argument-less options could only set a boolean to true. This lets them also set an enum variable to a specific value, as is currently done by the `ls` utility. --- Userland/Libraries/LibCore/ArgsParser.h | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/Userland/Libraries/LibCore/ArgsParser.h b/Userland/Libraries/LibCore/ArgsParser.h index d73841b539..af3e17971f 100644 --- a/Userland/Libraries/LibCore/ArgsParser.h +++ b/Userland/Libraries/LibCore/ArgsParser.h @@ -87,6 +87,20 @@ public: void add_option(Option&&); void add_ignored(char const* long_name, char short_name, OptionHideMode hide_mode = OptionHideMode::None); void add_option(bool& value, char const* help_string, char const* long_name, char short_name, OptionHideMode hide_mode = OptionHideMode::None); + /// If the option is present, set the enum to have the given `new_value`. + template + void add_option(T& value, T new_value, char const* help_string, char const* long_name, char short_name, OptionHideMode hide_mode = OptionHideMode::None) + { + add_option({ .argument_mode = Core::ArgsParser::OptionArgumentMode::None, + .help_string = help_string, + .long_name = long_name, + .short_name = short_name, + .accept_value = [&](StringView) { + value = new_value; + return true; + }, + .hide_mode = hide_mode }); + } void add_option(DeprecatedString& 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);