Skip to content

Commit

Permalink
LibCore: Refactor template definitions to header in ArgsParser
Browse files Browse the repository at this point in the history
This allows for automatic template instatiation and remove the need to
explicitly instantiate the template functions for different types.
  • Loading branch information
Ollrogge committed Jan 15, 2024
1 parent e0fd5be commit bdf8acd
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 101 deletions.
96 changes: 0 additions & 96 deletions Userland/Libraries/LibCore/ArgsParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -490,33 +490,6 @@ void ArgsParser::add_option(StringView& value, char const* help_string, char con
add_option(move(option));
}

template<Integral I>
void ArgsParser::add_option(I& 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 view) -> ErrorOr<bool> {
Optional<I> opt = view.to_number<I>();
value = opt.value_or(0);
return opt.has_value();
},
hide_mode,
};
add_option(move(option));
}

template void ArgsParser::add_option(i16&, char const*, char const*, char, char const*, OptionHideMode);
template void ArgsParser::add_option(i32&, char const*, char const*, char, char const*, OptionHideMode);
template void ArgsParser::add_option(i64&, char const*, char const*, char, char const*, OptionHideMode);
template void ArgsParser::add_option(u8&, char const*, char const*, char, char const*, OptionHideMode);
template void ArgsParser::add_option(u16&, char const*, char const*, char, char const*, OptionHideMode);
template void ArgsParser::add_option(u32&, char const*, char const*, char, char const*, OptionHideMode);
template void ArgsParser::add_option(u64&, char const*, char const*, char, char const*, OptionHideMode);

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 {
Expand Down Expand Up @@ -552,49 +525,6 @@ void ArgsParser::add_option(Optional<double>& value, char const* help_string, ch
add_option(move(option));
}

void ArgsParser::add_option(Optional<size_t>& 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<bool> {
value = s.to_number<size_t>();
return value.has_value();
},
hide_mode,
};
add_option(move(option));
}

void ArgsParser::add_option(Vector<size_t>& values, char const* help_string, char const* long_name, char short_name, char const* value_name, char separator, OptionHideMode hide_mode)
{
Option option {
OptionArgumentMode::Required,
help_string,
long_name,
short_name,
value_name,
[&values, separator](StringView s) -> ErrorOr<bool> {
bool parsed_all_values = true;

s.for_each_split_view(separator, SplitBehavior::Nothing, [&](auto value) {
if (auto maybe_value = AK::StringUtils::convert_to_uint<size_t>(value); maybe_value.has_value())
values.append(*maybe_value);
else
parsed_all_values = false;
});

return parsed_all_values;
},
hide_mode
};

add_option(move(option));
}

