From fa6bdc9f297106f0ee85071dd8295225a8551a69 Mon Sep 17 00:00:00 2001 From: mxve <68632137+mxve@users.noreply.github.com> Date: Thu, 14 Sep 2023 09:37:35 +0200 Subject: [PATCH] prepend args with --, -, add short args --- README.md | 17 ++++++++++++----- src/main.rs | 52 +++++++++++++++++++++++++++++++++------------------- 2 files changed, 45 insertions(+), 24 deletions(-) diff --git a/README.md b/README.md index cd1d189..f2d145b 100644 --- a/README.md +++ b/README.md @@ -16,11 +16,18 @@ #### Command line arguments -- Passing ```iw4-sp```, ```iw4x```, ```iw5-mod```, ```iw6-mod``` or ```s1-mod``` as the first argument will skip automatic game detection -- Passing ```update``` will stop the launcher from launching the game -- ```skip-launcher-update``` skips launcher self-update -- ```bonus``` download bonus content -- ```force``` forces file hash recheck +- ```iw4-sp```, ```iw4x```, ```iw5-mod```, ```iw6-mod```, ```s1-mod``` + - Skip automatic detection and launch the specified game +- ```--update```, ```-u``` + - Only update the game, don't launch it +- ```--skip-launcher-update``` + - Don't update the launcher +- ```--bonus``` + - Download bonus content +- ```--force```, ```-f``` + - Force file hash recheck + +Example: ```alterware-launcher.exe iw4x --bonus -u``` Some arguments can be set in alterware-launcher.json, args generally override the values of the config. diff --git a/src/main.rs b/src/main.rs index c240143..045a125 100644 --- a/src/main.rs +++ b/src/main.rs @@ -281,6 +281,26 @@ fn setup_env() { }); } +fn arg_value(args: &[String], arg: &str) -> Option { + let val = args.iter() + .position(|r| r == arg) + .map(|e| args[e + 1].clone()); + if let Some(ref val) = val { + if val.starts_with('-') { + return None; + } + } + val +} + +fn arg_bool(args: &[String], arg: &str) -> bool { + args.iter().any(|r| r == arg) +} + +fn arg_remove(args: &mut Vec, arg: &str) { + args.iter().position(|r| r == arg).map(|e| args.remove(e)); +} + fn main() { #[cfg(windows)] setup_env(); @@ -288,33 +308,27 @@ fn main() { let mut args: Vec = std::env::args().collect(); let mut cfg = config::load(PathBuf::from("alterware-launcher.json")); - if args.contains(&String::from("update")) { - cfg.update_only = true; - args.iter() - .position(|r| r == "update") - .map(|e| args.remove(e)); - } - - if !args.contains(&String::from("skip-launcher-update")) && !cfg.skip_self_update { + if !arg_bool(&args, "--skip-launcher-update") && !cfg.skip_self_update { self_update::run(cfg.update_only); } else { - args.iter() - .position(|r| r == "skip-launcher-update") - .map(|e| args.remove(e)); + arg_remove(&mut args, "--skip-launcher-update"); } - if args.contains(&String::from("bonus")) { + if arg_bool(&args, "--update") || arg_bool(&args, "-u") { + cfg.update_only = true; + arg_remove(&mut args, "--update"); + arg_remove(&mut args, "-u"); + } + + if arg_bool(&args, "--bonus") { cfg.download_bonus_content = true; - args.iter() - .position(|r| r == "bonus") - .map(|e| args.remove(e)); + arg_remove(&mut args, "--bonus"); } - if args.contains(&String::from("force")) { + if arg_bool(&args, "--force") || arg_bool(&args, "-f") { cfg.force_update = true; - args.iter() - .position(|r| r == "force") - .map(|e| args.remove(e)); + arg_remove(&mut args, "--force"); + arg_remove(&mut args, "-f"); } let games_json = http::get_body_string(format!("{}/games.json", MASTER).as_str());