Skip to content

Commit 73e059f

Browse files
committed
update to clap 4 from structopt
1 parent 372733b commit 73e059f

File tree

8 files changed

+64
-77
lines changed

8 files changed

+64
-77
lines changed

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ serde_ignored = "0.1.7"
3030
serde_json = "1.0.91"
3131
siphasher = "0.3.10"
3232
strsim = "0.10.0"
33-
structopt = "0.3.26"
33+
clap = { version = "4.2.5", features = ["derive"] }
3434
toml = "0.5.11"
3535
ureq = { version = "2.6.2", features = ["json"] }
3636
walkdir = "2.3.2"

src/command/build.rs

+18-25
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use std::fmt;
1919
use std::path::PathBuf;
2020
use std::str::FromStr;
2121
use std::time::Instant;
22-
use structopt::clap::AppSettings;
22+
use clap::{Args};
2323

2424
/// Everything required to configure and run the `wasm-pack build` command.
2525
#[allow(missing_docs)]
@@ -109,74 +109,67 @@ pub enum BuildProfile {
109109
}
110110

111111
/// Everything required to configure and run the `wasm-pack build` command.
112-
#[derive(Debug, StructOpt)]
113-
#[structopt(
114-
// Allows unknown `--option`s to be parsed as positional arguments, so we can forward it to `cargo`.
115-
setting = AppSettings::AllowLeadingHyphen,
116-
117-
// Allows `--` to be parsed as an argument, so we can forward it to `cargo`.
118-
setting = AppSettings::TrailingVarArg,
119-
)]
112+
#[derive(Debug, Args)]
113+
#[command(allow_hyphen_values = true, trailing_var_arg = true)]
120114
pub struct BuildOptions {
121115
/// The path to the Rust crate. If not set, searches up the path from the current directory.
122-
#[structopt(parse(from_os_str))]
116+
#[clap()]
123117
pub path: Option<PathBuf>,
124118

125119
/// The npm scope to use in package.json, if any.
126-
#[structopt(long = "scope", short = "s")]
120+
#[clap(long = "scope", short = 's')]
127121
pub scope: Option<String>,
128122

129-
#[structopt(long = "mode", short = "m", default_value = "normal")]
123+
#[clap(long = "mode", short = 'm', default_value = "normal")]
130124
/// Sets steps to be run. [possible values: no-install, normal, force]
131125
pub mode: InstallMode,
132126

133-
#[structopt(long = "no-typescript")]
127+
#[clap(long = "no-typescript")]
134128
/// By default a *.d.ts file is generated for the generated JS file, but
135129
/// this flag will disable generating this TypeScript file.
136130
pub disable_dts: bool,
137131

138-
#[structopt(long = "weak-refs")]
132+
#[clap(long = "weak-refs")]
139133
/// Enable usage of the JS weak references proposal.
140134
pub weak_refs: bool,
141135

142-
#[structopt(long = "reference-types")]
136+
#[clap(long = "reference-types")]
143137
/// Enable usage of WebAssembly reference types.
144138
pub reference_types: bool,
145139

146-
#[structopt(long = "target", short = "t", default_value = "bundler")]
140+
#[clap(long = "target", short = 't', default_value = "bundler")]
147141
/// Sets the target environment. [possible values: bundler, nodejs, web, no-modules]
148142
pub target: Target,
149143

150-
#[structopt(long = "debug")]
144+
#[clap(long = "debug")]
151145
/// Deprecated. Renamed to `--dev`.
152146
pub debug: bool,
153147

154-
#[structopt(long = "dev")]
148+
#[clap(long = "dev")]
155149
/// Create a development build. Enable debug info, and disable
156150
/// optimizations.
157151
pub dev: bool,
158152

159-
#[structopt(long = "release")]
153+
#[clap(long = "release")]
160154
/// Create a release build. Enable optimizations and disable debug info.
161155
pub release: bool,
162156

163-
#[structopt(long = "profiling")]
157+
#[clap(long = "profiling")]
164158
/// Create a profiling build. Enable optimizations and debug info.
165159
pub profiling: bool,
166160

167-
#[structopt(long = "out-dir", short = "d", default_value = "pkg")]
161+
#[clap(long = "out-dir", short = 'd', default_value = "pkg")]
168162
/// Sets the output directory with a relative path.
169163
pub out_dir: String,
170164

171-
#[structopt(long = "out-name")]
165+
#[clap(long = "out-name")]
172166
/// Sets the output file names. Defaults to package name.
173167
pub out_name: Option<String>,
174168

175-
#[structopt(long = "no-pack", alias = "no-package")]
169+
#[clap(long = "no-pack", alias = "no-package")]
176170
/// Option to not generate a package.json
177171
pub no_pack: bool,
178172

179-
#[structopt(allow_hyphen_values = true)]
180173
/// List of extra options to pass to `cargo build`
181174
pub extra_options: Vec<String>,
182175
}
@@ -225,7 +218,7 @@ impl Build {
225218
(false, false, false) | (false, true, false) => BuildProfile::Release,
226219
(true, false, false) => BuildProfile::Dev,
227220
(false, false, true) => BuildProfile::Profiling,
228-
// Unfortunately, `structopt` doesn't expose clap's `conflicts_with`
221+
// Unfortunately, `clap` doesn't expose clap's `conflicts_with`
229222
// functionality yet, so we have to implement it ourselves.
230223
_ => bail!("Can only supply one of the --dev, --release, or --profiling flags"),
231224
};

