use anyhow::Result;
use clap::Parser;
#[derive(Parser)]
#[command(
name = "wasmtime",
version = version(),
after_help = "If a subcommand is not provided, the `run` subcommand will be used.\n\
\n\
Usage examples:\n\
\n\
Running a WebAssembly module with a start function:\n\
\n \
wasmtime example.wasm
\n\
Passing command line arguments to a WebAssembly module:\n\
\n \
wasmtime example.wasm arg1 arg2 arg3\n\
\n\
Invoking a specific function (e.g. `add`) in a WebAssembly module:\n\
\n \
wasmtime --invoke add example.wasm 1 2\n",
args_conflicts_with_subcommands = true
)]
struct Wasmtime {
#[cfg(not(feature = "run"))]
#[command(subcommand)]
subcommand: Subcommand,
#[cfg(feature = "run")]
#[command(subcommand)]
subcommand: Option<Subcommand>,
#[command(flatten)]
#[cfg(feature = "run")]
run: wasmtime_cli::commands::RunCommand,
}
fn version() -> &'static str {
option_env!("WASMTIME_VERSION_INFO").unwrap_or(env!("CARGO_PKG_VERSION"))
}
#[derive(Parser)]
enum Subcommand {
#[cfg(feature = "run")]
Run(wasmtime_cli::commands::RunCommand),
#[cfg(feature = "cache")]
Config(wasmtime_cli::commands::ConfigCommand),
#[cfg(feature = "compile")]
Compile(wasmtime_cli::commands::CompileCommand),
#[cfg(feature = "explore")]
Explore(wasmtime_cli::commands::ExploreCommand),
#[cfg(feature = "serve")]
Serve(wasmtime_cli::commands::ServeCommand),
#[cfg(feature = "cranelift")]
Settings(wasmtime_cli::commands::SettingsCommand),
#[cfg(feature = "wast")]
Wast(wasmtime_cli::commands::WastCommand),
#[cfg(feature = "completion")]
Completion(CompletionCommand),
#[cfg(feature = "objdump")]
Objdump(wasmtime_cli::commands::ObjdumpCommand),
}
impl Wasmtime {
pub fn execute(self) -> Result<()> {
#[cfg(feature = "run")]
let subcommand = self.subcommand.unwrap_or(Subcommand::Run(self.run));
#[cfg(not(feature = "run"))]
let subcommand = self.subcommand;
match subcommand {
#[cfg(feature = "run")]
Subcommand::Run(c) => c.execute(),
#[cfg(feature = "cache")]
Subcommand::Config(c) => c.execute(),
#[cfg(feature = "compile")]
Subcommand::Compile(c) => c.execute(),
#[cfg(feature = "explore")]
Subcommand::Explore(c) => c.execute(),
#[cfg(feature = "serve")]
Subcommand::Serve(c) => c.execute(),
#[cfg(feature = "cranelift")]
Subcommand::Settings(c) => c.execute(),
#[cfg(feature = "wast")]
Subcommand::Wast(c) => c.execute(),
#[cfg(feature = "completion")]
Subcommand::Completion(c) => c.execute(),
#[cfg(feature = "objdump")]
Subcommand::Objdump(c) => c.execute(),
}
}
}
#[derive(Parser)]
#[cfg(feature = "completion")]
pub struct CompletionCommand {
shell: clap_complete::Shell,
}
#[cfg(feature = "completion")]
impl CompletionCommand {
pub fn execute(&self) -> Result<()> {
use clap::CommandFactory;
let mut cmd = Wasmtime::command();
let cli_name = cmd.get_name().to_owned();
clap_complete::generate(self.shell, &mut cmd, cli_name, &mut std::io::stdout());
Ok(())
}
}
fn main() -> Result<()> {
return Wasmtime::parse().execute();
}
#[test]
fn verify_cli() {
use clap::CommandFactory;
Wasmtime::command().debug_assert()
}