diff --git a/Cargo.toml b/Cargo.toml index 05e3f48..461547e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "shticker_book_unwritten" -version = "1.0.5" +version = "1.0.6" authors = ["Dr. Jonathan Helianthicus Doe, IV "] edition = "2021" description = "Minimal CLI launcher for the Toontown Rewritten MMORPG" @@ -19,12 +19,12 @@ serde_json = "1.0.81" sha-1 = "0.10.0" [dependencies.clap] -version = "3.1.18" +version = "3.2.1" default-features = false features = ["std", "cargo", "suggestions"] [dependencies.reqwest] -version = "0.11.10" +version = "0.11.11" features = ["blocking", "default-tls"] [profile.release] diff --git a/src/config.rs b/src/config.rs index ef3a43d..3d848c9 100644 --- a/src/config.rs +++ b/src/config.rs @@ -42,26 +42,23 @@ impl Config { pub fn get_config( no_config: bool, - config_path: Option<&str>, - install_path: Option<&str>, - cache_path: Option<&str>, + config_path: Option, + install_path: Option, + cache_path: Option, quiet: bool, ) -> Result<(Config, PathBuf), Error> { let inject_arg_values = |c| { - let c = if let Some(ip) = install_path { + let c = if let Some(ip) = install_path.clone() { Config { - install_dir: PathBuf::from(ip), + install_dir: ip, ..c } } else { c }; - if let Some(cp) = cache_path { - Config { - cache_dir: PathBuf::from(cp), - ..c - } + if let Some(cp) = cache_path.clone() { + Config { cache_dir: cp, ..c } } else { c } @@ -69,7 +66,7 @@ pub fn get_config( if !no_config { let config_path = if let Some(s) = config_path { - PathBuf::from(s) + s } else { #[cfg(target_os = "linux")] { @@ -203,12 +200,12 @@ pub fn get_config( Ok(( Config { - install_dir: PathBuf::from(install_path.ok_or_else(|| { + install_dir: install_path.ok_or_else(|| { Error::MissingCommandLineArg("--install-dir") - })?), - cache_dir: PathBuf::from(cache_path.ok_or_else(|| { + })?, + cache_dir: cache_path.ok_or_else(|| { Error::MissingCommandLineArg("--cache-dir") - })?), + })?, manifest_uri: DEFAULT_MANIFEST_URI.to_owned(), cdn_uri: DEFAULT_CDN_URI.to_owned(), store_passwords: false, diff --git a/src/error.rs b/src/error.rs index 0cb1446..5a480e4 100644 --- a/src/error.rs +++ b/src/error.rs @@ -43,7 +43,6 @@ pub enum Error { ThreadJoinError(io::Error), ProcessKillError(u32, io::Error), HashMismatch(PathBuf, [u8; 20]), - InvalidArgValue(&'static str), } impl fmt::Display for Error { @@ -190,9 +189,6 @@ impl fmt::Display for Error { Ok(()) } - Self::InvalidArgValue(param) => { - write!(f, "Invalid value for the argument of {}", param) - } } } } @@ -239,7 +235,6 @@ impl Error { Self::ThreadJoinError(_) => 35, Self::ProcessKillError(_, _) => 36, Self::HashMismatch(_, _) => 37, - Self::InvalidArgValue(_) => 38, } } } diff --git a/src/main.rs b/src/main.rs index c91c792..f3fd30b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -10,7 +10,8 @@ mod update; mod util; use clap::{ - crate_authors, crate_description, crate_name, crate_version, Arg, Command, + builder::ValueParser, crate_authors, crate_description, crate_name, + crate_version, value_parser, Arg, ArgAction, Command, }; use error::Error; use reqwest::blocking as rb; @@ -60,6 +61,7 @@ fn run() -> Result<(), Error> { .help("Configuration JSON file to use.") .long_help(CONFIG_LONG_HELP) .takes_value(true) + .value_parser(ValueParser::path_buf()) .conflicts_with("no-config"), ) .arg( @@ -84,7 +86,8 @@ fn run() -> Result<(), Error> { will not be written to the config. Usually you won't \ need this option.", ) - .takes_value(true), + .takes_value(true) + .value_parser(ValueParser::path_buf()), ) .arg( Arg::new("cache-dir") @@ -100,7 +103,8 @@ fn run() -> Result<(), Error> { named \"cache/\" and is in the same directory as the \ config file. Usually you won't need this option.", ) - .takes_value(true), + .takes_value(true) + .value_parser(ValueParser::path_buf()), ) .arg( Arg::new("no-auto-update") @@ -131,7 +135,7 @@ fn run() -> Result<(), Error> { (assuming `-d` is not supplied).", ) .takes_value(true) - .multiple_occurrences(true) + .action(ArgAction::Append) .multiple_values(true), ) .arg( @@ -177,7 +181,8 @@ fn run() -> Result<(), Error> { 5. Currently works for downloading files, including the \ manifest.", ) - .takes_value(true), + .takes_value(true) + .value_parser(value_parser!(NonZeroUsize)), ) .arg( Arg::new("dry-update") @@ -196,20 +201,19 @@ fn run() -> Result<(), Error> { ) .get_matches(); - let quiet = arg_matches.is_present("quiet"); - let max_tries = if let Some(tries_str) = arg_matches.value_of("tries") { - tries_str - .parse() - .map_err(|_| Error::InvalidArgValue("--tries/-t"))? - } else { - NonZeroUsize::new(5).unwrap() - }; + let quiet = arg_matches.contains_id("quiet"); + let max_tries = + if let Some(tries) = arg_matches.get_one::("tries") { + *tries + } else { + NonZeroUsize::new(5).unwrap() + }; let (mut config, config_path) = config::get_config( - arg_matches.is_present("no-config"), - arg_matches.value_of("config"), - arg_matches.value_of("install-dir"), - arg_matches.value_of("cache-dir"), + arg_matches.contains_id("no-config"), + arg_matches.get_one("config").cloned(), + arg_matches.get_one("install-dir").cloned(), + arg_matches.get_one("cache-dir").cloned(), quiet, )?; @@ -217,13 +221,13 @@ fn run() -> Result<(), Error> { .build() .map_err(Error::HttpClientCreateError)?; - if !arg_matches.is_present("no-auto-update") { + if !arg_matches.contains_id("no-auto-update") { update::update( &config, &client, quiet, max_tries, - arg_matches.is_present("dry-update"), + arg_matches.contains_id("dry-update"), )?; if !quiet { @@ -236,8 +240,10 @@ fn run() -> Result<(), Error> { &config_path, &client, quiet, - arg_matches.values_of("username"), - arg_matches.is_present("detach"), + arg_matches + .get_many::("username") + .map(|it| it.map(|v| v.as_str())), + arg_matches.contains_id("detach"), max_tries, ) }