src/command/mod.rs

+19-19
Original file line numberDiff line numberDiff line change
@@ -20,78 +20,78 @@ use crate::install::InstallMode;
2020
use anyhow::Result;
2121
use log::info;
2222
use std::path::PathBuf;
23-
23+
use clap::Subcommand;
24+
use clap::builder::ValueParser;
2425
/// The various kinds of commands that `wasm-pack` can execute.
25-
#[derive(Debug, StructOpt)]
26+
#[derive(Debug, Subcommand)]
2627
pub enum Command {
2728
/// 🏗️ build your npm package!
28-
#[structopt(name = "build", alias = "init")]
29+
#[clap(name = "build", alias = "init")]
2930
Build(BuildOptions),
3031

31-
#[structopt(name = "pack")]
32+
#[clap(name = "pack")]
3233
/// 🍱 create a tar of your npm package but don't publish!
3334
Pack {
3435
/// The path to the Rust crate. If not set, searches up the path from the current directory.
35-
#[structopt(parse(from_os_str))]
36+
#[clap(value_parser = ValueParser::os_string())]
3637
path: Option<PathBuf>,
3738
},
3839

39-
#[structopt(name = "new")]
40+
#[clap(name = "new")]
4041
/// 🐑 create a new project with a template
4142
Generate {
4243
/// The name of the project
4344
name: String,
4445
/// The URL to the template
45-
#[structopt(
46+
#[clap(
4647
long = "template",
47-
short = "temp",
4848
default_value = "https://github.com/rustwasm/wasm-pack-template"
4949
)]
5050
template: String,
51-
#[structopt(long = "mode", short = "m", default_value = "normal")]
51+
#[clap(long = "mode", short = 'm', default_value = "normal")]
5252
/// Should we install or check the presence of binary tools. [possible values: no-install, normal, force]
5353
mode: InstallMode,
5454
},
5555

56-
#[structopt(name = "publish")]
56+
#[clap(name = "publish")]
5757
/// 🎆 pack up your npm package and publish!
5858
Publish {
59-
#[structopt(long = "target", short = "t", default_value = "bundler")]
59+
#[clap(long = "target", short = 't', default_value = "bundler")]
6060
/// Sets the target environment. [possible values: bundler, nodejs, web, no-modules]
6161
target: String,
6262

6363
/// The access level for the package to be published
64-
#[structopt(long = "access", short = "a")]
64+
#[clap(long = "access", short = 'a')]
6565
access: Option<Access>,
6666

6767
/// The distribution tag being used for publishing.
6868
/// See https://docs.npmjs.com/cli/dist-tag
69-
#[structopt(long = "tag")]
69+
#[clap(long = "tag")]
7070
tag: Option<String>,
7171

7272
/// The path to the Rust crate. If not set, searches up the path from the current directory.
73-
#[structopt(parse(from_os_str))]
73+
#[clap(value_parser = ValueParser::os_string())]
7474
path: Option<PathBuf>,
7575
},
7676

