update to clap 4 from structopt

master
Muhammad Hamza 2 years ago
parent 372733b007
commit 73e059f754
No known key found for this signature in database
GPG Key ID: B7812BE5DBACA4E0
  1. 2
      Cargo.toml
  2. 43
      src/command/build.rs
  3. 38
      src/command/mod.rs
  4. 2
      src/command/publish/access.rs
  5. 34
      src/command/test.rs
  6. 14
      src/lib.rs
  7. 6
      src/main.rs
  8. 2
      tests/all/main.rs

@ -30,7 +30,7 @@ serde_ignored = "0.1.7"
serde_json = "1.0.91" serde_json = "1.0.91"
siphasher = "0.3.10" siphasher = "0.3.10"
strsim = "0.10.0" strsim = "0.10.0"
structopt = "0.3.26" clap = { version = "4.2.5", features = ["derive"] }
toml = "0.5.11" toml = "0.5.11"
ureq = { version = "2.6.2", features = ["json"] } ureq = { version = "2.6.2", features = ["json"] }
walkdir = "2.3.2" walkdir = "2.3.2"

@ -19,7 +19,7 @@ use std::fmt;
use std::path::PathBuf; use std::path::PathBuf;
use std::str::FromStr; use std::str::FromStr;
use std::time::Instant; use std::time::Instant;
use structopt::clap::AppSettings; use clap::{Args};
/// Everything required to configure and run the `wasm-pack build` command. /// Everything required to configure and run the `wasm-pack build` command.
#[allow(missing_docs)] #[allow(missing_docs)]
@ -109,74 +109,67 @@ pub enum BuildProfile {
} }
/// Everything required to configure and run the `wasm-pack build` command. /// Everything required to configure and run the `wasm-pack build` command.
#[derive(Debug, StructOpt)] #[derive(Debug, Args)]
#[structopt( #[command(allow_hyphen_values = true, trailing_var_arg = true)]
// 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,
)]
pub struct BuildOptions { pub struct BuildOptions {
/// The path to the Rust crate. If not set, searches up the path from the current directory. /// The path to the Rust crate. If not set, searches up the path from the current directory.
#[structopt(parse(from_os_str))] #[clap()]
pub path: Option<PathBuf>, pub path: Option<PathBuf>,
/// The npm scope to use in package.json, if any. /// The npm scope to use in package.json, if any.
#[structopt(long = "scope", short = "s")] #[clap(long = "scope", short = 's')]
pub scope: Option<String>, pub scope: Option<String>,
#[structopt(long = "mode", short = "m", default_value = "normal")] #[clap(long = "mode", short = 'm', default_value = "normal")]
/// Sets steps to be run. [possible values: no-install, normal, force] /// Sets steps to be run. [possible values: no-install, normal, force]
pub mode: InstallMode, pub mode: InstallMode,
#[structopt(long = "no-typescript")] #[clap(long = "no-typescript")]
/// By default a *.d.ts file is generated for the generated JS file, but /// By default a *.d.ts file is generated for the generated JS file, but
/// this flag will disable generating this TypeScript file. /// this flag will disable generating this TypeScript file.
pub disable_dts: bool, pub disable_dts: bool,
#[structopt(long = "weak-refs")] #[clap(long = "weak-refs")]
/// Enable usage of the JS weak references proposal. /// Enable usage of the JS weak references proposal.
pub weak_refs: bool, pub weak_refs: bool,
#[structopt(long = "reference-types")] #[clap(long = "reference-types")]
/// Enable usage of WebAssembly reference types. /// Enable usage of WebAssembly reference types.
pub reference_types: bool, pub reference_types: bool,
#[structopt(long = "target", short = "t", default_value = "bundler")] #[clap(long = "target", short = 't', default_value = "bundler")]
/// Sets the target environment. [possible values: bundler, nodejs, web, no-modules] /// Sets the target environment. [possible values: bundler, nodejs, web, no-modules]
pub target: Target, pub target: Target,
#[structopt(long = "debug")] #[clap(long = "debug")]
/// Deprecated. Renamed to `--dev`. /// Deprecated. Renamed to `--dev`.
pub debug: bool, pub debug: bool,
#[structopt(long = "dev")] #[clap(long = "dev")]
/// Create a development build. Enable debug info, and disable /// Create a development build. Enable debug info, and disable
/// optimizations. /// optimizations.
pub dev: bool, pub dev: bool,
#[structopt(long = "release")] #[clap(long = "release")]
/// Create a release build. Enable optimizations and disable debug info. /// Create a release build. Enable optimizations and disable debug info.
pub release: bool, pub release: bool,
#[structopt(long = "profiling")] #[clap(long = "profiling")]
/// Create a profiling build. Enable optimizations and debug info. /// Create a profiling build. Enable optimizations and debug info.
pub profiling: bool, pub profiling: bool,
#[structopt(long = "out-dir", short = "d", default_value = "pkg")] #[clap(long = "out-dir", short = 'd', default_value = "pkg")]
/// Sets the output directory with a relative path. /// Sets the output directory with a relative path.
pub out_dir: String, pub out_dir: String,
#[structopt(long = "out-name")] #[clap(long = "out-name")]
/// Sets the output file names. Defaults to package name. /// Sets the output file names. Defaults to package name.
pub out_name: Option<String>, pub out_name: Option<String>,
#[structopt(long = "no-pack", alias = "no-package")] #[clap(long = "no-pack", alias = "no-package")]
/// Option to not generate a package.json /// Option to not generate a package.json
pub no_pack: bool, pub no_pack: bool,
#[structopt(allow_hyphen_values = true)]
/// List of extra options to pass to `cargo build` /// List of extra options to pass to `cargo build`
pub extra_options: Vec<String>, pub extra_options: Vec<String>,
} }
@ -225,7 +218,7 @@ impl Build {
(false, false, false) | (false, true, false) => BuildProfile::Release, (false, false, false) | (false, true, false) => BuildProfile::Release,
(true, false, false) => BuildProfile::Dev, (true, false, false) => BuildProfile::Dev,
(false, false, true) => BuildProfile::Profiling, (false, false, true) => BuildProfile::Profiling,
// Unfortunately, `structopt` doesn't expose clap's `conflicts_with` // Unfortunately, `clap` doesn't expose clap's `conflicts_with`
// functionality yet, so we have to implement it ourselves. // functionality yet, so we have to implement it ourselves.
_ => bail!("Can only supply one of the --dev, --release, or --profiling flags"), _ => bail!("Can only supply one of the --dev, --release, or --profiling flags"),
}; };

