1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 08:47:34 +00:00

LibCore: Make ArgParser::Arg::accept_value return ErrorOr<bool>

Much like the previous commit, this commit makes the
ArgParser::Arg::accept_value callback return an ErrorOr<bool> instead of
just a bool.

The aim of this is to make argument parsing more robust, especially
with the newer String api that returns an ErrorOr for many functions.
This commit is contained in:
Carwyn Nelson 2023-07-01 16:08:46 +01:00 committed by Ali Mohammad Pur
parent abbfb00a02
commit a2591bc5fa
2 changed files with 12 additions and 16 deletions

View file

@ -191,7 +191,7 @@ bool ArgsParser::parse(Span<StringView> arguments, FailureBehavior failure_behav
auto& arg = m_positional_args[i]; auto& arg = m_positional_args[i];
for (int j = 0; j < num_values_for_arg[i]; j++) { for (int j = 0; j < num_values_for_arg[i]; j++) {
StringView value = arguments[option_index++]; StringView value = arguments[option_index++];
if (!arg.accept_value(value)) { if (!MUST(arg.accept_value(value))) {
warnln("Invalid value for argument {}", arg.name); warnln("Invalid value for argument {}", arg.name);
fail(); fail();
return false; return false;
@ -615,7 +615,7 @@ void ArgsParser::add_positional_argument(DeprecatedString& value, char const* he
name, name,
required == Required::Yes ? 1 : 0, required == Required::Yes ? 1 : 0,
1, 1,
[&value](StringView s) { [&value](StringView s) -> ErrorOr<bool> {
value = s; value = s;
return true; return true;
} }
@ -630,7 +630,7 @@ void ArgsParser::add_positional_argument(StringView& value, char const* help_str
name, name,
required == Required::Yes ? 1 : 0, required == Required::Yes ? 1 : 0,
1, 1,
[&value](StringView s) { [&value](StringView s) -> ErrorOr<bool> {
value = s; value = s;
return true; return true;
} }
@ -645,12 +645,8 @@ void ArgsParser::add_positional_argument(String& value, char const* help_string,
name, name,
required == Required::Yes ? 1 : 0, required == Required::Yes ? 1 : 0,
1, 1,
[&value](StringView s) { [&value](StringView s) -> ErrorOr<bool> {
auto value_or_error = String::from_utf8(s); value = TRY_OR_ERROR_IF_NOT_OOM(String::from_utf8(s), s);
if (value_or_error.is_error())
return false;
value = value_or_error.release_value();
return true; return true;
} }
}; };
@ -665,7 +661,7 @@ void ArgsParser::add_positional_argument(I& value, char const* help_string, char
name, name,
required == Required::Yes ? 1 : 0, required == Required::Yes ? 1 : 0,
1, 1,
[&value](StringView view) { [&value](StringView view) -> ErrorOr<bool> {
Optional<I> opt; Optional<I> opt;
if constexpr (IsSigned<I>) if constexpr (IsSigned<I>)
opt = view.to_int<I>(); opt = view.to_int<I>();
@ -694,7 +690,7 @@ void ArgsParser::add_positional_argument(double& value, char const* help_string,
name, name,
required == Required::Yes ? 1 : 0, required == Required::Yes ? 1 : 0,
1, 1,
[&value](StringView s) { [&value](StringView s) -> ErrorOr<bool> {
auto opt = s.to_double(); auto opt = s.to_double();
value = opt.value_or(0.0); value = opt.value_or(0.0);
return opt.has_value(); return opt.has_value();
@ -710,8 +706,8 @@ void ArgsParser::add_positional_argument(Vector<DeprecatedString>& values, char
name, name,
required == Required::Yes ? 1 : 0, required == Required::Yes ? 1 : 0,
INT_MAX, INT_MAX,
[&values](StringView s) { [&values](StringView s) -> ErrorOr<bool> {
values.append(s); TRY_OR_ERROR_IF_NOT_OOM(values.try_append(s), s);
return true; return true;
} }
}; };
@ -725,8 +721,8 @@ void ArgsParser::add_positional_argument(Vector<StringView>& values, char const*
name, name,
required == Required::Yes ? 1 : 0, required == Required::Yes ? 1 : 0,
INT_MAX, INT_MAX,
[&values](StringView s) { [&values](StringView s) -> ErrorOr<bool> {
values.append(s); TRY_OR_ERROR_IF_NOT_OOM(values.try_append(s), s);
return true; return true;
} }
}; };

View file

@ -67,7 +67,7 @@ public:
char const* name { nullptr }; char const* name { nullptr };
int min_values { 0 }; int min_values { 0 };
int max_values { 1 }; int max_values { 1 };
Function<bool(StringView)> accept_value; Function<ErrorOr<bool>(StringView)> accept_value;
}; };
bool parse(Span<StringView> arguments, FailureBehavior failure_behavior = FailureBehavior::PrintUsageAndExit); bool parse(Span<StringView> arguments, FailureBehavior failure_behavior = FailureBehavior::PrintUsageAndExit);