#[cfg(feature = "compiler")]
use crate::commands::Compile;
#[cfg(all(feature = "object-file", feature = "compiler"))]
use crate::commands::CreateExe;
#[cfg(feature = "wast")]
use crate::commands::Wast;
use crate::commands::{Cache, Config, Inspect, Run, SelfUpdate, Validate};
use crate::error::PrettyError;
use anyhow::Result;
use structopt::{clap::ErrorKind, StructOpt};
#[derive(Debug, StructOpt)]
#[cfg_attr(
not(feature = "headless"),
structopt(name = "wasmer", about = "WebAssembly standalone runtime.", author)
)]
#[cfg_attr(
feature = "headless",
structopt(
name = "wasmer-headless",
about = "Headless WebAssembly standalone runtime.",
author
)
)]
enum WasmerCLIOptions {
#[structopt(name = "run")]
Run(Run),
#[structopt(name = "cache")]
Cache(Cache),
#[structopt(name = "validate")]
Validate(Validate),
#[cfg(feature = "compiler")]
#[structopt(name = "compile")]
Compile(Compile),
#[cfg(all(feature = "object-file", feature = "compiler"))]
#[structopt(name = "create-exe")]
CreateExe(CreateExe),
#[structopt(name = "config")]
Config(Config),
#[structopt(name = "self-update")]
SelfUpdate(SelfUpdate),
#[structopt(name = "inspect")]
Inspect(Inspect),
#[cfg(feature = "wast")]
#[structopt(name = "wast")]
Wast(Wast),
}
impl WasmerCLIOptions {
fn execute(&self) -> Result<()> {
match self {
Self::Run(options) => options.execute(),
Self::SelfUpdate(options) => options.execute(),
Self::Cache(cache) => cache.execute(),
Self::Validate(validate) => validate.execute(),
#[cfg(feature = "compiler")]
Self::Compile(compile) => compile.execute(),
#[cfg(all(feature = "object-file", feature = "compiler"))]
Self::CreateExe(create_exe) => create_exe.execute(),
Self::Config(config) => config.execute(),
Self::Inspect(inspect) => inspect.execute(),
#[cfg(feature = "wast")]
Self::Wast(wast) => wast.execute(),
}
}
}
pub fn wasmer_main() {
#[cfg(windows)]
colored::control::set_virtual_terminal(true).unwrap();
let args = std::env::args().collect::<Vec<_>>();
let command = args.get(1);
let options = match command.unwrap_or(&"".to_string()).as_ref() {
"cache" | "compile" | "config" | "create-exe" | "help" | "inspect" | "run"
| "self-update" | "validate" | "wast" => WasmerCLIOptions::from_args(),
_ => {
WasmerCLIOptions::from_iter_safe(args.iter()).unwrap_or_else(|e| {
match e.kind {
ErrorKind::VersionDisplayed | ErrorKind::HelpDisplayed => e.exit(),
_ => WasmerCLIOptions::Run(Run::from_args()),
}
})
}
};
PrettyError::report(options.execute());
}