@ -20,64 +20,64 @@ use crate::install::InstallMode;
use anyhow::Result; use anyhow::Result;
use log::info; use log::info;
use std::path::PathBuf; use std::path::PathBuf;
use clap::Subcommand;
use clap::builder::ValueParser;
/// The various kinds of commands that `wasm-pack` can execute. /// The various kinds of commands that `wasm-pack` can execute.
#[derive(Debug, StructOpt)] #[derive(Debug, Subcommand)]
pub enum Command { pub enum Command {
/// 🏗 build your npm package! /// 🏗 build your npm package!
#[structopt(name = "build", alias = "init")] #[clap(name = "build", alias = "init")]
Build(BuildOptions), Build(BuildOptions),
#[structopt(name = "pack")] #[clap(name = "pack")]
/// 🍱 create a tar of your npm package but don't publish! /// 🍱 create a tar of your npm package but don't publish!
Pack { Pack {
/// The path to the Rust crate. If not set, searches up the path from the current directory. /// The path to the Rust crate. If not set, searches up the path from the current directory.
#[structopt(parse(from_os_str))] #[clap(value_parser = ValueParser::os_string())]
path: Option<PathBuf>, path: Option<PathBuf>,
}, },
#[structopt(name = "new")] #[clap(name = "new")]
/// 🐑 create a new project with a template /// 🐑 create a new project with a template
Generate { Generate {
/// The name of the project /// The name of the project
name: String, name: String,
/// The URL to the template /// The URL to the template
#[structopt( #[clap(
long = "template", long = "template",
short = "temp",
default_value = "https://github.com/rustwasm/wasm-pack-template" default_value = "https://github.com/rustwasm/wasm-pack-template"
)] )]
template: String, template: String,
#[structopt(long = "mode", short = "m", default_value = "normal")] #[clap(long = "mode", short = 'm', default_value = "normal")]
/// Should we install or check the presence of binary tools. [possible values: no-install, normal, force] /// Should we install or check the presence of binary tools. [possible values: no-install, normal, force]
mode: InstallMode, mode: InstallMode,
}, },
#[structopt(name = "publish")] #[clap(name = "publish")]
/// 🎆 pack up your npm package and publish! /// 🎆 pack up your npm package and publish!
Publish { Publish {
#[structopt(long = "target", short = "t", default_value = "bundler")] #[clap(long = "target", short = 't', default_value = "bundler")]
/// Sets the target environment. [possible values: bundler, nodejs, web, no-modules] /// Sets the target environment. [possible values: bundler, nodejs, web, no-modules]
target: String, target: String,
/// The access level for the package to be published /// The access level for the package to be published
#[structopt(long = "access", short = "a")] #[clap(long = "access", short = 'a')]
access: Option<Access>, access: Option<Access>,
/// The distribution tag being used for publishing. /// The distribution tag being used for publishing.
/// See https://docs.npmjs.com/cli/dist-tag /// See https://docs.npmjs.com/cli/dist-tag
#[structopt(long = "tag")] #[clap(long = "tag")]
tag: Option<String>, tag: Option<String>,
/// The path to the Rust crate. If not set, searches up the path from the current directory. /// The path to the Rust crate. If not set, searches up the path from the current directory.
#[structopt(parse(from_os_str))] #[clap(value_parser = ValueParser::os_string())]
path: Option<PathBuf>, path: Option<PathBuf>,
}, },
#[structopt(name = "login", alias = "adduser", alias = "add-user")] #[clap(name = "login", alias = "adduser", alias = "add-user")]
/// 👤 Add an npm registry user account! (aliases: adduser, add-user) /// 👤 Add an npm registry user account! (aliases: adduser, add-user)
Login { Login {
#[structopt(long = "registry", short = "r")] #[clap(long = "registry", short = 'r')]
/// Default: 'https://registry.npmjs.org/'. /// Default: 'https://registry.npmjs.org/'.
/// The base URL of the npm package registry. If scope is also /// The base URL of the npm package registry. If scope is also
/// specified, this registry will only be used for packages with that /// specified, this registry will only be used for packages with that
@ -85,13 +85,13 @@ pub enum Command {
/// currently in, if any. /// currently in, if any.
registry: Option<String>, registry: Option<String>,
#[structopt(long = "scope", short = "s")] #[clap(long = "scope", short = 's')]
/// Default: none. /// Default: none.
/// If specified, the user and login credentials given will be /// If specified, the user and login credentials given will be
/// associated with the specified scope. /// associated with the specified scope.
scope: Option<String>, scope: Option<String>,
#[structopt(long = "auth-type", short = "t")] #[clap(long = "auth-type", short = 't')]
/// Default: 'legacy'. /// Default: 'legacy'.
/// Type: 'legacy', 'sso', 'saml', 'oauth'. /// Type: 'legacy', 'sso', 'saml', 'oauth'.
/// What authentication strategy to use with adduser/login. Some npm /// What authentication strategy to use with adduser/login. Some npm
@ -100,7 +100,7 @@ pub enum Command {
auth_type: Option<String>, auth_type: Option<String>,
}, },
#[structopt(name = "test")] #[clap(name = "test")]
/// 👩🔬 test your wasm! /// 👩🔬 test your wasm!
Test(TestOptions), Test(TestOptions),
} }