void ArgsParser::add_option(Vector<ByteString>& values, char const* help_string, char const* long_name, char short_name, char const* value_name, OptionHideMode hide_mode)
{
Option option {
Expand Down Expand Up @@ -663,32 +593,6 @@ void ArgsParser::add_positional_argument(String& value, char const* help_string,
add_positional_argument(move(arg));
}

template<Integral I>
void ArgsParser::add_positional_argument(I& value, char const* help_string, char const* name, Required required)
{
Arg arg {
help_string,
name,
required == Required::Yes ? 1 : 0,
1,
[&value](StringView view) -> ErrorOr<bool> {
Optional<I> opt = view.to_number<I>();
value = opt.value_or(0);
return opt.has_value();
},
};
add_positional_argument(move(arg));
}

template void ArgsParser::add_positional_argument(int&, char const*, char const*, Required);
template void ArgsParser::add_positional_argument(long&, char const*, char const*, Required);
template void ArgsParser::add_positional_argument(long long&, char const*, char const*, Required);
template void ArgsParser::add_positional_argument(short&, char const*, char const*, Required);
template void ArgsParser::add_positional_argument(unsigned&, char const*, char const*, Required);
template void ArgsParser::add_positional_argument(unsigned long&, char const*, char const*, Required);
template void ArgsParser::add_positional_argument(unsigned long long&, char const*, char const*, Required);
template void ArgsParser::add_positional_argument(unsigned short&, char const*, char const*, Required);

void ArgsParser::add_positional_argument(double& value, char const* help_string, char const* name, Required required)
{
Arg arg {
Expand Down
86 changes: 81 additions & 5 deletions Userland/Libraries/LibCore/ArgsParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,15 +101,77 @@ class ArgsParser {
},
.hide_mode = hide_mode });
}

template<Integral I>
void add_option(I& value, char const* help_string, char const* long_name, char short_name, char const* value_name, OptionHideMode hide_mode = OptionHideMode::None)
{
Option option {
OptionArgumentMode::Required,
help_string,
long_name,
short_name,
value_name,
[&value](StringView view) -> ErrorOr<bool> {
Optional<I> opt = view.to_number<I>();
value = opt.value_or(0);
return opt.has_value();
},
hide_mode,
};
add_option(move(option));
}

template<Integral I>
void add_option(Optional<I>& value, char const* help_string, char const* long_name, char short_name, char const* value_name, OptionHideMode hide_mode = OptionHideMode::None)
{

Option option {
OptionArgumentMode::Required,
help_string,
long_name,
short_name,
value_name,
[&value](StringView view) -> ErrorOr<bool> {
value = view.to_number<I>();
return value.has_value();
},
hide_mode,
};
add_option(move(option));
}

template<Integral I>
void add_option(Vector<I>& values, char const* help_string, char const* long_name, char short_name, char const* value_name, char separator = ',', OptionHideMode hide_mode = OptionHideMode::None)
{

Option option {
OptionArgumentMode::Required,
help_string,
long_name,
short_name,
value_name,
[&values, separator](StringView s) -> ErrorOr<bool> {
bool parsed_all_values = true;

s.for_each_split_view(separator, SplitBehavior::Nothing, [&](auto value) {
if (auto maybe_value = value.template to_number<I>(); maybe_value.has_value())
values.append(*maybe_value);
else
parsed_all_values = false;
});

return parsed_all_values;
},
hide_mode
};

add_option(move(option));
}
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);
template<Integral I>
void add_option(I& 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<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<size_t>& value, char const* help_string, char const* long_name, char short_name, char const* value_name, OptionHideMode hide_mode = OptionHideMode::None);
void add_option(Vector<size_t>& values, char const* help_string, char const* long_name, char short_name, char const* value_name, char separator = ',', OptionHideMode hide_mode = OptionHideMode::None);
// Note: This option is being used when we expect the user to use the same option
// multiple times (e.g. "program --option=example --option=anotherexample ...").
void add_option(Vector<ByteString>& values, char const* help_string, char const* long_name, char short_name, char const* value_name, OptionHideMode hide_mode = OptionHideMode::None);
Expand All @@ -119,7 +181,21 @@ class ArgsParser {
void add_positional_argument(StringView& value, char const* help_string, char const* name, Required required = Required::Yes);
void add_positional_argument(String& value, char const* help_string, char const* name, Required required = Required::Yes);
template<Integral I>
void add_positional_argument(I& value, char const* help_string, char const* name, Required required = Required::Yes);
void add_positional_argument(I& value, char const* help_string, char const* name, Required required = Required::Yes)
{
Arg arg {
help_string,
name,
required == Required::Yes ? 1 : 0,
1,
[&value](StringView view) -> ErrorOr<bool> {
Optional<I> opt = view.to_number<I>();
value = opt.value_or(0);
return opt.has_value();
},
};
add_positional_argument(move(arg));
}
void add_positional_argument(double& value, char const* help_string, char const* name, Required required = Required::Yes);
void add_positional_argument(Vector<ByteString>& value, char const* help_string, char const* name, Required required = Required::Yes);
void add_positional_argument(Vector<StringView>& value, char const* help_string, char const* name, Required required = Required::Yes);
Expand Down

0 comments on commit bdf8acd

Please sign in to comment.