Skip to content

Commit

Permalink
v1.2.0: upgrade clap v3 → v4
Browse files Browse the repository at this point in the history
The upgrade to clap v4 has mostly only changed the help texts
(specifically the ones that you get from passing either the `-h`
or `--help` flags). These now look quite a bit different, although the
content is substantially identical. There’s no more coloration, and help
texts should now only wrap at your terminal’s width, which might be
difficult to read if your terminal is many characters wide. In any case,
this required adding the `"wrap_help"` feature to clap. Other features
were also added, but they are not new; they were just not previously
optional.
  • Loading branch information
JonathanHelianthicusDoe committed Sep 30, 2022
1 parent 3d827ac commit 2ddbe9b
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 30 deletions.
20 changes: 14 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "shticker_book_unwritten"
version = "1.1.0"
version = "1.2.0"
authors = ["Dr. Jonathan Helianthicus Doe, IV <[email protected]>"]
edition = "2021"
description = "Minimal CLI launcher for the Toontown Rewritten MMORPG"
Expand All @@ -14,17 +14,25 @@ license = "GPL-3.0-or-later"
[dependencies]
bzip2 = "0.4.3"
rpassword = "7.0.0"
serde = { version = "1.0.140", features = ["derive"] }
serde_json = "1.0.82"
serde = { version = "1.0.145", features = ["derive"] }
serde_json = "1.0.85"
sha-1 = "0.10.0"

[dependencies.clap]
version = "3.2.15"
version = "4.0.4"
default-features = false
features = ["std", "cargo", "suggestions"]
features = [
"std",
"cargo",
"suggestions",
"help",
"usage",
"error-context",
"wrap_help",
]

[dependencies.reqwest]
version = "0.11.11"
version = "0.11.12"
default-features = false
features = ["blocking", "rustls-tls"]

Expand Down
64 changes: 40 additions & 24 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,17 @@ mod update;
mod util;

use clap::{
builder::ValueParser, crate_authors, crate_description, crate_name,
crate_version, value_parser, Arg, ArgAction, Command,
builder::{ArgPredicate, ValueParser},
crate_authors, crate_description, crate_name, crate_version, value_parser,
Arg, ArgAction, Command,
};
use error::Error;
use reqwest::blocking as rb;
use std::{num::NonZeroUsize, process};

fn main() {
if let Err(e) = run() {
eprintln!("{}", e);
eprintln!("{e}");

process::exit(e.return_code())
}
Expand Down Expand Up @@ -60,18 +61,23 @@ fn run() -> Result<(), Error> {
.value_name("CONFIG_FILE")
.help("Configuration JSON file to use.")
.long_help(CONFIG_LONG_HELP)
.takes_value(true)
.num_args(1)
.value_parser(ValueParser::path_buf())
.conflicts_with("no-config"),
.conflicts_with("no-config")
.action(ArgAction::Set),
)
.arg(
Arg::new("no-config")
.long("no-config")
.aliases(&["no-conf", "no-configuration"])
.help("Don't read nor write any config files.")
.takes_value(false)
.requires_all(&["install-dir", "cache-dir"])
.conflicts_with("config"),
.num_args(0)
.requires_ifs([
(ArgPredicate::IsPresent, "install-dir"),
(ArgPredicate::IsPresent, "cache-dir"),
])
.conflicts_with("config")
.action(ArgAction::SetTrue),
)
.arg(
Arg::new("install-dir")
Expand All @@ -86,8 +92,9 @@ fn run() -> Result<(), Error> {
will not be written to the config. Usually you won't \
need this option.",
)
.takes_value(true)
.value_parser(ValueParser::path_buf()),
.num_args(1)
.value_parser(ValueParser::path_buf())
.action(ArgAction::Set),
)
.arg(
Arg::new("cache-dir")
Expand All @@ -103,7 +110,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)
.num_args(1)
.action(ArgAction::Set)
.value_parser(ValueParser::path_buf()),
)
.arg(
Expand All @@ -115,7 +123,8 @@ fn run() -> Result<(), Error> {
"Suppresses auto-updating, although you can still decide \
to update via the `update`/`up` command.",
)
.takes_value(false),
.num_args(0)
.action(ArgAction::SetTrue),
)
.arg(
Arg::new("username")
Expand All @@ -134,9 +143,8 @@ fn run() -> Result<(), Error> {
Then, if the login(s) succeed, command mode is entered \
(assuming `-d` is not supplied).",
)
.takes_value(true)
.action(ArgAction::Append)
.multiple_values(true),
.num_args(1..)
.action(ArgAction::Append),
)
.arg(
Arg::new("detach")
Expand All @@ -155,7 +163,8 @@ fn run() -> Result<(), Error> {
reparented to the init process (actually, on Linux, the \
closest parent process marked as a subreaper).",
)
.takes_value(false),
.num_args(0)
.action(ArgAction::SetTrue),
)
.arg(
Arg::new("quiet")
Expand All @@ -165,7 +174,8 @@ fn run() -> Result<(), Error> {
"Don't output anything unless necessary or explicitly \
requested.",
)
.takes_value(false),
.num_args(0)
.action(ArgAction::SetTrue),
)
.arg(
Arg::new("tries")
Expand All @@ -181,7 +191,8 @@ fn run() -> Result<(), Error> {
5. Currently works for downloading files, including the \
manifest.",
)
.takes_value(true)
.num_args(1)
.action(ArgAction::Set)
.value_parser(value_parser!(NonZeroUsize)),
)
.arg(
Expand All @@ -196,12 +207,13 @@ fn run() -> Result<(), Error> {
not updates are available, and for which files; that \
is, no updates will be downloaded nor applied.",
)
.takes_value(false)
.num_args(0)
.action(ArgAction::SetTrue)
.conflicts_with("no-auto-update"),
)
.get_matches();

let quiet = arg_matches.contains_id("quiet");
let quiet = arg_matches.get_one("quiet").copied().unwrap_or(false);
let max_tries =
if let Some(tries) = arg_matches.get_one::<NonZeroUsize>("tries") {
*tries
Expand All @@ -210,7 +222,7 @@ fn run() -> Result<(), Error> {
};

let (mut config, config_path) = config::get_config(
arg_matches.contains_id("no-config"),
arg_matches.get_one("no-config").copied().unwrap_or(false),
arg_matches.get_one("config").cloned(),
arg_matches.get_one("install-dir").cloned(),
arg_matches.get_one("cache-dir").cloned(),
Expand All @@ -221,13 +233,17 @@ fn run() -> Result<(), Error> {
.build()
.map_err(Error::HttpClientCreateError)?;

if !arg_matches.contains_id("no-auto-update") {
if !arg_matches
.get_one("no-auto-update")
.copied()
.unwrap_or(false)
{
update::update(
&config,
&client,
quiet,
max_tries,
arg_matches.contains_id("dry-update"),
arg_matches.get_one("dry-update").copied().unwrap_or(false),
)?;

if !quiet {
Expand All @@ -243,7 +259,7 @@ fn run() -> Result<(), Error> {
arg_matches
.get_many::<String>("username")
.map(|it| it.map(|v| v.as_str())),
arg_matches.contains_id("detach"),
arg_matches.get_one("detach").copied().unwrap_or(false),
max_tries,
)
}

0 comments on commit 2ddbe9b

Please sign in to comment.