From f1cc3d0fc48af5247278101f863d6416f763733e Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Fri, 26 Nov 2021 22:32:37 +0100 Subject: [PATCH] Userland: Use Core::ArgsParser's Vector API everywhere ...and remove the Vector variant since there are no remaining users of this API. --- Tests/LibCore/TestLibCoreArgsParser.cpp | 6 +++--- Userland/Libraries/LibCore/ArgsParser.cpp | 15 --------------- Userland/Libraries/LibCore/ArgsParser.h | 1 - Userland/Services/FileOperation/main.cpp | 14 +++++++------- Userland/Utilities/asctl.cpp | 4 ++-- Userland/Utilities/cat.cpp | 2 +- Userland/Utilities/checksum.cpp | 2 +- Userland/Utilities/cp.cpp | 2 +- Userland/Utilities/cut.cpp | 4 ++-- Userland/Utilities/gunzip.cpp | 2 +- Userland/Utilities/gzip.cpp | 6 +++--- Userland/Utilities/js.cpp | 2 +- Userland/Utilities/ls.cpp | 4 ++-- Userland/Utilities/rev.cpp | 4 ++-- Userland/Utilities/sysctl.cpp | 4 ++-- Userland/Utilities/tac.cpp | 4 ++-- Userland/Utilities/zip.cpp | 2 +- 17 files changed, 31 insertions(+), 47 deletions(-) diff --git a/Tests/LibCore/TestLibCoreArgsParser.cpp b/Tests/LibCore/TestLibCoreArgsParser.cpp index 753dca89583..b19f0fab817 100644 --- a/Tests/LibCore/TestLibCoreArgsParser.cpp +++ b/Tests/LibCore/TestLibCoreArgsParser.cpp @@ -147,7 +147,7 @@ TEST_CASE(positional_string_argument) TEST_CASE(positional_vector_string_argument) { // Zero or more positional arguments, zero given - Vector values = {}; + Vector values; auto parser_result = run_parser({ "app" }, [&](auto& parser) { parser.add_positional_argument(values, "values", "values", Core::ArgsParser::Required::No); }); @@ -213,7 +213,7 @@ TEST_CASE(combination_of_bool_options_with_positional_vector_string) // Expected: all arguments fill as given bool bool_opt1 = false; bool bool_opt2 = false; - Vector positionals = {}; + Vector positionals; auto parser_result = run_parser({ "app", "-b", "-c", "one", "two" }, [&](auto& parser) { parser.add_option(bool_opt1, "bool_opt1", nullptr, 'b'); parser.add_option(bool_opt2, "bool_opt2", nullptr, 'c'); @@ -318,7 +318,7 @@ TEST_CASE(stop_on_first_non_option) // Expected: bool options are set and one positional argument is filled bool bool_opt1 = false; bool bool_opt2 = false; - Vector positionals = {}; + Vector positionals; auto parser_result = run_parser({ "app", "-b", "-c", "one" }, [&](auto& parser) { parser.set_stop_on_first_non_option(false); parser.add_option(bool_opt1, "bool_opt1", nullptr, 'b'); diff --git a/Userland/Libraries/LibCore/ArgsParser.cpp b/Userland/Libraries/LibCore/ArgsParser.cpp index db1ec8d5e7f..f37e0a7d9bb 100644 --- a/Userland/Libraries/LibCore/ArgsParser.cpp +++ b/Userland/Libraries/LibCore/ArgsParser.cpp @@ -580,21 +580,6 @@ void ArgsParser::add_positional_argument(Vector& values, const char add_positional_argument(move(arg)); } -void ArgsParser::add_positional_argument(Vector& values, const char* help_string, const char* name, Required required) -{ - Arg arg { - help_string, - name, - required == Required::Yes ? 1 : 0, - INT_MAX, - [&values](const char* s) { - values.append(s); - return true; - } - }; - add_positional_argument(move(arg)); -} - void ArgsParser::add_positional_argument(Vector& values, char const* help_string, char const* name, Required required) { Arg arg { diff --git a/Userland/Libraries/LibCore/ArgsParser.h b/Userland/Libraries/LibCore/ArgsParser.h index 9c0998968b2..ffeafeb46bb 100644 --- a/Userland/Libraries/LibCore/ArgsParser.h +++ b/Userland/Libraries/LibCore/ArgsParser.h @@ -86,7 +86,6 @@ public: void add_positional_argument(unsigned& value, const char* help_string, const char* name, Required required = Required::Yes); void add_positional_argument(double& value, const char* help_string, const char* name, Required required = Required::Yes); void add_positional_argument(Vector& value, const char* help_string, const char* name, Required required = Required::Yes); - void add_positional_argument(Vector& value, const char* help_string, const char* name, Required required = Required::Yes); void add_positional_argument(Vector& value, char const* help_string, char const* name, Required required = Required::Yes); private: diff --git a/Userland/Services/FileOperation/main.cpp b/Userland/Services/FileOperation/main.cpp index 5515a3569db..6e174e74718 100644 --- a/Userland/Services/FileOperation/main.cpp +++ b/Userland/Services/FileOperation/main.cpp @@ -28,9 +28,9 @@ struct WorkItem { off_t size; }; -static int perform_copy(Vector const& sources, String const& destination); -static int perform_move(Vector const& sources, String const& destination); -static int perform_delete(Vector const& sources); +static int perform_copy(Vector const& sources, String const& destination); +static int perform_move(Vector const& sources, String const& destination); +static int perform_delete(Vector const& sources); static int execute_work_items(Vector const& items); static void report_error(String message); static void report_warning(String message); @@ -40,7 +40,7 @@ static String deduplicate_destination_file_name(String const& destination); int main(int argc, char** argv) { String operation; - Vector paths; + Vector paths; Core::ArgsParser args_parser; args_parser.add_positional_argument(operation, "Operation: either 'Copy', 'Move' or 'Delete'", "operation", Core::ArgsParser::Required::Yes); @@ -117,7 +117,7 @@ static bool collect_copy_work_items(String const& source, String const& destinat return true; } -int perform_copy(Vector const& sources, String const& destination) +int perform_copy(Vector const& sources, String const& destination) { Vector items; @@ -178,7 +178,7 @@ static bool collect_move_work_items(String const& source, String const& destinat return true; } -int perform_move(Vector const& sources, String const& destination) +int perform_move(Vector const& sources, String const& destination) { Vector items; @@ -228,7 +228,7 @@ static bool collect_delete_work_items(String const& source, Vector& it return true; } -int perform_delete(Vector const& sources) +int perform_delete(Vector const& sources) { Vector items; diff --git a/Userland/Utilities/asctl.cpp b/Userland/Utilities/asctl.cpp index 8d064880d7d..28749d5f476 100644 --- a/Userland/Utilities/asctl.cpp +++ b/Userland/Utilities/asctl.cpp @@ -30,7 +30,7 @@ int main(int argc, char** argv) auto audio_client = Audio::ClientConnection::construct(); String command = String::empty(); - Vector arguments; + Vector arguments; bool human_mode = false; Core::ArgsParser args_parser; @@ -113,7 +113,7 @@ int main(int argc, char** argv) } values_to_set.set(AudioVariable::Volume, volume.value()); } else if (variable.is_one_of("m"sv, "mute"sv)) { - String& mute_text = arguments[++i]; + auto& mute_text = arguments[++i]; bool mute; if (mute_text.equals_ignoring_case("true") || mute_text == "1") { mute = true; diff --git a/Userland/Utilities/cat.cpp b/Userland/Utilities/cat.cpp index c749cbeff51..aa9dda6bb8d 100644 --- a/Userland/Utilities/cat.cpp +++ b/Userland/Utilities/cat.cpp @@ -20,7 +20,7 @@ ErrorOr serenity_main(Main::Arguments arguments) { TRY(Core::System::pledge("stdio rpath", nullptr)); - Vector paths; + Vector paths; Core::ArgsParser args_parser; args_parser.set_general_help("Concatenate files or pipes to stdout."); diff --git a/Userland/Utilities/checksum.cpp b/Userland/Utilities/checksum.cpp index 254df5bd5a3..03971b8b280 100644 --- a/Userland/Utilities/checksum.cpp +++ b/Userland/Utilities/checksum.cpp @@ -36,7 +36,7 @@ int main(int argc, char** argv) auto hash_name = program_name.substring_view(0, program_name.length() - 3).to_string().to_uppercase(); auto paths_help_string = String::formatted("File(s) to print {} checksum of", hash_name); - Vector paths; + Vector paths; Core::ArgsParser args_parser; args_parser.add_positional_argument(paths, paths_help_string.characters(), "path", Core::ArgsParser::Required::No); diff --git a/Userland/Utilities/cp.cpp b/Userland/Utilities/cp.cpp index b5b1cfa6680..d6d1fb4599e 100644 --- a/Userland/Utilities/cp.cpp +++ b/Userland/Utilities/cp.cpp @@ -21,7 +21,7 @@ int main(int argc, char** argv) bool preserve = false; bool recursion_allowed = false; bool verbose = false; - Vector sources; + Vector sources; String destination; Core::ArgsParser args_parser; diff --git a/Userland/Utilities/cut.cpp b/Userland/Utilities/cut.cpp index c50684d1036..c035c5b38b8 100644 --- a/Userland/Utilities/cut.cpp +++ b/Userland/Utilities/cut.cpp @@ -153,7 +153,7 @@ int main(int argc, char** argv) String fields_list = ""; String delimiter = "\t"; - Vector files; + Vector files; Core::ArgsParser args_parser; args_parser.add_positional_argument(files, "file(s) to cut", "file", Core::ArgsParser::Required::No); @@ -230,7 +230,7 @@ int main(int argc, char** argv) for (auto& file : files) { FILE* fp = stdin; if (!file.is_null()) { - fp = fopen(file.characters(), "r"); + fp = fopen(String(file).characters(), "r"); if (!fp) { warnln("cut: Could not open file '{}'", file); continue; diff --git a/Userland/Utilities/gunzip.cpp b/Userland/Utilities/gunzip.cpp index 589e0ddd3d6..b2743406b44 100644 --- a/Userland/Utilities/gunzip.cpp +++ b/Userland/Utilities/gunzip.cpp @@ -25,7 +25,7 @@ static bool decompress_file(Buffered& input_stream, Buffe int main(int argc, char** argv) { - Vector filenames; + Vector filenames; bool keep_input_files { false }; bool write_to_stdout { false }; diff --git a/Userland/Utilities/gzip.cpp b/Userland/Utilities/gzip.cpp index 71bf6485203..18cefb442a3 100644 --- a/Userland/Utilities/gzip.cpp +++ b/Userland/Utilities/gzip.cpp @@ -12,7 +12,7 @@ int main(int argc, char** argv) { - Vector filenames; + Vector filenames; bool keep_input_files { false }; bool write_to_stdout { false }; bool decompress { false }; @@ -34,7 +34,7 @@ int main(int argc, char** argv) warnln("unknown suffix for: {}, skipping", input_filename); continue; } - output_filename = input_filename.substring(0, input_filename.length() - ".gz"sv.length()); + output_filename = input_filename.substring_view(0, input_filename.length() - ".gz"sv.length()); } else { output_filename = String::formatted("{}.gz", input_filename); } @@ -78,7 +78,7 @@ int main(int argc, char** argv) } if (!keep_input_files) { - const auto retval = unlink(input_filename.characters()); + const auto retval = unlink(String(input_filename).characters()); if (retval != 0) { warnln("Failed removing input file"); return 1; diff --git a/Userland/Utilities/js.cpp b/Userland/Utilities/js.cpp index e433be05aca..22bb3675c6a 100644 --- a/Userland/Utilities/js.cpp +++ b/Userland/Utilities/js.cpp @@ -1163,7 +1163,7 @@ ErrorOr serenity_main(Main::Arguments arguments) bool gc_on_every_allocation = false; bool disable_syntax_highlight = false; - Vector script_paths; + Vector script_paths; Core::ArgsParser args_parser; args_parser.set_general_help("This is a JavaScript interpreter."); diff --git a/Userland/Utilities/ls.cpp b/Userland/Utilities/ls.cpp index 8704f5caf35..39614ed8895 100644 --- a/Userland/Utilities/ls.cpp +++ b/Userland/Utilities/ls.cpp @@ -98,7 +98,7 @@ int main(int argc, char** argv) return 1; } - Vector paths; + Vector paths; Core::ArgsParser args_parser; args_parser.set_general_help("List files in a directory."); @@ -148,7 +148,7 @@ int main(int argc, char** argv) FileMetadata metadata; metadata.name = path; - int rc = lstat(path.characters(), &metadata.stat); + int rc = lstat(String(path).characters(), &metadata.stat); if (rc < 0) perror("lstat"); diff --git a/Userland/Utilities/rev.cpp b/Userland/Utilities/rev.cpp index 5b427542035..5fb4498ad72 100644 --- a/Userland/Utilities/rev.cpp +++ b/Userland/Utilities/rev.cpp @@ -16,7 +16,7 @@ int main(int argc, char** argv) return 1; } - Vector paths; + Vector paths; Core::ArgsParser args_parser; args_parser.set_general_help("Concatente files to stdout with each line in reverse."); @@ -29,7 +29,7 @@ int main(int argc, char** argv) if (!paths.is_empty()) { for (auto const& path : paths) { - FILE* stream = fopen(path.characters(), "r"); + FILE* stream = fopen(String(path).characters(), "r"); if (!stream) { warnln("Failed to open {}: {}", path, strerror(errno)); continue; diff --git a/Userland/Utilities/sysctl.cpp b/Userland/Utilities/sysctl.cpp index 8d1a3573742..f339ddf2f82 100644 --- a/Userland/Utilities/sysctl.cpp +++ b/Userland/Utilities/sysctl.cpp @@ -54,7 +54,7 @@ static bool write_variable(StringView name, StringView value) return true; } -static int handle_variables(Vector const& variables) +static int handle_variables(Vector const& variables) { bool success = false; for (auto const& variable : variables) { @@ -95,7 +95,7 @@ static int handle_show_all() int main(int argc, char** argv) { bool show_all = false; - Vector variables; + Vector variables; Core::ArgsParser args_parser; args_parser.set_general_help("Show or modify system-internal values. This requires root, and can crash your system."); diff --git a/Userland/Utilities/tac.cpp b/Userland/Utilities/tac.cpp index 8568677421d..e6681158019 100644 --- a/Userland/Utilities/tac.cpp +++ b/Userland/Utilities/tac.cpp @@ -16,7 +16,7 @@ int main(int argc, char** argv) return 1; } - Vector paths; + Vector paths; Core::ArgsParser args_parser; args_parser.set_general_help("Concatenate files or pipes to stdout, last line first."); @@ -33,7 +33,7 @@ int main(int argc, char** argv) if (path == "-"sv) { stream = stdin; } else { - stream = fopen(path.characters(), "r"); + stream = fopen(String(path).characters(), "r"); if (!stream) { warnln("Failed to open {}: {}", path, strerror(errno)); continue; diff --git a/Userland/Utilities/zip.cpp b/Userland/Utilities/zip.cpp index 8ecf1b65e4c..0f68c8f3044 100644 --- a/Userland/Utilities/zip.cpp +++ b/Userland/Utilities/zip.cpp @@ -16,7 +16,7 @@ int main(int argc, char** argv) { const char* zip_path; - Vector source_paths; + Vector source_paths; bool recurse = false; bool force = false;