Userland: Use Core::ArgsParser's Vector<StringView> API everywhere

...and remove the Vector<String> variant since there are no remaining
users of this API.
This commit is contained in:
Andreas Kling 2021-11-26 22:32:37 +01:00
parent 395ba619d8
commit f1cc3d0fc4
Notes: sideshowbarker 2024-07-18 00:38:48 +09:00
17 changed files with 31 additions and 47 deletions

View file

@ -147,7 +147,7 @@ TEST_CASE(positional_string_argument)
TEST_CASE(positional_vector_string_argument)
{
// Zero or more positional arguments, zero given
Vector<String> values = {};
Vector<StringView> 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<String> positionals = {};
Vector<StringView> 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<String> positionals = {};
Vector<StringView> 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');

View file

@ -580,21 +580,6 @@ void ArgsParser::add_positional_argument(Vector<const char*>& values, const char
add_positional_argument(move(arg));
}
void ArgsParser::add_positional_argument(Vector<String>& 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<StringView>& values, char const* help_string, char const* name, Required required)
{
Arg arg {

View file

@ -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<const char*>& value, const char* help_string, const char* name, Required required = Required::Yes);
void add_positional_argument(Vector<String>& value, const char* help_string, const char* name, Required required = Required::Yes);
void add_positional_argument(Vector<StringView>& value, char const* help_string, char const* name, Required required = Required::Yes);
private:

View file

@ -28,9 +28,9 @@ struct WorkItem {
off_t size;
};
static int perform_copy(Vector<String> const& sources, String const& destination);
static int perform_move(Vector<String> const& sources, String const& destination);
static int perform_delete(Vector<String> const& sources);
static int perform_copy(Vector<StringView> const& sources, String const& destination);
static int perform_move(Vector<StringView> const& sources, String const& destination);
static int perform_delete(Vector<StringView> const& sources);
static int execute_work_items(Vector<WorkItem> 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<String> paths;
Vector<StringView> 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<String> const& sources, String const& destination)
int perform_copy(Vector<StringView> const& sources, String const& destination)
{
Vector<WorkItem> items;
@ -178,7 +178,7 @@ static bool collect_move_work_items(String const& source, String const& destinat
return true;
}
int perform_move(Vector<String> const& sources, String const& destination)
int perform_move(Vector<StringView> const& sources, String const& destination)
{
Vector<WorkItem> items;
@ -228,7 +228,7 @@ static bool collect_delete_work_items(String const& source, Vector<WorkItem>& it
return true;
}
int perform_delete(Vector<String> const& sources)
int perform_delete(Vector<StringView> const& sources)
{
Vector<WorkItem> items;

View file

@ -30,7 +30,7 @@ int main(int argc, char** argv)
auto audio_client = Audio::ClientConnection::construct();
String command = String::empty();
Vector<String> arguments;
Vector<StringView> 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;

View file

@ -20,7 +20,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
{
TRY(Core::System::pledge("stdio rpath", nullptr));
Vector<String> paths;
Vector<StringView> paths;
Core::ArgsParser args_parser;
args_parser.set_general_help("Concatenate files or pipes to stdout.");

View file

@ -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<String> paths;
Vector<StringView> paths;
Core::ArgsParser args_parser;
args_parser.add_positional_argument(paths, paths_help_string.characters(), "path", Core::ArgsParser::Required::No);

View file

@ -21,7 +21,7 @@ int main(int argc, char** argv)
bool preserve = false;
bool recursion_allowed = false;
bool verbose = false;
Vector<String> sources;
Vector<StringView> sources;
String destination;
Core::ArgsParser args_parser;

View file

@ -153,7 +153,7 @@ int main(int argc, char** argv)
String fields_list = "";
String delimiter = "\t";
Vector<String> files;
Vector<StringView> 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;

View file

@ -25,7 +25,7 @@ static bool decompress_file(Buffered<Core::InputFileStream>& input_stream, Buffe
int main(int argc, char** argv)
{
Vector<String> filenames;
Vector<StringView> filenames;
bool keep_input_files { false };
bool write_to_stdout { false };

View file

@ -12,7 +12,7 @@
int main(int argc, char** argv)
{
Vector<String> filenames;
Vector<StringView> 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;

View file

@ -1163,7 +1163,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
bool gc_on_every_allocation = false;
bool disable_syntax_highlight = false;
Vector<String> script_paths;
Vector<StringView> script_paths;
Core::ArgsParser args_parser;
args_parser.set_general_help("This is a JavaScript interpreter.");

View file

@ -98,7 +98,7 @@ int main(int argc, char** argv)
return 1;
}
Vector<String> paths;
Vector<StringView> 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");

View file

@ -16,7 +16,7 @@ int main(int argc, char** argv)
return 1;
}
Vector<String> paths;
Vector<StringView> 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;

View file

@ -54,7 +54,7 @@ static bool write_variable(StringView name, StringView value)
return true;
}
static int handle_variables(Vector<String> const& variables)
static int handle_variables(Vector<StringView> 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<String> variables;
Vector<StringView> variables;
Core::ArgsParser args_parser;
args_parser.set_general_help("Show or modify system-internal values. This requires root, and can crash your system.");

View file

@ -16,7 +16,7 @@ int main(int argc, char** argv)
return 1;
}
Vector<String> paths;
Vector<StringView> 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;

View file

@ -16,7 +16,7 @@
int main(int argc, char** argv)
{
const char* zip_path;
Vector<String> source_paths;
Vector<StringView> source_paths;
bool recurse = false;
bool force = false;