From 7ca66fea1d0d984c8688ef30a31b3e117d52b7b1 Mon Sep 17 00:00:00 2001 From: Daniel Conley Date: Fri, 1 Sep 2023 16:11:10 -0400 Subject: [PATCH] Move argument parsing and use references where possible --- src/args/launch.rs | 2 +- src/main.rs | 35 +++++++---------------------------- src/utils/argparse.rs | 29 +++++++++++++++++++++++++++++ 3 files changed, 37 insertions(+), 29 deletions(-) diff --git a/src/args/launch.rs b/src/args/launch.rs index 925de0a..7e2f489 100644 --- a/src/args/launch.rs +++ b/src/args/launch.rs @@ -6,7 +6,7 @@ use discord_rich_presence::{activity, DiscordIpc, DiscordIpcClient}; const _HELP_TEXT: &str = "\nUsage: TODO"; -pub fn main(raw_args: Vec<(String, String)>) { +pub fn main(raw_args: &[(String, String)]) { let dir_location = setup::get_applejuice_dir(); let binary_type = argparse::get_param_value_new(&raw_args, "binary").unwrap(); let channel = argparse::get_param_value_new(&raw_args, "channel").unwrap(); diff --git a/src/main.rs b/src/main.rs index 47c5d0e..e5e5edb 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,8 @@ use std::{env, process::exit}; mod utils; // Import utilities that are not necessarily commands -mod args; // Import modules which act as a handler for certain command parameters +mod args; use utils::argparse::parse_arguments; + +// Import modules which act as a handler for certain command parameters use crate::utils::{terminal::*, *}; #[cfg(not(target_os = "linux"))] @@ -24,40 +26,17 @@ fn main() { let command = &args[1]; let command_clean: &str = &command.replace("--", ""); - let mut arguments: Vec> = vec![]; // Collected args and their values - let mut arg_command = String::new(); // Current command parameter being collected - let mut arg_command_value = String::new(); // Current command parameter value being collected - - for (index, argument) in args.iter().enumerate() { - if index == 0 { continue; } - - if argument.contains("--") { - if arg_command.is_empty() && arg_command_value.is_empty() { - arg_command = argument.replace("--", ""); - } - } else if arg_command_value.is_empty() { - arg_command_value = argument.to_string(); // Construct first argument - } else { - arg_command_value = format!("{} {}", arg_command_value, argument); // Construct argument value and concatenate :3 - } - - // If this is the last argument or next argument is a command, push to vec - if index == args.len() - 1 || args[index + 1].contains("--") { - arguments.push(vec![(arg_command, arg_command_value)]); - arg_command = String::new(); - arg_command_value = String::new(); - } - } + let arguments = parse_arguments(&args); match command_clean { "help" => args::help::main(), "init" => args::initialise::main(), - "install" => args::install::main(arguments), - "purge" => args::purge::main(arguments), + "install" => args::install::main(arguments.into_iter().map(|item| vec![item]).collect()), + "purge" => args::purge::main(arguments.into_iter().map(|item| vec![item]).collect()), "opendata" => args::opendata::main(), "play" => args::play::main(), // TODO: fix this in above code - "launch" => args::launch::main(arguments.into_iter().flatten().collect()), + "launch" => args::launch::main(&arguments), _ => { let _indentation = error!("Unknown command parameter: '{:?}'", command); help!("Run '{} --help' for more information.", args[0]); diff --git a/src/utils/argparse.rs b/src/utils/argparse.rs index 481b47d..799133e 100644 --- a/src/utils/argparse.rs +++ b/src/utils/argparse.rs @@ -20,3 +20,32 @@ pub fn get_param_value_new<'v>(command_vector: &'v [(String, String)], argument_ None } + +pub fn parse_arguments(args: &[String]) -> Vec<(String, String)> { + let mut arguments = vec![]; // Collected args and their values + let mut arg_command = String::new(); // Current command parameter being collected + let mut arg_command_value = String::new(); // Current command parameter value being collected + + for (index, argument) in args.iter().enumerate() { + if index == 0 { continue; } + + if argument.contains("--") { + if arg_command.is_empty() && arg_command_value.is_empty() { + arg_command = argument.replace("--", ""); + } + } else if arg_command_value.is_empty() { + arg_command_value = argument.to_string(); // Construct first argument + } else { + arg_command_value = format!("{} {}", arg_command_value, argument); // Construct argument value and concatenate :3 + } + + // If this is the last argument or next argument is a command, push to vec + if index == args.len() - 1 || args[index + 1].contains("--") { + arguments.push((arg_command, arg_command_value)); + arg_command = String::new(); + arg_command_value = String::new(); + } + } + + arguments +}