From db886fe18bad881c1e1064780937c75e92e52b5f Mon Sep 17 00:00:00 2001 From: Ali Mohammad Pur Date: Tue, 21 Feb 2023 15:14:41 +0330 Subject: 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 to ArgsParser::parse(), applications using LibMain are unaffected, but anything not using that or taking its own argc/argv has to construct a Vector for this method. --- Userland/Libraries/LibCore/ArgsParser.cpp | 144 +++++++++++++++--------------- 1 file changed, 70 insertions(+), 74 deletions(-) (limited to 'Userland/Libraries/LibCore/ArgsParser.cpp') diff --git a/Userland/Libraries/LibCore/ArgsParser.cpp b/Userland/Libraries/LibCore/ArgsParser.cpp index a3448921dd..e0a390a86a 100644 --- a/Userland/Libraries/LibCore/ArgsParser.cpp +++ b/Userland/Libraries/LibCore/ArgsParser.cpp @@ -7,24 +7,15 @@ #include #include +#include #include #include #include -#include #include #include #include #include -static Optional convert_to_double(char const* s) -{ - char* p; - double v = strtod(s, &p); - if (isnan(v) || p == s) - return {}; - return v; -} - namespace Core { ArgsParser::ArgsParser() @@ -34,16 +25,18 @@ ArgsParser::ArgsParser() add_option(m_perform_autocomplete, "Perform autocompletion", "complete", 0, OptionHideMode::CommandLineAndMarkdown); } -bool ArgsParser::parse(int argc, char* const* argv, FailureBehavior failure_behavior) +bool ArgsParser::parse(Span arguments, FailureBehavior failure_behavior) { - auto fail = [this, argv, failure_behavior] { + auto fail = [this, name = arguments[0], failure_behavior] { if (failure_behavior == FailureBehavior::PrintUsage || failure_behavior == FailureBehavior::PrintUsageAndExit) - print_usage(stderr, argv[0]); + print_usage(stderr, name); if (failure_behavior == FailureBehavior::Exit || failure_behavior == FailureBehavior::PrintUsageAndExit) exit(1); }; - Vector