1 //! The `wasmtime` command line tool.
2 //!
3 //! Primarily used to run WebAssembly modules.
4 //! See `wasmtime --help` for usage.
5
6 use anyhow::Result;
7 use structopt::{clap::AppSettings, clap::ErrorKind, StructOpt};
8 use wasmtime_cli::commands::{
9 ConfigCommand, RunCommand, WasmToObjCommand, WastCommand, WASM2OBJ_AFTER_HELP,
10 };
11
12 /// Wasmtime WebAssembly Runtime
13 #[derive(StructOpt)]
14 #[structopt(
15 name = "wasmtime",
16 version = env!("CARGO_PKG_VERSION"),
17 global_settings = &[
18 AppSettings::VersionlessSubcommands,
19 AppSettings::ColoredHelp
20 ],
21 after_help = "If a subcommand is not provided, the `run` subcommand will be used.\n\
22 \n\
23 Usage examples:\n\
24 \n\
25 Running a WebAssembly module with a start function:\n\
26 \n \
27 wasmtime example.wasm
28 \n\
29 Passing command line arguments to a WebAssembly module:\n\
30 \n \
31 wasmtime example.wasm arg1 arg2 arg3\n\
32 \n\
33 Invoking a specific function (e.g. `add`) in a WebAssembly module:\n\
34 \n \
35 wasmtime example.wasm --invoke add 1 2\n"
36 )]
37 enum WasmtimeApp {
38 // !!! IMPORTANT: if subcommands are added or removed, update `parse_module` in `src/commands/run.rs`. !!!
39 /// Controls Wasmtime configuration settings
40 Config(ConfigCommand),
41 /// Runs a WebAssembly module
42 Run(RunCommand),
43 /// Translates a WebAssembly module to native object file
44 #[structopt(name = "wasm2obj", after_help = WASM2OBJ_AFTER_HELP)]
45 WasmToObj(WasmToObjCommand),
46 /// Runs a WebAssembly test script file
47 Wast(WastCommand),
48 }
49
50 impl WasmtimeApp {
51 /// Executes the command.
execute(&self) -> Result<()>52 pub fn execute(&self) -> Result<()> {
53 match self {
54 Self::Config(c) => c.execute(),
55 Self::Run(c) => c.execute(),
56 Self::WasmToObj(c) => c.execute(),
57 Self::Wast(c) => c.execute(),
58 }
59 }
60 }
61
main() -> Result<()>62 fn main() -> Result<()> {
63 WasmtimeApp::from_iter_safe(std::env::args())
64 .unwrap_or_else(|e| match e.kind {
65 ErrorKind::HelpDisplayed
66 | ErrorKind::VersionDisplayed
67 | ErrorKind::MissingArgumentOrSubcommand => e.exit(),
68 _ => WasmtimeApp::Run(
69 RunCommand::from_iter_safe(std::env::args()).unwrap_or_else(|_| e.exit()),
70 ),
71 })
72 .execute()
73 }
74