xref: /wasmtime-44.0.1/src/bin/wasmtime.rs (revision 7fa89c4a)
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 clap::{ErrorKind, Parser};
8 use wasmtime_cli::commands::{
9     CompileCommand, ConfigCommand, RunCommand, SettingsCommand, WastCommand,
10 };
11 
12 /// Wasmtime WebAssembly Runtime
13 #[derive(Parser)]
14 #[clap(
15     version,
16     after_help = "If a subcommand is not provided, the `run` subcommand will be used.\n\
17                   \n\
18                   Usage examples:\n\
19                   \n\
20                   Running a WebAssembly module with a start function:\n\
21                   \n  \
22                   wasmtime example.wasm
23                   \n\
24                   Passing command line arguments to a WebAssembly module:\n\
25                   \n  \
26                   wasmtime example.wasm arg1 arg2 arg3\n\
27                   \n\
28                   Invoking a specific function (e.g. `add`) in a WebAssembly module:\n\
29                   \n  \
30                   wasmtime example.wasm --invoke add 1 2\n"
31 )]
32 enum Wasmtime {
33     // !!! IMPORTANT: if subcommands are added or removed, update `parse_module` in `src/commands/run.rs`. !!!
34     /// Controls Wasmtime configuration settings
35     Config(ConfigCommand),
36     /// Compiles a WebAssembly module.
37     Compile(CompileCommand),
38     /// Runs a WebAssembly module
39     Run(RunCommand),
40     /// Displays available Cranelift settings for a target.
41     Settings(SettingsCommand),
42     /// Runs a WebAssembly test script file
43     Wast(WastCommand),
44 }
45 
46 impl Wasmtime {
47     /// Executes the command.
48     pub fn execute(self) -> Result<()> {
49         match self {
50             Self::Config(c) => c.execute(),
51             Self::Compile(c) => c.execute(),
52             Self::Run(c) => c.execute(),
53             Self::Settings(c) => c.execute(),
54             Self::Wast(c) => c.execute(),
55         }
56     }
57 }
58 
59 fn main() -> Result<()> {
60     Wasmtime::try_parse()
61         .unwrap_or_else(|e| match e.kind() {
62             ErrorKind::UnrecognizedSubcommand | ErrorKind::UnknownArgument => {
63                 Wasmtime::Run(RunCommand::parse())
64             }
65             _ => e.exit(),
66         })
67         .execute()
68 }
69