Skip to content

Commit

Permalink
Update clap from 3 to 4
Browse files Browse the repository at this point in the history
  • Loading branch information
ehuss committed Oct 12, 2023
1 parent d010c4d commit 49624c7
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 38 deletions.
100 changes: 75 additions & 25 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ include = ["/src"]

[dependencies]
anyhow = "1.0.37"
clap = { version = "3.1.18", optional = true, features = ["cargo"] }
clap = { version = "4.4.6", optional = true, features = ["cargo"] }
env_logger = { version = "0.9.0", optional = true }
flate2 = "1.0.19"
log = "0.4.11"
Expand All @@ -28,7 +28,8 @@ tar = { version = "0.4.30", default-features = false }
tempfile = "3.1.0"

[features]
default = ["clap", "env_logger", "reqwest/default-tls"]
bin = ["dep:clap", "dep:env_logger"]
default = ["bin", "reqwest/default-tls"]
rustls = ["reqwest/rustls-tls"]

[lib]
Expand Down
23 changes: 12 additions & 11 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use cargo_clone;
use clap::{Arg, Command};
use clap::{Arg, ArgAction, Command};
use env_logger::{Builder, Target};
use std::{env, io::Write, process::exit};

Expand Down Expand Up @@ -31,6 +31,7 @@ fn main() {

let matches = Command::new("cargo-clone")
.version(clap::crate_version!())
.disable_version_flag(true)
.bin_name("cargo")
.subcommand_required(true)
.propagate_version(true)
Expand All @@ -41,8 +42,8 @@ fn main() {
.arg(
Arg::new("method")
.long("method")
.takes_value(true)
.possible_values(&["crate", "git", "hg", "pijul", "fossil", "auto"])
.action(ArgAction::Set)
.value_parser(["crate", "git", "hg", "pijul", "fossil", "auto"])
.default_value("auto")
.help("Method to fetch package."),
)
Expand All @@ -54,13 +55,13 @@ fn main() {
.arg(
Arg::new("version")
.long("version")
.takes_value(true)
.action(ArgAction::Set)
.help("Version to download."),
)
.arg(
Arg::new("extra")
.allow_hyphen_values(true)
.multiple_occurrences(true)
.action(ArgAction::Append)
.help("Additional arguments passed to clone command."),
),
)
Expand All @@ -69,19 +70,19 @@ fn main() {
.subcommand_matches("clone")
.expect("Expected `clone` subcommand.");

let method = submatches.value_of("method").unwrap();
let name = submatches.value_of("name").unwrap();
let version = submatches.value_of("version");
let method = submatches.get_one::<String>("method").unwrap();
let name = submatches.get_one::<String>("name").unwrap();
let version = submatches.get_one::<String>("version");
let extra: Vec<&str> = submatches
.values_of("extra")
.map_or_else(Vec::new, |e| e.collect());
.get_many::<String>("extra")
.map_or_else(Vec::new, |e| e.map(|x| x.as_str()).collect());

let cloner = cargo_clone::Cloner::new();
let result = cloner.clone(
// UNWRAP: The argument parser should guarantee only sane values get passed here
cargo_clone::CloneMethodKind::from(method).unwrap(),
name,
version,
version.map(|x| x.as_str()),
&extra,
);
if let Err(e) = result {
Expand Down

0 comments on commit 49624c7

Please sign in to comment.