Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Migrate wasmer-cli to clap from structopt #2890

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 69 additions & 29 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion lib/cli-compiler/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ wasmer-types = { version = "=2.3.0", path = "../types" }
atty = "0.2"
colored = "2.0"
anyhow = "1.0"
structopt = { version = "0.3", features = ["suggestions"] }
clap = { version = "3.1", features = ["derive"] }
# For the function names autosuggestion
distance = "0.4"
# For the inspect subcommand
Expand Down
22 changes: 11 additions & 11 deletions lib/cli-compiler/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,27 @@ use crate::commands::{Config, Validate};
use crate::error::PrettyError;
use anyhow::Result;

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

#[derive(StructOpt)]
#[structopt(
#[derive(Parser)]
#[clap(
name = "wasmer-compiler",
about = "WebAssembly standalone Compiler.",
author
)]
/// The options for the wasmer Command Line Interface
enum WasmerCLIOptions {
/// Validate a WebAssembly binary
#[structopt(name = "validate")]
#[clap(name = "validate")]
Validate(Validate),

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

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

Expand Down Expand Up @@ -56,15 +56,15 @@ pub fn wasmer_main() {
let command = args.get(1);
let options = {
match command.unwrap_or(&"".to_string()).as_ref() {
"compile" | "config" | "help" | "inspect" | "validate" => WasmerCLIOptions::from_args(),
"compile" | "config" | "help" | "inspect" | "validate" => 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::Compile(Compile::from_args()),
ErrorKind::DisplayVersion | ErrorKind::DisplayHelp => e.exit(),
_ => WasmerCLIOptions::Compile(Compile::parse()),
}
})
}
Expand Down
17 changes: 10 additions & 7 deletions lib/cli-compiler/src/commands/compile.rs
Original file line number Diff line number Diff line change
@@ -1,32 +1,35 @@
use crate::store::{EngineType, StoreOptions};
use crate::warning;
use anyhow::{Context, Result};
use clap::Parser;
use std::path::{Path, PathBuf};
use structopt::StructOpt;
use wasmer_compiler::{CompileError, CpuFeature, ModuleEnvironment, Target, Triple};
use wasmer_engine_universal_artifact::{ArtifactCreate, UniversalArtifactBuild};
use wasmer_types::entity::PrimaryMap;
use wasmer_types::{MemoryIndex, MemoryStyle, TableIndex, TableStyle};

#[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,

/// 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
18 changes: 9 additions & 9 deletions lib/cli-compiler/src/commands/config.rs
Original file line number Diff line number Diff line change
@@ -1,39 +1,39 @@
use crate::VERSION;
use anyhow::{Context, Result};
use clap::Parser;
use std::env;
use std::path::PathBuf;
use structopt::StructOpt;

#[derive(Debug, StructOpt)]
#[derive(Debug, Parser)]
/// The options for the `wasmer config` subcommand
pub struct Config {
/// Print the installation prefix.
#[structopt(long, conflicts_with = "pkg-config")]
#[clap(long, conflicts_with = "pkg-config")]
prefix: bool,

/// Directory containing Wasmer executables.
#[structopt(long, conflicts_with = "pkg-config")]
#[clap(long, conflicts_with = "pkg-config")]
bindir: bool,

/// Directory containing Wasmer headers.
#[structopt(long, conflicts_with = "pkg-config")]
#[clap(long, conflicts_with = "pkg-config")]
includedir: bool,

/// Directory containing Wasmer libraries.
#[structopt(long, conflicts_with = "pkg-config")]
#[clap(long, conflicts_with = "pkg-config")]
libdir: bool,

/// Libraries needed to link against Wasmer components.
#[structopt(long, conflicts_with = "pkg-config")]
#[clap(long, conflicts_with = "pkg-config")]
libs: bool,

/// C compiler flags for files that include Wasmer headers.
#[structopt(long, conflicts_with = "pkg-config")]
#[clap(long, conflicts_with = "pkg-config")]
cflags: bool,

/// It outputs the necessary details for compiling
/// and linking a program to Wasmer, using the `pkg-config` format.
#[structopt(long)]
#[clap(long)]
pkg_config: bool,
}

Expand Down
8 changes: 4 additions & 4 deletions lib/cli-compiler/src/commands/validate.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
use crate::store::StoreOptions;
use anyhow::{bail, Context, Result};
use clap::Parser;
use std::path::PathBuf;
use std::str::FromStr;
use structopt::StructOpt;
use wasmer_compiler::{CpuFeature, Target, Triple};
use wasmer_types::is_wasm;

#[derive(Debug, StructOpt)]
#[derive(Debug, Parser)]
/// The options for the `wasmer validate` subcommand
pub struct Validate {
/// File to validate as WebAssembly
#[structopt(name = "FILE", parse(from_os_str))]
#[clap(name = "FILE", parse(from_os_str))]
path: PathBuf,

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

Expand Down
Loading