77-
#[structopt(name = "login", alias = "adduser", alias = "add-user")]
77+
#[clap(name = "login", alias = "adduser", alias = "add-user")]
7878
/// 👤 Add an npm registry user account! (aliases: adduser, add-user)
7979
Login {
80-
#[structopt(long = "registry", short = "r")]
80+
#[clap(long = "registry", short = 'r')]
8181
/// Default: 'https://registry.npmjs.org/'.
8282
/// The base URL of the npm package registry. If scope is also
8383
/// specified, this registry will only be used for packages with that
8484
/// scope. scope defaults to the scope of the project directory you're
8585
/// currently in, if any.
8686
registry: Option<String>,
8787

88-
#[structopt(long = "scope", short = "s")]
88+
#[clap(long = "scope", short = 's')]
8989
/// Default: none.
9090
/// If specified, the user and login credentials given will be
9191
/// associated with the specified scope.
9292
scope: Option<String>,
9393

94-
#[structopt(long = "auth-type", short = "t")]
94+
#[clap(long = "auth-type", short = 't')]
9595
/// Default: 'legacy'.
9696
/// Type: 'legacy', 'sso', 'saml', 'oauth'.
9797
/// What authentication strategy to use with adduser/login. Some npm
@@ -100,7 +100,7 @@ pub enum Command {
100100
auth_type: Option<String>,
101101
},
102102

103-
#[structopt(name = "test")]
103+
#[clap(name = "test")]
104104
/// 👩‍🔬 test your wasm!
105105
Test(TestOptions),
106106
}

src/command/publish/access.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::fmt;
33
use std::str::FromStr;
44

