From 06923770e67375333314d47c796411172c49006e Mon Sep 17 00:00:00 2001 From: dullbananas Date: Thu, 8 Jun 2023 02:35:14 +0000 Subject: [PATCH] Replace structopt with clap (allocations increased) --- Cargo.lock | 138 ++++++++++------------------------ Cargo.toml | 2 +- src/command/build.rs | 39 +++++----- src/command/mod.rs | 34 ++++----- src/command/publish/access.rs | 2 +- src/command/test.rs | 32 ++++---- src/lib.rs | 10 +-- src/main.rs | 6 +- tests/all/main.rs | 2 +- 9 files changed, 97 insertions(+), 168 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 1372beba..a927710e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -52,15 +52,6 @@ dependencies = [ "libc", ] -[[package]] -name = "ansi_term" -version = "0.12.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d52a9bb7ec0cf484c551830a7ce27bd20d67eac647e1befb56b0be4ee39a55d2" -dependencies = [ - "winapi", -] - [[package]] name = "anstream" version = "0.3.2" @@ -327,19 +318,46 @@ dependencies = [ [[package]] name = "clap" -version = "2.34.0" +version = "4.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a0610544180c38b88101fecf2dd634b174a62eef6946f84dfc6a7127512b381c" +checksum = "401a4694d2bf92537b6867d94de48c4842089645fdcdf6c71865b175d836e9c2" dependencies = [ - "ansi_term", - "atty", + "clap_builder", + "clap_derive", + "once_cell", +] + +[[package]] +name = "clap_builder" +version = "4.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72394f3339a76daf211e57d4bcb374410f3965dcc606dd0e03738c7888766980" +dependencies = [ + "anstream", + "anstyle", "bitflags", - "strsim 0.8.0", - "textwrap", - "unicode-width", - "vec_map", + "clap_lex", + "strsim", ] +[[package]] +name = "clap_derive" +version = "4.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b8cd2b2a819ad6eec39e8f1d6b53001af1e5469f8c177579cdaeb313115b825f" +dependencies = [ + "heck", + "proc-macro2", + "quote", + "syn 2.0.18", +] + +[[package]] +name = "clap_lex" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2da6da31387c7e4ef160ffab6d5e7f00c42626fe39aea70a7b0f1773f7dd6c1b" + [[package]] name = "colorchoice" version = "1.0.0" @@ -742,12 +760,9 @@ checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888" [[package]] name = "heck" -version = "0.3.3" +version = "0.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6d621efb26863f0e9924c6ac577e8275e5e6b77455db64ffa6c65c904e9e132c" -dependencies = [ - "unicode-segmentation", -] +checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8" [[package]] name = "hermit-abi" @@ -1172,30 +1187,6 @@ dependencies = [ "termtree", ] -[[package]] -name = "proc-macro-error" -version = "1.0.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c" -dependencies = [ - "proc-macro-error-attr", - "proc-macro2", - "quote", - "syn 1.0.109", - "version_check", -] - -[[package]] -name = "proc-macro-error-attr" -version = "1.0.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869" -dependencies = [ - "proc-macro2", - "quote", - "version_check", -] - [[package]] name = "proc-macro2" version = "1.0.59" @@ -1513,42 +1504,12 @@ version = "0.5.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6e63cff320ae2c57904679ba7cb63280a3dc4613885beafb148ee7bf9aa9042d" -[[package]] -name = "strsim" -version = "0.8.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a" - [[package]] name = "strsim" version = "0.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623" -[[package]] -name = "structopt" -version = "0.3.26" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0c6b5c64445ba8094a6ab0c3cd2ad323e07171012d9c98b0b15651daf1787a10" -dependencies = [ - "clap", - "lazy_static", - "structopt-derive", -] - -[[package]] -name = "structopt-derive" -version = "0.4.18" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dcb5ae327f9cc13b68763b5749770cb9e048a99bd9dfdfa58d0cf05d5f64afe0" -dependencies = [ - "heck", - "proc-macro-error", - "proc-macro2", - "quote", - "syn 1.0.109", -] - [[package]] name = "subtle" version = "2.5.0" @@ -1617,15 +1578,6 @@ version = "0.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3369f5ac52d5eb6ab48c6b4ffdc8efbcad6b89c765749064ba298f2c68a16a76" -[[package]] -name = "textwrap" -version = "0.11.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d326610f408c7a4eb6f51c37c330e496b08506c9457c9d34287ecc38809fb060" -dependencies = [ - "unicode-width", -] - [[package]] name = "thiserror" version = "1.0.40" @@ -1764,12 +1716,6 @@ dependencies = [ "tinyvec", ] -[[package]] -name = "unicode-segmentation" -version = "1.10.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1dd624098567895118886609431a7c3b8f516e41d30e0643f03d94592a147e36" - [[package]] name = "unicode-width" version = "0.1.10" @@ -1832,12 +1778,6 @@ version = "0.2.15" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426" -[[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.4" @@ -1939,6 +1879,7 @@ dependencies = [ "binary-install", "cargo_metadata", "chrono", + "clap", "console", "dhat", "dialoguer", @@ -1956,8 +1897,7 @@ dependencies = [ "serde_json", "serial_test", "siphasher", - "strsim 0.10.0", - "structopt", + "strsim", "tempfile", "toml 0.5.11", "ureq", diff --git a/Cargo.toml b/Cargo.toml index 1fa675cb..038ff155 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,6 +16,7 @@ atty = "0.2.14" binary-install = "0.1.0" cargo_metadata = "0.15.2" chrono = "0.4.23" +clap = { version = "4.3.2", features = ["derive"] } console = "0.15.5" dhat = { version = "0.3.2", optional = true } dialoguer = "0.10.3" @@ -31,7 +32,6 @@ serde_ignored = "0.1.7" serde_json = "1.0.91" siphasher = "0.3.10" strsim = "0.10.0" -structopt = "0.3.26" toml = "0.5.11" ureq = { version = "2.6.2", features = ["json"] } walkdir = "2.3.2" diff --git a/src/command/build.rs b/src/command/build.rs index b4d4f829..6267c6c4 100644 --- a/src/command/build.rs +++ b/src/command/build.rs @@ -19,7 +19,6 @@ use std::fmt; use std::path::PathBuf; use std::str::FromStr; use std::time::Instant; -use structopt::clap::AppSettings; /// Everything required to configure and run the `wasm-pack build` command. #[allow(missing_docs)] @@ -109,74 +108,70 @@ pub enum BuildProfile { } /// Everything required to configure and run the `wasm-pack build` command. -#[derive(Debug, StructOpt)] -#[structopt( +#[derive(Debug, Parser)] +#[command( // Allows unknown `--option`s to be parsed as positional arguments, so we can forward it to `cargo`. - setting = AppSettings::AllowLeadingHyphen, - - // Allows `--` to be parsed as an argument, so we can forward it to `cargo`. - setting = AppSettings::TrailingVarArg, + allow_hyphen_values = true, )] pub struct BuildOptions { /// The path to the Rust crate. If not set, searches up the path from the current directory. - #[structopt(parse(from_os_str))] pub path: Option, /// The npm scope to use in package.json, if any. - #[structopt(long = "scope", short = "s")] + #[arg(long = "scope", short = 's')] pub scope: Option, - #[structopt(long = "mode", short = "m", default_value = "normal")] + #[arg(long = "mode", short = 'm', default_value = "normal")] /// Sets steps to be run. [possible values: no-install, normal, force] pub mode: InstallMode, - #[structopt(long = "no-typescript")] + #[arg(long = "no-typescript")] /// By default a *.d.ts file is generated for the generated JS file, but /// this flag will disable generating this TypeScript file. pub disable_dts: bool, - #[structopt(long = "weak-refs")] + #[arg(long = "weak-refs")] /// Enable usage of the JS weak references proposal. pub weak_refs: bool, - #[structopt(long = "reference-types")] + #[arg(long = "reference-types")] /// Enable usage of WebAssembly reference types. pub reference_types: bool, - #[structopt(long = "target", short = "t", default_value = "bundler")] + #[arg(long = "target", short = 't', default_value = "bundler")] /// Sets the target environment. [possible values: bundler, nodejs, web, no-modules] pub target: Target, - #[structopt(long = "debug")] + #[arg(long = "debug")] /// Deprecated. Renamed to `--dev`. pub debug: bool, - #[structopt(long = "dev")] + #[arg(long = "dev")] /// Create a development build. Enable debug info, and disable /// optimizations. pub dev: bool, - #[structopt(long = "release")] + #[arg(long = "release")] /// Create a release build. Enable optimizations and disable debug info. pub release: bool, - #[structopt(long = "profiling")] + #[arg(long = "profiling")] /// Create a profiling build. Enable optimizations and debug info. pub profiling: bool, - #[structopt(long = "out-dir", short = "d", default_value = "pkg")] + #[arg(long = "out-dir", short = 'd', default_value = "pkg")] /// Sets the output directory with a relative path. pub out_dir: String, - #[structopt(long = "out-name")] + #[arg(long = "out-name")] /// Sets the output file names. Defaults to package name. pub out_name: Option, - #[structopt(long = "no-pack", alias = "no-package")] + #[arg(long = "no-pack", alias = "no-package")] /// Option to not generate a package.json pub no_pack: bool, - #[structopt(allow_hyphen_values = true)] + #[structopt(allow_hyphen_values = true, trailing_var_arg = true)] /// List of extra options to pass to `cargo build` pub extra_options: Vec, } diff --git a/src/command/mod.rs b/src/command/mod.rs index 631eca71..3090e8b9 100644 --- a/src/command/mod.rs +++ b/src/command/mod.rs @@ -22,62 +22,60 @@ use log::info; use std::path::PathBuf; /// The various kinds of commands that `wasm-pack` can execute. -#[derive(Debug, StructOpt)] +#[derive(Debug, Subcommand)] pub enum Command { /// 🏗️ build your npm package! - #[structopt(name = "build", alias = "init")] + #[command(name = "build", alias = "init")] Build(BuildOptions), - #[structopt(name = "pack")] + #[command(name = "pack")] /// 🍱 create a tar of your npm package but don't publish! Pack { /// The path to the Rust crate. If not set, searches up the path from the current directory. - #[structopt(parse(from_os_str))] path: Option, }, - #[structopt(name = "new")] + #[command(name = "new")] /// 🐑 create a new project with a template Generate { /// The name of the project name: String, /// The URL to the template - #[structopt( + #[arg( long = "template", - short = "temp", + short = 't', default_value = "https://github.com/rustwasm/wasm-pack-template" )] template: String, - #[structopt(long = "mode", short = "m", default_value = "normal")] + #[arg(long = "mode", short = 'm', default_value = "normal")] /// Should we install or check the presence of binary tools. [possible values: no-install, normal, force] mode: InstallMode, }, - #[structopt(name = "publish")] + #[command(name = "publish")] /// 🎆 pack up your npm package and publish! Publish { - #[structopt(long = "target", short = "t", default_value = "bundler")] + #[arg(long = "target", short = 't', default_value = "bundler")] /// Sets the target environment. [possible values: bundler, nodejs, web, no-modules] target: String, /// The access level for the package to be published - #[structopt(long = "access", short = "a")] + #[arg(long = "access", short = 'a')] access: Option, /// The distribution tag being used for publishing. /// See https://docs.npmjs.com/cli/dist-tag - #[structopt(long = "tag")] + #[arg(long = "tag")] tag: Option, /// The path to the Rust crate. If not set, searches up the path from the current directory. - #[structopt(parse(from_os_str))] path: Option, }, - #[structopt(name = "login", alias = "adduser", alias = "add-user")] + #[command(name = "login", alias = "adduser", alias = "add-user")] /// 👤 Add an npm registry user account! (aliases: adduser, add-user) Login { - #[structopt(long = "registry", short = "r")] + #[arg(long = "registry", short = 'r')] /// Default: 'https://registry.npmjs.org/'. /// The base URL of the npm package registry. If scope is also /// specified, this registry will only be used for packages with that @@ -85,13 +83,13 @@ pub enum Command { /// currently in, if any. registry: Option, - #[structopt(long = "scope", short = "s")] + #[arg(long = "scope", short = 's')] /// Default: none. /// If specified, the user and login credentials given will be /// associated with the specified scope. scope: Option, - #[structopt(long = "auth-type", short = "t")] + #[arg(long = "auth-type", short = 't')] /// Default: 'legacy'. /// Type: 'legacy', 'sso', 'saml', 'oauth'. /// What authentication strategy to use with adduser/login. Some npm @@ -100,7 +98,7 @@ pub enum Command { auth_type: Option, }, - #[structopt(name = "test")] + #[command(name = "test")] /// 👩‍🔬 test your wasm! Test(TestOptions), } diff --git a/src/command/publish/access.rs b/src/command/publish/access.rs index 6b374b4c..6ae572ca 100644 --- a/src/command/publish/access.rs +++ b/src/command/publish/access.rs @@ -3,7 +3,7 @@ use std::fmt; use std::str::FromStr; /// Represents access level for the to-be publish package. Passed to `wasm-pack publish` as a flag, e.g. `--access=public`. -#[derive(Debug)] +#[derive(Debug, Clone)] pub enum Access { /// Access is granted to all. All unscoped packages *must* be public. Public, diff --git a/src/command/test.rs b/src/command/test.rs index 0a5cff9b..aa6ea6a1 100644 --- a/src/command/test.rs +++ b/src/command/test.rs @@ -13,68 +13,64 @@ use log::info; use std::path::PathBuf; use std::str::FromStr; use std::time::Instant; -use structopt::clap::AppSettings; -#[derive(Debug, Default, StructOpt)] -#[structopt( +#[derive(Debug, Default, Parser)] +#[command( // Allows unknown `--option`s to be parsed as positional arguments, so we can forward it to `cargo`. - setting = AppSettings::AllowLeadingHyphen, - - // Allows `--` to be parsed as an argument, so we can forward it to `cargo`. - setting = AppSettings::TrailingVarArg, + allow_hyphen_values = true, )] /// Everything required to configure the `wasm-pack test` command. pub struct TestOptions { - #[structopt(long = "node")] + #[arg(long = "node")] /// Run the tests in Node.js. pub node: bool, - #[structopt(long = "firefox")] + #[arg(long = "firefox")] /// Run the tests in Firefox. This machine must have a Firefox installation. /// If the `geckodriver` WebDriver client is not on the `$PATH`, and not /// specified with `--geckodriver`, then `wasm-pack` will download a local /// copy. pub firefox: bool, - #[structopt(long = "geckodriver", parse(from_os_str))] + #[arg(long = "geckodriver")] /// The path to the `geckodriver` WebDriver client for testing in /// Firefox. Implies `--firefox`. pub geckodriver: Option, - #[structopt(long = "chrome")] + #[arg(long = "chrome")] /// Run the tests in Chrome. This machine must have a Chrome installation. /// If the `chromedriver` WebDriver client is not on the `$PATH`, and not /// specified with `--chromedriver`, then `wasm-pack` will download a local /// copy. pub chrome: bool, - #[structopt(long = "chromedriver", parse(from_os_str))] + #[arg(long = "chromedriver")] /// The path to the `chromedriver` WebDriver client for testing in /// Chrome. Implies `--chrome`. pub chromedriver: Option, - #[structopt(long = "safari")] + #[arg(long = "safari")] /// Run the tests in Safari. This machine must have a Safari installation, /// and the `safaridriver` WebDriver client must either be on the `$PATH` or /// specified explicitly with the `--safaridriver` flag. `wasm-pack` cannot /// download the `safaridriver` WebDriver client for you. pub safari: bool, - #[structopt(long = "safaridriver", parse(from_os_str))] + #[arg(long = "safaridriver")] /// The path to the `safaridriver` WebDriver client for testing in /// Safari. Implies `--safari`. pub safaridriver: Option, - #[structopt(long = "headless")] + #[arg(long = "headless")] /// When running browser tests, run the browser in headless mode without any /// UI or windows. pub headless: bool, - #[structopt(long = "mode", short = "m", default_value = "normal")] + #[arg(long = "mode", short = 'm', default_value = "normal")] /// Sets steps to be run. [possible values: no-install, normal] pub mode: InstallMode, - #[structopt(long = "release", short = "r")] + #[arg(long = "release", short = 'r')] /// Build with the release profile. pub release: bool, @@ -84,7 +80,7 @@ pub struct TestOptions { /// /// This is a workaround to allow wasm pack to provide the same command line interface as `cargo`. /// See for more information. - #[structopt(allow_hyphen_values = true)] + #[structopt(allow_hyphen_values = true, trailing_var_arg = true)] pub path_and_extra_options: Vec, } diff --git a/src/lib.rs b/src/lib.rs index 1f5c5085..563dc7c7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -16,7 +16,7 @@ extern crate serde_derive; extern crate serde_ignored; extern crate serde_json; #[macro_use] -extern crate structopt; +extern crate clap; extern crate binary_install; extern crate chrono; extern crate dialoguer; @@ -49,21 +49,21 @@ use crate::progressbar::{LogLevel, ProgressOutput}; pub static PBAR: ProgressOutput = ProgressOutput::new(); /// 📦 ✨ pack and publish your wasm! -#[derive(Debug, StructOpt)] +#[derive(Debug, Parser)] pub struct Cli { /// The subcommand to run. #[structopt(subcommand)] // Note that we mark a field as a subcommand pub cmd: command::Command, /// Log verbosity is based off the number of v used - #[structopt(long = "verbose", short = "v", parse(from_occurrences))] + #[arg(long = "verbose", short = 'v', action = clap::ArgAction::Count)] pub verbosity: u8, - #[structopt(long = "quiet", short = "q")] + #[arg(long = "quiet", short = 'q')] /// No output printed to stdout pub quiet: bool, - #[structopt(long = "log-level", default_value = "info")] + #[arg(long = "log-level", default_value = "info")] /// The maximum level of messages that should be logged by wasm-pack. [possible values: info, warn, error] pub log_level: LogLevel, } diff --git a/src/main.rs b/src/main.rs index 7075eb6d..a02c4e3b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,19 +2,19 @@ extern crate anyhow; extern crate atty; +extern crate clap; extern crate env_logger; extern crate human_panic; extern crate log; -extern crate structopt; extern crate wasm_pack; extern crate which; use anyhow::Result; +use clap::Parser; use std::env; use std::panic; use std::sync::mpsc; use std::thread; -use structopt::StructOpt; use wasm_pack::{ build::{self, WasmPackVersion}, command::run_wasm_pack, @@ -86,7 +86,7 @@ fn run() -> Result<()> { } } - let args = Cli::from_args(); + let args = Cli::parse(); PBAR.set_log_level(args.log_level); diff --git a/tests/all/main.rs b/tests/all/main.rs index c99a871c..a7cdb8bf 100644 --- a/tests/all/main.rs +++ b/tests/all/main.rs @@ -8,7 +8,7 @@ extern crate binary_install; extern crate serde_json; #[macro_use] extern crate serial_test; -extern crate structopt; +extern crate clap; extern crate tempfile; extern crate wasm_pack;