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 20, 2022
1 parent 5e52b64 commit 2c3efeb
Show file tree
Hide file tree
Showing 25 changed files with 262 additions and 205 deletions.
96 changes: 65 additions & 31 deletions Cargo.lock

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

4 changes: 2 additions & 2 deletions lib/cli-compiler/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ doc = false

[dependencies]
wasmer-engine-universal-artifact = { version = "=2.2.1", path = "../universal-artifact", features = ["compiler"] }
wasmer-compiler = { version = "=2.2.1", path = "../compiler" }
wasmer-compiler = { version = "=2.2.1", path = "../compiler", features = ["std"] }
wasmer-types = { version = "=2.2.1", 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
Expand Up @@ -2,31 +2,34 @@ use crate::store::{EngineType, StoreOptions};
use crate::warning;
use anyhow::{Context, Result};
use std::path::{Path, PathBuf};
use structopt::StructOpt;
use clap::Parser;
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
Expand Up @@ -2,38 +2,38 @@ use crate::VERSION;
use anyhow::{Context, Result};
use std::env;
use std::path::PathBuf;
use structopt::StructOpt;
use clap::Parser;

#[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
Expand Up @@ -2,18 +2,18 @@ use crate::store::StoreOptions;
use anyhow::{bail, Context, Result};
use std::path::PathBuf;
use std::str::FromStr;
use structopt::StructOpt;
use clap::Parser;
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

0 comments on commit 2c3efeb

Please sign in to comment.