55
/// Represents access level for the to-be publish package. Passed to `wasm-pack publish` as a flag, e.g. `--access=public`.
6-
#[derive(Debug)]
6+
#[derive(Clone, Debug)]
77
pub enum Access {
88
/// Access is granted to all. All unscoped packages *must* be public.
99
Public,

src/command/test.rs

+14-20
Original file line numberDiff line numberDiff line change
@@ -14,68 +14,63 @@ use log::info;
1414
use std::path::PathBuf;
1515
use std::str::FromStr;
1616
use std::time::Instant;
17-
use structopt::clap::AppSettings;
17+
use clap::Args;
18+
use clap::builder::ValueParser;
1819

19-
#[derive(Debug, Default, StructOpt)]
20-
#[structopt(
21-
// Allows unknown `--option`s to be parsed as positional arguments, so we can forward it to `cargo`.
22-
setting = AppSettings::AllowLeadingHyphen,
23-
24-
// Allows `--` to be parsed as an argument, so we can forward it to `cargo`.
25-
setting = AppSettings::TrailingVarArg,
26-
)]
20+
#[derive(Debug, Default, Args)]
21+
#[command(allow_hyphen_values = true, trailing_var_arg = true)]
2722
/// Everything required to configure the `wasm-pack test` command.
2823
pub struct TestOptions {
29-
#[structopt(long = "node")]
24+
#[clap(long = "node")]
3025
/// Run the tests in Node.js.
3126
pub node: bool,
3227

33-
#[structopt(long = "firefox")]
28+
#[clap(long = "firefox")]
3429
/// Run the tests in Firefox. This machine must have a Firefox installation.
3530
/// If the `geckodriver` WebDriver client is not on the `$PATH`, and not
3631
/// specified with `--geckodriver`, then `wasm-pack` will download a local
3732
/// copy.
3833
pub firefox: bool,
3934

40-
#[structopt(long = "geckodriver", parse(from_os_str))]
35+
#[clap(long = "geckodriver", value_parser = ValueParser::os_string())]
4136
/// The path to the `geckodriver` WebDriver client for testing in
4237
/// Firefox. Implies `--firefox`.
4338
pub geckodriver: Option<PathBuf>,
4439

45-
#[structopt(long = "chrome")]
40+
#[clap(long = "chrome")]
4641
/// Run the tests in Chrome. This machine must have a Chrome installation.
4742
/// If the `chromedriver` WebDriver client is not on the `$PATH`, and not
4843
/// specified with `--chromedriver`, then `wasm-pack` will download a local
4944
/// copy.
5045
pub chrome: bool,
5146

52-
#[structopt(long = "chromedriver", parse(from_os_str))]
47+
#[clap(long = "chromedriver", value_parser = ValueParser::os_string())]
5348
/// The path to the `chromedriver` WebDriver client for testing in
5449
/// Chrome. Implies `--chrome`.
5550
pub chromedriver: Option<PathBuf>,
5651

57-
#[structopt(long = "safari")]
52+
#[clap(long = "safari")]
5853
/// Run the tests in Safari. This machine must have a Safari installation,
5954
/// and the `safaridriver` WebDriver client must either be on the `$PATH` or
6055
/// specified explicitly with the `--safaridriver` flag. `wasm-pack` cannot
6156
/// download the `safaridriver` WebDriver client for you.
6257
pub safari: bool,
6358

64-
#[structopt(long = "safaridriver", parse(from_os_str))]
59+
#[clap(long = "safaridriver", value_parser = ValueParser::os_string())]
6560
/// The path to the `safaridriver` WebDriver client for testing in
6661
/// Safari. Implies `--safari`.
6762
pub safaridriver: Option<PathBuf>,
6863

69-
#[structopt(long = "headless")]
64+
#[clap(long = "headless")]
7065
/// When running browser tests, run the browser in headless mode without any
7166
/// UI or windows.
7267
pub headless: bool,
7368

74-
#[structopt(long = "mode", short = "m", default_value = "normal")]
69+
#[clap(long = "mode", short = 'm', default_value = "normal")]
7570
/// Sets steps to be run. [possible values: no-install, normal]
7671
pub mode: InstallMode,
7772

78-
#[structopt(long = "release", short = "r")]
73+
#[clap(long = "release", short = 'r')]
7974
/// Build with the release profile.
8075
pub release: bool,
8176

@@ -85,7 +80,6 @@ pub struct TestOptions {
8580
///
8681
/// This is a workaround to allow wasm pack to provide the same command line interface as `cargo`.
8782
/// See <https://github.com/rustwasm/wasm-pack/pull/851> for more information.
88-
#[structopt(allow_hyphen_values = true)]
8983
pub path_and_extra_options: Vec<String>,
9084
}
9185

src/lib.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ extern crate which;
1515
extern crate serde_derive;
1616
extern crate serde_ignored;
1717
extern crate serde_json;
18-
#[macro_use]
19-
extern crate structopt;
2018
extern crate binary_install;
2119
extern crate chrono;
2220
extern crate dialoguer;
@@ -43,27 +41,29 @@ pub mod target;
4341
pub mod test;
4442
pub mod wasm_opt;
4543

44+
use clap::Parser;
45+
use clap::builder::ArgAction;
4646
use crate::progressbar::{LogLevel, ProgressOutput};
4747

4848
/// The global progress bar and user-facing message output.
4949
pub static PBAR: ProgressOutput = ProgressOutput::new();
5050

5151
/// 📦 ✨ pack and publish your wasm!
52-
#[derive(Debug, StructOpt)]
52+
#[derive(Debug, Parser)]
5353
pub struct Cli {
5454
/// The subcommand to run.
55-
#[structopt(subcommand)] // Note that we mark a field as a subcommand
55+
#[clap(subcommand)] // Note that we mark a field as a subcommand
5656
pub cmd: command::Command,
5757

5858
/// Log verbosity is based off the number of v used
59-
#[structopt(long = "verbose", short = "v", parse(from_occurrences))]
59+
#[clap(long = "verbose", short = 'v', action = ArgAction::Count)]
6060
pub verbosity: u8,
6161

62-
#[structopt(long = "quiet", short = "q")]
62+
#[clap(long = "quiet", short = 'q')]
6363
/// No output printed to stdout
6464
pub quiet: bool,
6565

66-
#[structopt(long = "log-level", default_value = "info")]
66+
#[clap(long = "log-level", default_value = "info")]
6767
/// The maximum level of messages that should be logged by wasm-pack. [possible values: info, warn, error]
6868
pub log_level: LogLevel,
6969
}

src/main.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ extern crate atty;
55
extern crate env_logger;
66
extern crate human_panic;
77
extern crate log;
8-
extern crate structopt;
8+
extern crate clap;
99
extern crate wasm_pack;
1010
extern crate which;
1111

@@ -14,7 +14,7 @@ use std::env;
1414
use std::panic;
1515
use std::sync::mpsc;
1616
use std::thread;
17-
use structopt::StructOpt;
17+
use clap::Parser;
1818
use wasm_pack::{
1919
build::{self, WasmPackVersion},
2020
command::run_wasm_pack,
@@ -79,7 +79,7 @@ fn run() -> Result<()> {
7979
}
8080
}
8181

82-
let args = Cli::from_args();
82+
let args = Cli::parse();
8383

8484
PBAR.set_log_level(args.log_level);
8585

0 commit comments

Comments
 (0)