@ -3,7 +3,7 @@ use std::fmt;
use std::str::FromStr; 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`. /// Represents access level for the to-be publish package. Passed to `wasm-pack publish` as a flag, e.g. `--access=public`.
#[derive(Debug)] #[derive(Clone, Debug)]
pub enum Access { pub enum Access {
/// Access is granted to all. All unscoped packages *must* be public. /// Access is granted to all. All unscoped packages *must* be public.
Public, Public,

@ -14,68 +14,63 @@ use log::info;
use std::path::PathBuf; use std::path::PathBuf;
use std::str::FromStr; use std::str::FromStr;
use std::time::Instant; use std::time::Instant;
use structopt::clap::AppSettings; use clap::Args;
use clap::builder::ValueParser;
#[derive(Debug, Default, StructOpt)] #[derive(Debug, Default, Args)]
#[structopt( #[command(allow_hyphen_values = true, trailing_var_arg = true)]
// 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,
)]
/// Everything required to configure the `wasm-pack test` command. /// Everything required to configure the `wasm-pack test` command.
pub struct TestOptions { pub struct TestOptions {
#[structopt(long = "node")] #[clap(long = "node")]
/// Run the tests in Node.js. /// Run the tests in Node.js.
pub node: bool, pub node: bool,
#[structopt(long = "firefox")] #[clap(long = "firefox")]
/// Run the tests in Firefox. This machine must have a Firefox installation. /// Run the tests in Firefox. This machine must have a Firefox installation.
/// If the `geckodriver` WebDriver client is not on the `$PATH`, and not /// If the `geckodriver` WebDriver client is not on the `$PATH`, and not
/// specified with `--geckodriver`, then `wasm-pack` will download a local /// specified with `--geckodriver`, then `wasm-pack` will download a local
/// copy. /// copy.
pub firefox: bool, pub firefox: bool,
#[structopt(long = "geckodriver", parse(from_os_str))] #[clap(long = "geckodriver", value_parser = ValueParser::os_string())]
/// The path to the `geckodriver` WebDriver client for testing in /// The path to the `geckodriver` WebDriver client for testing in
/// Firefox. Implies `--firefox`. /// Firefox. Implies `--firefox`.
pub geckodriver: Option<PathBuf>, pub geckodriver: Option<PathBuf>,
#[structopt(long = "chrome")] #[clap(long = "chrome")]
/// Run the tests in Chrome. This machine must have a Chrome installation. /// Run the tests in Chrome. This machine must have a Chrome installation.
/// If the `chromedriver` WebDriver client is not on the `$PATH`, and not /// If the `chromedriver` WebDriver client is not on the `$PATH`, and not
/// specified with `--chromedriver`, then `wasm-pack` will download a local /// specified with `--chromedriver`, then `wasm-pack` will download a local
/// copy. /// copy.
pub chrome: bool, pub chrome: bool,
#[structopt(long = "chromedriver", parse(from_os_str))] #[clap(long = "chromedriver", value_parser = ValueParser::os_string())]
/// The path to the `chromedriver` WebDriver client for testing in /// The path to the `chromedriver` WebDriver client for testing in
/// Chrome. Implies `--chrome`. /// Chrome. Implies `--chrome`.
pub chromedriver: Option<PathBuf>, pub chromedriver: Option<PathBuf>,
#[structopt(long = "safari")] #[clap(long = "safari")]
/// Run the tests in Safari. This machine must have a Safari installation, /// Run the tests in Safari. This machine must have a Safari installation,
/// and the `safaridriver` WebDriver client must either be on the `$PATH` or /// and the `safaridriver` WebDriver client must either be on the `$PATH` or
/// specified explicitly with the `--safaridriver` flag. `wasm-pack` cannot /// specified explicitly with the `--safaridriver` flag. `wasm-pack` cannot
/// download the `safaridriver` WebDriver client for you. /// download the `safaridriver` WebDriver client for you.
pub safari: bool, pub safari: bool,
#[structopt(long = "safaridriver", parse(from_os_str))] #[clap(long = "safaridriver", value_parser = ValueParser::os_string())]
/// The path to the `safaridriver` WebDriver client for testing in /// The path to the `safaridriver` WebDriver client for testing in
/// Safari. Implies `--safari`. /// Safari. Implies `--safari`.
pub safaridriver: Option<PathBuf>, pub safaridriver: Option<PathBuf>,
#[structopt(long = "headless")] #[clap(long = "headless")]
/// When running browser tests, run the browser in headless mode without any /// When running browser tests, run the browser in headless mode without any
/// UI or windows. /// UI or windows.
pub headless: bool, pub headless: bool,
#[structopt(long = "mode", short = "m", default_value = "normal")] #[clap(long = "mode", short = 'm', default_value = "normal")]
/// Sets steps to be run. [possible values: no-install, normal] /// Sets steps to be run. [possible values: no-install, normal]
pub mode: InstallMode, pub mode: InstallMode,
#[structopt(long = "release", short = "r")] #[clap(long = "release", short = 'r')]
/// Build with the release profile. /// Build with the release profile.
pub release: bool, pub release: bool,
@ -85,7 +80,6 @@ pub struct TestOptions {
/// ///
/// This is a workaround to allow wasm pack to provide the same command line interface as `cargo`. /// This is a workaround to allow wasm pack to provide the same command line interface as `cargo`.
/// See <https://github.com/rustwasm/wasm-pack/pull/851> for more information. /// See <https://github.com/rustwasm/wasm-pack/pull/851> for more information.
#[structopt(allow_hyphen_values = true)]
pub path_and_extra_options: Vec<String>, pub path_and_extra_options: Vec<String>,
} }

@ -15,8 +15,6 @@ extern crate which;
extern crate serde_derive; extern crate serde_derive;
extern crate serde_ignored; extern crate serde_ignored;
extern crate serde_json; extern crate serde_json;
#[macro_use]
extern crate structopt;
extern crate binary_install; extern crate binary_install;
extern crate chrono; extern crate chrono;
extern crate dialoguer; extern crate dialoguer;
@ -43,27 +41,29 @@ pub mod target;
pub mod test; pub mod test;
pub mod wasm_opt; pub mod wasm_opt;
use clap::Parser;
use clap::builder::ArgAction;
use crate::progressbar::{LogLevel, ProgressOutput}; use crate::progressbar::{LogLevel, ProgressOutput};
/// The global progress bar and user-facing message output. /// The global progress bar and user-facing message output.
pub static PBAR: ProgressOutput = ProgressOutput::new(); pub static PBAR: ProgressOutput = ProgressOutput::new();
/// 📦 ✨ pack and publish your wasm! /// 📦 ✨ pack and publish your wasm!
#[derive(Debug, StructOpt)] #[derive(Debug, Parser)]
pub struct Cli { pub struct Cli {
/// The subcommand to run. /// The subcommand to run.
#[structopt(subcommand)] // Note that we mark a field as a subcommand #[clap(subcommand)] // Note that we mark a field as a subcommand
pub cmd: command::Command, pub cmd: command::Command,
/// Log verbosity is based off the number of v used /// Log verbosity is based off the number of v used
#[structopt(long = "verbose", short = "v", parse(from_occurrences))] #[clap(long = "verbose", short = 'v', action = ArgAction::Count)]
pub verbosity: u8, pub verbosity: u8,
#[structopt(long = "quiet", short = "q")] #[clap(long = "quiet", short = 'q')]
/// No output printed to stdout /// No output printed to stdout
pub quiet: bool, pub quiet: bool,
#[structopt(long = "log-level", default_value = "info")] #[clap(long = "log-level", default_value = "info")]
/// The maximum level of messages that should be logged by wasm-pack. [possible values: info, warn, error] /// The maximum level of messages that should be logged by wasm-pack. [possible values: info, warn, error]
pub log_level: LogLevel, pub log_level: LogLevel,
} }

@ -5,7 +5,7 @@ extern crate atty;
extern crate env_logger; extern crate env_logger;
extern crate human_panic; extern crate human_panic;
extern crate log; extern crate log;
extern crate structopt; extern crate clap;
extern crate wasm_pack; extern crate wasm_pack;
extern crate which; extern crate which;
@ -14,7 +14,7 @@ use std::env;
use std::panic; use std::panic;
use std::sync::mpsc; use std::sync::mpsc;
use std::thread; use std::thread;
use structopt::StructOpt; use clap::Parser;
use wasm_pack::{ use wasm_pack::{
build::{self, WasmPackVersion}, build::{self, WasmPackVersion},
command::run_wasm_pack, command::run_wasm_pack,
@ -79,7 +79,7 @@ fn run() -> Result<()> {
} }
} }
let args = Cli::from_args(); let args = Cli::parse();
PBAR.set_log_level(args.log_level); PBAR.set_log_level(args.log_level);

@ -8,7 +8,7 @@ extern crate binary_install;
extern crate serde_json; extern crate serde_json;
#[macro_use] #[macro_use]
extern crate serial_test; extern crate serial_test;
extern crate structopt; extern crate clap;
extern crate tempfile; extern crate tempfile;
extern crate wasm_pack; extern crate wasm_pack;

Loading…
Cancel
Save