Skip to content

Commit a1d3b97

Browse files
authored
Move from structopt to clap version 3 (bytecodealliance#435)
* Move from `structopt` to `clap` version 3 A straightforward update! * More migration
1 parent f49ee18 commit a1d3b97

16 files changed

Lines changed: 106 additions & 106 deletions

File tree

Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ members = ['fuzz', 'crates/wasm-encoder', 'crates/fuzz-stats', 'crates/wasm-muta
1818
anyhow = "1.0"
1919
env_logger = "0.8"
2020
log = "0.4"
21-
structopt = "0.3.16"
21+
clap = { version = "3.0", features = ['derive'] }
2222
tempfile = "3.2.0"
2323
wat = { path = "crates/wat", optional = true }
2424

@@ -36,11 +36,11 @@ serde_json = { version = "1", optional = true }
3636
wasm-smith = { path = "crates/wasm-smith", features = ["_internal_cli"], optional = true }
3737

3838
# Dependencies of `shrink`
39-
wasm-shrink = { path = "crates/wasm-shrink", features = ["structopt"], optional = true}
39+
wasm-shrink = { path = "crates/wasm-shrink", features = ["clap"], optional = true}
4040
is_executable = { version = "1.0.1", optional = true }
4141

4242
# Dependencies of `mutate`
43-
wasm-mutate = { path = "crates/wasm-mutate", features = ["structopt"], optional = true }
43+
wasm-mutate = { path = "crates/wasm-mutate", features = ["clap"], optional = true }
4444

4545
# Dependencies of `dump`
4646
wasmparser-dump = { path = "crates/dump", optional = true }

crates/wasm-mutate-stats/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ wasmparser = { version = "0.81.0", path = "../wasmparser" }
1515
wasmtime = "0.32.0"
1616
env_logger = "0.8"
1717
itertools = "0.10.0"
18-
structopt = "0.3.16"
18+
clap = "3.0"
1919
log = "0.4"
2020

2121
[lib]

crates/wasm-mutate-stats/src/bin/wasm-mutate-stats.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use anyhow::Context;
2+
use clap::Parser;
23
use core::sync::atomic::Ordering::{Relaxed, SeqCst};
34
use rand::Rng;
45
use rand::{rngs::SmallRng, SeedableRng};
@@ -12,14 +13,14 @@ use std::sync::Mutex;
1213
use std::time::Duration;
1314
use std::{collections::HashMap, sync::Arc};
1415
use std::{panic, process};
15-
use structopt::StructOpt;
1616
use wasm_mutate::WasmMutate;
1717
use wasmtime::{Config, Engine, OptLevel};
1818

1919
#[derive(Debug)]
2020
enum ParsingOptLevelError {
2121
Parsing(String),
2222
}
23+
2324
impl Display for ParsingOptLevelError {
2425
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
2526
match self {
@@ -28,6 +29,8 @@ impl Display for ParsingOptLevelError {
2829
}
2930
}
3031

32+
impl std::error::Error for ParsingOptLevelError {}
33+
3134
/// Parses the list of optimizations to set in wasmtime
3235
fn parse_optimization_types(s: &str) -> Result<OptLevel, ParsingOptLevelError> {
3336
match s {
@@ -66,25 +69,25 @@ fn parse_optimization_types(s: &str) -> Result<OptLevel, ParsingOptLevelError> {
6669
/// checking preservation for O0(No optimizations) and O2(size and speed optimization), using architecture
6770
/// triple `x86_64-apple-darwin`.
6871
///
69-
#[derive(StructOpt)]
72+
#[derive(Parser)]
7073
struct Options {
7174
/// The input folder that contains the Wasm binaries.
7275
input: PathBuf,
7376
/// The timeout, 0 to wait for keyboard interrupt
7477
timeout: u64,
7578
/// The seed of the random mutation, 0 by default
76-
#[structopt(short = "s", long = "seed")]
79+
#[clap(short = 's', long = "seed")]
7780
seed: u64,
7881
/// List of engine configurations.
7982
/// Allowed values: [O0, O2, Os]
8083
/// If it is not set, the default configuration of wasmtime will be used
81-
#[structopt(short = "c", long = "compilation-configs", parse(try_from_str=parse_optimization_types) )]
84+
#[clap(short = 'c', long = "compilation-configs", parse(try_from_str=parse_optimization_types) )]
8285
configs: Option<Vec<OptLevel>>,
8386
/// Target triple during compilation, e.g. "x86_64-apple-darwin"
84-
#[structopt(short = "a", long = "triple")]
87+
#[clap(short = 'a', long = "triple")]
8588
triple: Option<String>,
8689
/// Only generate report, if this option is set, it will skip the generation
87-
#[structopt(short = "k", long = "skip")]
90+
#[clap(short = 'k', long = "skip")]
8891
skip_generation: bool,
8992
}
9093

@@ -107,7 +110,7 @@ fn main() -> anyhow::Result<()> {
107110
// Init logs
108111
env_logger::init();
109112

110-
let opts = Options::from_args();
113+
let opts = Options::parse();
111114
let timeout = opts.timeout;
112115
let seed = opts.seed;
113116

crates/wasm-mutate/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ repository = "https://github.com/bytecodealliance/wasm-tools"
77
description = "A WebAssembly test case mutator"
88

99
[dependencies]
10-
structopt = { optional = true, version = "0.3" }
10+
clap = { optional = true, version = "3.0", features = ['derive'] }
1111
thiserror = "1.0.28"
1212
wasmparser = { version = "0.81", path = "../wasmparser" }
1313
wasm-encoder = { version = "0.8.0", path = "../wasm-encoder"}

crates/wasm-mutate/src/lib.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
//! tool. `wasm-mutate` can serve as a custom mutator for mutation-based
88
//! fuzzing.
99
10-
#![cfg_attr(not(feature = "structopt"), deny(missing_docs))]
10+
#![cfg_attr(not(feature = "clap"), deny(missing_docs))]
1111

1212
mod error;
1313
mod info;
@@ -31,8 +31,8 @@ use mutators::Mutator;
3131
use rand::{rngs::SmallRng, Rng, SeedableRng};
3232
use std::{cell::Cell, sync::Arc};
3333

34-
#[cfg(feature = "structopt")]
35-
use structopt::StructOpt;
34+
#[cfg(feature = "clap")]
35+
use clap::Parser;
3636

3737
macro_rules! define_mutators {
3838
(@count) => {0};
@@ -104,7 +104,7 @@ macro_rules! define_mutators {
104104
// NB: only add this doc comment if we are not building the CLI, since otherwise
105105
// it will override the main CLI's about text.
106106
#[cfg_attr(
107-
not(feature = "structopt"),
107+
not(feature = "clap"),
108108
doc = r###"
109109
A WebAssembly test case mutator.
110110
@@ -149,36 +149,36 @@ for mutated_wasm in mutate.run(&input_wasm)? {
149149
```
150150
"###
151151
)]
152-
#[cfg_attr(feature = "structopt", derive(StructOpt))]
152+
#[cfg_attr(feature = "clap", derive(Parser))]
153153
#[derive(Clone)]
154154
pub struct WasmMutate<'wasm> {
155155
/// The RNG seed used to choose which transformation to apply. Given the
156156
/// same input Wasm and same seed, `wasm-mutate` will always generate the
157157
/// same output Wasm.
158-
#[cfg_attr(feature = "structopt", structopt(short, long))]
158+
#[cfg_attr(feature = "clap", clap(short, long))]
159159
seed: u64,
160160

161161
/// Only perform semantics-preserving transformations on the Wasm module.
162-
#[cfg_attr(feature = "structopt", structopt(long))]
162+
#[cfg_attr(feature = "clap", clap(long))]
163163
preserve_semantics: bool,
164164

165165
/// Fuel to control the time of the mutation.
166-
#[cfg_attr(feature = "structopt", structopt(skip = Cell::new(u64::MAX)))]
166+
#[cfg_attr(feature = "clap", clap(skip = Cell::new(u64::MAX)))]
167167
fuel: Cell<u64>,
168168
/// Only perform size-reducing transformations on the Wasm module. This
169169
/// allows `wasm-mutate` to be used as a test case reducer.
170-
#[cfg_attr(feature = "structopt", structopt(long))]
170+
#[cfg_attr(feature = "clap", clap(long))]
171171
reduce: bool,
172172

173173
// Note: this is only exposed via the programmatic interface, not via the
174174
// CLI.
175-
#[cfg_attr(feature = "structopt", structopt(skip = None))]
175+
#[cfg_attr(feature = "clap", clap(skip = None))]
176176
raw_mutate_func: Option<Arc<dyn Fn(&mut Vec<u8>) -> Result<()>>>,
177177

178-
#[cfg_attr(feature = "structopt", structopt(skip = None))]
178+
#[cfg_attr(feature = "clap", clap(skip = None))]
179179
rng: Option<SmallRng>,
180180

181-
#[cfg_attr(feature = "structopt", structopt(skip = None))]
181+
#[cfg_attr(feature = "clap", clap(skip = None))]
182182
info: Option<ModuleInfo<'wasm>>,
183183
}
184184

crates/wasm-shrink/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ anyhow = "1"
1515
blake3 = "1.2.0"
1616
log = "0.4"
1717
rand = { version = "0.8.4", features = ["small_rng"] }
18-
structopt = { version = "0.3.25", optional = true }
18+
clap = { version = "3.0", optional = true, features = ['derive'] }
1919
wasm-mutate = { version = "0.1.0", path = "../wasm-mutate" }
2020
wasmparser = { version = "0.81", path = "../wasmparser" }
2121

crates/wasm-shrink/src/lib.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ static EMPTY_WASM: &'static [u8] = &[
1818
];
1919

2020
#[cfg_attr(
21-
not(feature = "structopt"),
21+
not(feature = "clap"),
2222
doc = r###"
2323
Shrink a Wasm file while maintaining a property of interest (such as
2424
triggering a compiler bug).
@@ -63,22 +63,22 @@ let shrunken_wasm = info.output;
6363
```
6464
"###
6565
)]
66-
#[cfg_attr(feature = "structopt", derive(structopt::StructOpt))]
66+
#[cfg_attr(feature = "clap", derive(clap::Parser))]
6767
pub struct WasmShrink {
6868
/// The number of shrink attempts to try before considering a Wasm module as
6969
/// small as it will ever get.
70-
#[cfg_attr(feature = "structopt", structopt(short, long, default_value = "1000"))]
70+
#[cfg_attr(feature = "clap", clap(short, long, default_value = "1000"))]
7171
attempts: u32,
7272

7373
/// Allow shrinking the input down to an empty Wasm module.
7474
///
7575
/// This is usually not desired and typically reflects a bug in the
7676
/// predicate script.
77-
#[cfg_attr(feature = "structopt", structopt(long))]
77+
#[cfg_attr(feature = "clap", clap(long))]
7878
allow_empty: bool,
7979

8080
/// The RNG seed for choosing which size-reducing mutation to attempt next.
81-
#[cfg_attr(feature = "structopt", structopt(short, long, default_value = "42"))]
81+
#[cfg_attr(feature = "clap", clap(short, long, default_value = "42"))]
8282
seed: u64,
8383
}
8484

src/bin/wasm-tools/dump.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
use anyhow::Result;
22
use std::path::PathBuf;
3-
use structopt::StructOpt;
43

54
/// Debugging utility to dump information about a wasm binary.
65
///
76
/// This can be useful to figure out precisely how each byte of a wasm binary is
87
/// classified or where particular constructs are at particular offsets.
9-
#[derive(StructOpt)]
8+
#[derive(clap::Parser)]
109
pub struct Opts {
1110
/// Input WebAssembly file to dump information about.
1211
input: PathBuf,

src/bin/wasm-tools/main.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use anyhow::Result;
2-
use structopt::StructOpt;
2+
use clap::Parser;
33

44
macro_rules! subcommands {
55
($(($name:ident, $string:tt))*) => {
@@ -8,13 +8,13 @@ macro_rules! subcommands {
88
mod $name;
99
)*
1010

11-
#[derive(StructOpt)]
12-
#[allow(non_camel_case_types)]
11+
#[derive(Parser)]
12+
#[allow(non_camel_case_types)]
1313
enum WasmTools {
1414
$(
1515
#[cfg(feature = $string)]
1616
$name {
17-
#[structopt(flatten)]
17+
#[clap(flatten)]
1818
opts: $name::Opts,
1919
},
2020
)*
@@ -46,5 +46,5 @@ subcommands! {
4646

4747
fn main() -> Result<()> {
4848
env_logger::init();
49-
WasmTools::from_args().run()
49+
<WasmTools as Parser>::parse().run()
5050
}

src/bin/wasm-tools/mutate.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use anyhow::{Context, Result};
2+
use clap::Parser;
23
use std::fs;
34
use std::io::{stdin, stdout, Read, Write};
45
use std::path::PathBuf;
5-
use structopt::StructOpt;
66
use wasm_mutate::ErrorKind;
77

88
/// A WebAssembly test case mutator.
@@ -38,22 +38,22 @@ use wasm_mutate::ErrorKind;
3838
/// supported by `wasm-mutate`.
3939
///
4040
/// * 6: Other error.
41-
#[derive(StructOpt)]
41+
#[derive(Parser)]
4242
pub struct Opts {
4343
/// The input WebAssembly binary that will be mutated.
4444
///
4545
/// `stdin` is used if this argument is not supplied.
46-
#[structopt(parse(from_os_str))]
46+
#[clap(parse(from_os_str))]
4747
input: Option<PathBuf>,
4848

4949
/// The output file path, where the new, mutated WebAssembly module is
5050
/// placed.
5151
///
5252
/// `stdout` is used if this argument is not supplied.
53-
#[structopt(short, long, parse(from_os_str))]
53+
#[clap(short, long, parse(from_os_str))]
5454
output: Option<PathBuf>,
5555

56-
#[structopt(flatten)]
56+
#[clap(flatten)]
5757
wasm_mutate: wasm_mutate::WasmMutate<'static>,
5858
}
5959

0 commit comments

Comments
 (0)