xref: /wasmtime-44.0.1/src/bin/wasmtime.rs (revision 248bc65e)
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::Parser;
8 
9 /// Wasmtime WebAssembly Runtime
10 #[derive(Parser, PartialEq)]
11 #[command(
12     version = version(),
13     after_help = "If a subcommand is not provided, the `run` subcommand will be used.\n\
14                   \n\
15                   Usage examples:\n\
16                   \n\
17                   Running a WebAssembly module with a start function:\n\
18                   \n  \
19                   wasmtime example.wasm
20                   \n\
21                   Passing command line arguments to a WebAssembly module:\n\
22                   \n  \
23                   wasmtime example.wasm arg1 arg2 arg3\n\
24                   \n\
25                   Invoking a specific function (e.g. `add`) in a WebAssembly module:\n\
26                   \n  \
27                   wasmtime --invoke add example.wasm 1 2\n",
28 
29     // This option enables the pattern below where we ask clap to parse twice
30     // sorta: once where it's trying to find a subcommand and once assuming
31     // a subcommand doesn't get passed. Clap should then, apparently,
32     // fill in the `subcommand` if found and otherwise fill in the
33     // `RunCommand`.
34     args_conflicts_with_subcommands = true
35 )]
36 struct Wasmtime {
37     #[cfg(not(feature = "run"))]
38     #[command(subcommand)]
39     subcommand: Subcommand,
40 
41     #[cfg(feature = "run")]
42     #[command(subcommand)]
43     subcommand: Option<Subcommand>,
44     #[command(flatten)]
45     #[cfg(feature = "run")]
46     run: wasmtime_cli::commands::RunCommand,
47 }
48 
49 /// If WASMTIME_VERSION_INFO is set, use it, otherwise use CARGO_PKG_VERSION.
50 fn version() -> &'static str {
51     option_env!("WASMTIME_VERSION_INFO").unwrap_or(env!("CARGO_PKG_VERSION"))
52 }
53 
54 #[derive(Parser, PartialEq)]
55 enum Subcommand {
56     /// Runs a WebAssembly module
57     #[cfg(feature = "run")]
58     Run(wasmtime_cli::commands::RunCommand),
59 
60     /// Controls Wasmtime configuration settings
61     #[cfg(feature = "cache")]
62     Config(wasmtime_cli::commands::ConfigCommand),
63 
64     /// Compiles a WebAssembly module.
65     #[cfg(feature = "compile")]
66     Compile(wasmtime_cli::commands::CompileCommand),
67 
68     /// Explore the compilation of a WebAssembly module to native code.
69     #[cfg(feature = "explore")]
70     Explore(wasmtime_cli::commands::ExploreCommand),
71 
72     /// Serves requests from a wasi-http proxy component.
73     #[cfg(feature = "serve")]
74     Serve(wasmtime_cli::commands::ServeCommand),
75 
76     /// Displays available Cranelift settings for a target.
77     #[cfg(feature = "cranelift")]
78     Settings(wasmtime_cli::commands::SettingsCommand),
79 
80     /// Runs a WebAssembly test script file
81     #[cfg(feature = "wast")]
82     Wast(wasmtime_cli::commands::WastCommand),
83 }
84 
85 impl Wasmtime {
86     /// Executes the command.
87     pub fn execute(self) -> Result<()> {
88         #[cfg(feature = "run")]
89         let subcommand = self.subcommand.unwrap_or(Subcommand::Run(self.run));
90         #[cfg(not(feature = "run"))]
91         let subcommand = self.subcommand;
92 
93         match subcommand {
94             #[cfg(feature = "run")]
95             Subcommand::Run(c) => c.execute(),
96 
97             #[cfg(feature = "cache")]
98             Subcommand::Config(c) => c.execute(),
99 
100             #[cfg(feature = "compile")]
101             Subcommand::Compile(c) => c.execute(),
102 
103             #[cfg(feature = "explore")]
104             Subcommand::Explore(c) => c.execute(),
105 
106             #[cfg(feature = "serve")]
107             Subcommand::Serve(c) => c.execute(),
108 
109             #[cfg(feature = "cranelift")]
110             Subcommand::Settings(c) => c.execute(),
111 
112             #[cfg(feature = "wast")]
113             Subcommand::Wast(c) => c.execute(),
114         }
115     }
116 }
117 
118 fn main() -> Result<()> {
119     return Wasmtime::parse().execute();
120 }
121 
122 #[test]
123 fn verify_cli() {
124     use clap::CommandFactory;
125     Wasmtime::command().debug_assert()
126 }
127