Skip to content

Commit

Permalink
v1.0.6: update clap 3.1 -> 3.2
Browse files Browse the repository at this point in the history
  • Loading branch information
JonathanHelianthicusDoe committed Jun 14, 2022
1 parent 4da660f commit 379c3a8
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 44 deletions.
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "shticker_book_unwritten"
version = "1.0.5"
version = "1.0.6"
authors = ["Dr. Jonathan Helianthicus Doe, IV <[email protected]>"]
edition = "2021"
description = "Minimal CLI launcher for the Toontown Rewritten MMORPG"
Expand All @@ -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]
Expand Down
27 changes: 12 additions & 15 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,34 +42,31 @@ impl Config {

pub fn get_config(
no_config: bool,
config_path: Option<&str>,
install_path: Option<&str>,
cache_path: Option<&str>,
config_path: Option<PathBuf>,
install_path: Option<PathBuf>,
cache_path: Option<PathBuf>,
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
}
};

if !no_config {
let config_path = if let Some(s) = config_path {
PathBuf::from(s)
s
} else {
#[cfg(target_os = "linux")]
{
Expand Down Expand Up @@ -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,
Expand Down
5 changes: 0 additions & 5 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -190,9 +189,6 @@ impl fmt::Display for Error {

Ok(())
}
Self::InvalidArgValue(param) => {
write!(f, "Invalid value for the argument of {}", param)
}
}
}
}
Expand Down Expand Up @@ -239,7 +235,6 @@ impl Error {
Self::ThreadJoinError(_) => 35,
Self::ProcessKillError(_, _) => 36,
Self::HashMismatch(_, _) => 37,
Self::InvalidArgValue(_) => 38,
}
}
}
48 changes: 27 additions & 21 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(
Expand All @@ -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")
Expand All @@ -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")
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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")
Expand All @@ -196,34 +201,33 @@ 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::<NonZeroUsize>("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,
)?;

let client = rb::ClientBuilder::new()
.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 {
Expand All @@ -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::<String>("username")
.map(|it| it.map(|v| v.as_str())),
arg_matches.contains_id("detach"),
max_tries,
)
}

0 comments on commit 379c3a8

Please sign in to comment.