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

Userland+AK: Stop using getopt() for ArgsParser

This commit moves the implementation of getopt into AK, and converts its
API to understand and use StringView instead of char*.
Everything else is caught in the crossfire of making
Option::accept_value() take a StringView instead of a char const*.

With this, we must now pass a Span<StringView> to ArgsParser::parse(),
applications using LibMain are unaffected, but anything not using that
or taking its own argc/argv has to construct a Vector<StringView> for
this method.
This commit is contained in:
Ali Mohammad Pur 2023-02-21 15:14:41 +03:30 committed by Ali Mohammad Pur
parent b2b851b361
commit db886fe18b
43 changed files with 673 additions and 584 deletions

View file

@ -25,8 +25,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
.long_name = "unveil",
.short_name = 'u',
.value_name = "path",
.accept_value = [&](auto* s) {
StringView path { s, strlen(s) };
.accept_value = [&](StringView path) {
if (path.is_empty())
return false;
auto maybe_error = Core::System::unveil(path, permissions);
@ -41,7 +40,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
.help_string = "Lock the veil",
.long_name = "lock",
.short_name = 'l',
.accept_value = [&](auto*) {
.accept_value = [&](StringView) {
auto maybe_error = Core::System::unveil(nullptr, nullptr);
if (maybe_error.is_error()) {
warnln("unveil(nullptr, nullptr): {}", maybe_error.error());
@ -54,8 +53,8 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
.name = "path",
.min_values = 0,
.max_values = INT_MAX,
.accept_value = [&](auto* s) {
auto maybe_error = Core::System::access({ s, strlen(s) }, X_OK);
.accept_value = [&](StringView s) {
auto maybe_error = Core::System::access(s, X_OK);
if (maybe_error.is_error())
warnln("'{}' - fail: {}", s, maybe_error.error());
else