Skip to content

Commit

Permalink
Migrate to clap from structopt
Browse files Browse the repository at this point in the history
  • Loading branch information
silwol committed May 17, 2022
1 parent 435cfa2 commit ed4fb52
Show file tree
Hide file tree
Showing 16 changed files with 201 additions and 123 deletions.
74 changes: 66 additions & 8 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions lib/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ wasmer-vfs = { version = "=2.2.1", path = "../vfs", default-features = false, f
atty = "0.2"
colored = "2.0"
anyhow = "1.0"
structopt = { version = "0.3", features = ["suggestions"] }
clap = { version = "3.1.18", features = ["derive"] }
# For the function names autosuggestion
distance = "0.4"
# For the inspect subcommand
Expand All @@ -54,6 +54,7 @@ cfg-if = "1.0"
fern = { version = "0.6", features = ["colored"], optional = true }
log = { version = "0.4", optional = true }
tempfile = "3"
target-lexicon = { version = "0.12.2", features = ["std"] }

[target.'cfg(target_os = "linux")'.dependencies]
unix_mode = "0.1.3"
Expand Down Expand Up @@ -119,4 +120,4 @@ headless-minimal = ["headless", "disable-all-logging", "wasi", "dylib", "univers

# Deprecated features.
jit = ["universal"]
native = ["dylib"]
native = ["dylib"]
39 changes: 20 additions & 19 deletions lib/cli/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@ use crate::commands::{Cache, Config, Inspect, Run, SelfUpdate, Validate};
use crate::error::PrettyError;
use anyhow::Result;

use structopt::{clap::ErrorKind, StructOpt};
use clap::{ErrorKind, Parser};

#[derive(StructOpt)]
#[derive(Parser)]
#[cfg_attr(
not(feature = "headless"),
structopt(name = "wasmer", about = "WebAssembly standalone runtime.", author)
clap(name = "wasmer", about = "WebAssembly standalone runtime.", author)
)]
#[cfg_attr(
feature = "headless",
structopt(
clap(
name = "wasmer-headless",
about = "Headless WebAssembly standalone runtime.",
author
Expand All @@ -30,48 +30,49 @@ use structopt::{clap::ErrorKind, StructOpt};
/// The options for the wasmer Command Line Interface
enum WasmerCLIOptions {
/// Run a WebAssembly file. Formats accepted: wasm, wat
#[structopt(name = "run")]
#[clap(name = "run")]
Run(Run),

// TODO: check whether this should really be a subcommand.
/// Wasmer cache
#[structopt(name = "cache")]
#[clap(subcommand, name = "cache")]
Cache(Cache),

/// Validate a WebAssembly binary
#[structopt(name = "validate")]
#[clap(name = "validate")]
Validate(Validate),

/// Compile a WebAssembly binary
#[cfg(feature = "compiler")]
#[structopt(name = "compile")]
#[clap(name = "compile")]
Compile(Compile),

/// Compile a WebAssembly binary into a native executable
#[cfg(all(feature = "staticlib", feature = "compiler"))]
#[structopt(name = "create-exe")]
#[clap(name = "create-exe")]
CreateExe(CreateExe),

/// Get various configuration information needed
/// to compile programs which use Wasmer
#[structopt(name = "config")]
#[clap(name = "config")]
Config(Config),

/// Update wasmer to the latest version
#[structopt(name = "self-update")]
#[clap(name = "self-update")]
SelfUpdate(SelfUpdate),

/// Inspect a WebAssembly file
#[structopt(name = "inspect")]
#[clap(name = "inspect")]
Inspect(Inspect),

/// Run spec testsuite
#[cfg(feature = "wast")]
#[structopt(name = "wast")]
#[clap(name = "wast")]
Wast(Wast),

/// Unregister and/or register wasmer as binfmt interpreter
#[cfg(target_os = "linux")]
#[structopt(name = "binfmt")]
#[clap(name = "binfmt")]
Binfmt(Binfmt),
}

Expand Down Expand Up @@ -117,15 +118,15 @@ pub fn wasmer_main() {
} else {
match command.unwrap_or(&"".to_string()).as_ref() {
"cache" | "compile" | "config" | "create-exe" | "help" | "inspect" | "run"
| "self-update" | "validate" | "wast" | "binfmt" => WasmerCLIOptions::from_args(),
| "self-update" | "validate" | "wast" | "binfmt" => WasmerCLIOptions::parse(),
_ => {
WasmerCLIOptions::from_iter_safe(args.iter()).unwrap_or_else(|e| {
match e.kind {
WasmerCLIOptions::try_parse_from(args.iter()).unwrap_or_else(|e| {
match e.kind() {
// This fixes a issue that:
// 1. Shows the version twice when doing `wasmer -V`
// 2. Shows the run help (instead of normal help) when doing `wasmer --help`
ErrorKind::VersionDisplayed | ErrorKind::HelpDisplayed => e.exit(),
_ => WasmerCLIOptions::Run(Run::from_args()),
ErrorKind::DisplayVersion | ErrorKind::DisplayHelp => e.exit(),
_ => WasmerCLIOptions::Run(Run::parse()),
}
})
}
Expand Down
10 changes: 5 additions & 5 deletions lib/cli/src/commands/binfmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ use std::io::Write;
use std::os::unix::ffi::OsStrExt;
use std::os::unix::fs::MetadataExt;
use std::path::{Path, PathBuf};
use structopt::StructOpt;
use clap::Parser;
use Action::*;

#[derive(StructOpt, Clone, Copy)]
#[derive(Parser, Clone, Copy)]
enum Action {
/// Register wasmer as binfmt interpreter
Register,
Expand All @@ -22,14 +22,14 @@ enum Action {
///
/// Check the wasmer repository for a systemd service definition example
/// to automate the process at start-up.
#[derive(StructOpt)]
#[derive(Parser)]
pub struct Binfmt {
// Might be better to traverse the mount list
/// Mount point of binfmt_misc fs
#[structopt(long, default_value = "/proc/sys/fs/binfmt_misc/")]
#[clap(long, default_value = "/proc/sys/fs/binfmt_misc/")]
binfmt_misc: PathBuf,

#[structopt(subcommand)]
#[clap(subcommand)]
action: Action,
}

Expand Down
8 changes: 4 additions & 4 deletions lib/cli/src/commands/cache.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
use crate::common::get_cache_dir;
use anyhow::{Context, Result};
use std::fs;
use structopt::StructOpt;
use clap::Parser;

#[derive(Debug, StructOpt)]
#[derive(Debug, Parser)]
/// The options for the `wasmer cache` subcommand
pub enum Cache {
/// Clear the cache
#[structopt(name = "clean")]
#[clap(name = "clean")]
Clean,

/// Display the location of the cache
#[structopt(name = "dir")]
#[clap(name = "dir")]
Dir,
}

Expand Down
19 changes: 11 additions & 8 deletions lib/cli/src/commands/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,35 @@ use crate::store::{EngineType, StoreOptions};
use crate::warning;
use anyhow::{Context, Result};
use std::path::PathBuf;
use structopt::StructOpt;
use clap::Parser;
use wasmer::*;

#[derive(Debug, StructOpt)]
#[derive(Debug, Parser)]
/// The options for the `wasmer compile` subcommand
pub struct Compile {
/// Input file
#[structopt(name = "FILE", parse(from_os_str))]
#[clap(name = "FILE", parse(from_os_str))]
path: PathBuf,

/// Output file
#[structopt(name = "OUTPUT PATH", short = "o", parse(from_os_str))]
#[clap(name = "OUTPUT PATH", short = 'o', parse(from_os_str))]
output: PathBuf,

/// Output path for generated header file
#[structopt(name = "HEADER PATH", long = "header", parse(from_os_str))]
#[clap(name = "HEADER PATH", long = "header", parse(from_os_str))]
header_path: Option<PathBuf>,

/// Compilation Target triple
#[structopt(long = "target")]
#[clap(long = "target")]
target_triple: Option<Triple>,

#[structopt(flatten)]
#[clap(flatten)]
store: StoreOptions,

#[structopt(short = "m", multiple = true, number_of_values = 1)]
// TODO: multiple_values or multiple_occurrences, or both?
// TODO: number_of_values = 1 required? need to read some more documentation
// before I can be sure
#[clap(short = 'm', multiple_occurrences = true, number_of_values = 1)]
cpu_features: Vec<CpuFeature>,
}

Expand Down
Loading

0 comments on commit ed4fb52

Please sign in to comment.