From 6170233eb05097818c6663f208efe1caf1d922d2 Mon Sep 17 00:00:00 2001 From: Marcin Puc Date: Sun, 2 Jan 2022 15:16:28 +0100 Subject: [PATCH] refactor: switch parsing to clap v3 --- Cargo.lock | 118 ++++++++++++++++++++++++++-------------------------- Cargo.toml | 3 +- src/cli.rs | 35 +++++++++------- src/main.rs | 4 +- 4 files changed, 84 insertions(+), 76 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 9aebd85..a4bbc02 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,15 +2,6 @@ # It is not intended for manual editing. version = 3 -[[package]] -name = "ansi_term" -version = "0.11.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ee49baf6cb617b853aa8d93bf420db2383fab46d314482ca2803b40d5fde979b" -dependencies = [ - "winapi", -] - [[package]] name = "atty" version = "0.2.14" @@ -72,17 +63,41 @@ dependencies = [ [[package]] name = "clap" -version = "2.33.3" +version = "3.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "37e58ac78573c40708d45522f0d80fa2f01cc4f9b4e2bf749807255454312002" +checksum = "d17bf219fcd37199b9a29e00ba65dfb8cd5b2688b7297ec14ff829c40ac50ca9" dependencies = [ - "ansi_term", "atty", "bitflags", + "clap_derive", + "indexmap", + "lazy_static", + "os_str_bytes", "strsim", + "termcolor", "textwrap", - "unicode-width", - "vec_map", +] + +[[package]] +name = "clap_complete" +version = "3.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "60d123fbea4c5d9799cffd44051e2125c880efd23b3b7c529baf3ea5508c8736" +dependencies = [ + "clap", +] + +[[package]] +name = "clap_derive" +version = "3.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e1b9752c030a14235a0bd5ef3ad60a1dcac8468c30921327fc8af36b20c790b9" +dependencies = [ + "heck", + "proc-macro-error", + "proc-macro2", + "quote", + "syn", ] [[package]] @@ -134,6 +149,12 @@ dependencies = [ "wasi", ] +[[package]] +name = "hashbrown" +version = "0.11.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ab5ef0d4909ef3724cc8cce6ccc8572c5c817592e9285f5464f8e86f8bd3726e" + [[package]] name = "heck" version = "0.3.3" @@ -152,6 +173,16 @@ dependencies = [ "libc", ] +[[package]] +name = "indexmap" +version = "1.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bc633605454125dec4b66843673f01c7df2b89479b32e0ed634e43a91cff62a5" +dependencies = [ + "autocfg", + "hashbrown", +] + [[package]] name = "itertools" version = "0.10.1" @@ -207,17 +238,27 @@ dependencies = [ "autocfg", ] +[[package]] +name = "os_str_bytes" +version = "6.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e22443d1643a904602595ba1cd8f7d896afe56d26712531c5ff73a15b2fbf64" +dependencies = [ + "memchr", +] + [[package]] name = "pakr" version = "0.6.0" dependencies = [ "bstr", + "clap", + "clap_complete", "confy", "itertools", "log", "serde", "stderrlog", - "structopt", ] [[package]] @@ -322,33 +363,9 @@ dependencies = [ [[package]] name = "strsim" -version = "0.8.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a" - -[[package]] -name = "structopt" -version = "0.3.25" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "40b9788f4202aa75c240ecc9c15c65185e6a39ccdeb0fd5d008b98825464c87c" -dependencies = [ - "clap", - "lazy_static", - "structopt-derive", -] - -[[package]] -name = "structopt-derive" -version = "0.4.18" +version = "0.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dcb5ae327f9cc13b68763b5749770cb9e048a99bd9dfdfa58d0cf05d5f64afe0" -dependencies = [ - "heck", - "proc-macro-error", - "proc-macro2", - "quote", - "syn", -] +checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623" [[package]] name = "syn" @@ -372,12 +389,9 @@ dependencies = [ [[package]] name = "textwrap" -version = "0.11.0" +version = "0.14.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d326610f408c7a4eb6f51c37c330e496b08506c9457c9d34287ecc38809fb060" -dependencies = [ - "unicode-width", -] +checksum = "0066c8d12af8b5acd21e00547c3797fde4e8677254a7ee429176ccebbe93dd80" [[package]] name = "thread_local" @@ -413,24 +427,12 @@ version = "1.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8895849a949e7845e06bd6dc1aa51731a103c42707010a5b591c0038fb73385b" -[[package]] -name = "unicode-width" -version = "0.1.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3ed742d4ea2bd1176e236172c8429aaf54486e7ac098db29ffe6529e0ce50973" - [[package]] name = "unicode-xid" version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3" -[[package]] -name = "vec_map" -version = "0.8.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191" - [[package]] name = "version_check" version = "0.9.3" diff --git a/Cargo.toml b/Cargo.toml index ff70bf7..5026fa3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,8 +13,9 @@ edition = "2018" bstr = "0.2" confy = "0.4" +clap = { version = "3.0", features = ["derive"] } +clap_complete = "3.0" itertools = "0.10" log = "0.4" serde = { version = "1.0", features = ["derive"] } stderrlog = "0.5" -structopt = "0.3" diff --git a/src/cli.rs b/src/cli.rs index 9a8fd81..2cede2e 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -1,18 +1,23 @@ use std::os::unix::process::ExitStatusExt; use std::process::ExitStatus; -use structopt::clap::Shell; -use structopt::StructOpt; +use clap::{IntoApp, Parser, Subcommand}; +use clap_complete::Shell; pub const PKG_NAME: &str = env!("CARGO_PKG_NAME"); pub fn generate_completion(shell: Shell) -> std::io::Result { - Cli::clap().gen_completions_to(PKG_NAME, shell, &mut std::io::stdout()); + clap_complete::generate( + shell, + &mut Cli::into_app(), + PKG_NAME, + &mut std::io::stdout(), + ); Ok(ExitStatus::from_raw(0)) } -#[derive(Copy, Clone, Debug, StructOpt)] +#[derive(Copy, Clone, Debug, Subcommand)] pub enum Orphans { /// List orphans List, @@ -27,30 +32,30 @@ impl Default for Orphans { } } -#[derive(Debug, StructOpt)] +#[derive(Debug, Subcommand)] pub enum Command { /// Install packages Install { /// Packages to be installed - #[structopt(value_name = "PKG", required = true, parse(from_str))] + #[clap(value_name = "PKG", required = true)] packages: Vec, /// Mark installed packages as dependencies - #[structopt(long, short = "d")] + #[clap(long, short = 'd')] as_deps: bool, }, /// Remove packages Uninstall { /// Packages to be removed - #[structopt(value_name = "PKG", required = true, parse(from_str))] + #[clap(value_name = "PKG", required = true)] packages: Vec, }, /// Display package info Info { /// Packages to be inspected - #[structopt(value_name = "PKG", required = true, parse(from_str))] + #[clap(value_name = "PKG", required = true)] packages: Vec, }, @@ -65,14 +70,14 @@ pub enum Command { /// Manage orphaned packages Orphans { - #[structopt(subcommand)] + #[clap(subcommand)] cmd: Option, }, /// Generate a completion script Completion { /// Shell type - #[structopt(value_name = "SHELL", possible_values = &["bash", "zsh", "fish", "powershell", "elvish"])] + #[clap(value_name = "SHELL", arg_enum)] shell: Shell, }, } @@ -87,13 +92,13 @@ pub enum Command { /// requires_root = true # whether this wrapper needs root permissions (granted via sudo) /// /// If this file is missing, a default configuration is created that runs `sudo pacman`. -#[derive(Debug, StructOpt)] -#[structopt(verbatim_doc_comment)] +#[derive(Debug, Parser)] +#[clap(verbatim_doc_comment, version)] pub struct Cli { - #[structopt(subcommand)] + #[clap(subcommand)] pub sub: Command, /// Display verbose logs (debug etc.) - #[structopt(short, long, global = true)] + #[clap(short, long, global = true)] pub verbose: bool, } diff --git a/src/main.rs b/src/main.rs index ce0db3d..4d1351c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,7 @@ use std::os::unix::process::ExitStatusExt; +use clap::Parser; use log::{debug, error}; -use structopt::StructOpt; use pakr::cli::{Cli, Command, Orphans}; use pakr::wrapper::PacmanWrapper; @@ -14,7 +14,7 @@ fn verbosity(verbose: bool) -> usize { } fn run_app() -> std::io::Result { - let cli = Cli::from_args(); + let cli = Cli::parse(); let pac = PacmanWrapper::from_config()?; stderrlog::new()