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.
This commit is contained in:
Sam Atkins 2023-09-24 16:13:00 +01:00 committed by Jelle Raaijmakers
parent 4d2af7c3d6
commit f71d74ed65
Notes: sideshowbarker 2024-07-16 19:57:55 +09:00

View file

@ -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<Enum T>
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);