Skip to content

Commit 3826dd8

Browse files
committed
Update CLI run documentation
Try to take the spirit of #10792 to improve our documentation for the `run` command and how CLI arguments interact with the provided WebAssembly module.
1 parent 2c70b3f commit 3826dd8

File tree

1 file changed

+78
-7
lines changed

1 file changed

+78
-7
lines changed

docs/cli-options.md

Lines changed: 78 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,25 +44,92 @@ as well as the text format for WebAssembly (`*.wat`):
4444
wasmtime foo.wat
4545
```
4646

47-
**Wasm Modules**
47+
#### Running "Command" Wasms
4848

49-
A Wasm **module** exports raw functions directly. The `run` command accepts an optional `--invoke` argument, which is the name of an exported raw function (of the module) to run:
49+
WebAssembly modules or components can behave like a "command" which means
50+
they're intended to look like a normal OS executable with a `main` function and
51+
run once to completion. This is the default mode of running a wasm provided to
52+
Wasmtime.
53+
54+
For core WebAssembly modules this means that the function exported as an empty
55+
string, or the `_start` export, is invoked. For WebAssembly components this
56+
means that the `wasi:cli/run` interface is executed.
57+
58+
For both core modules and components CLI arguments are passed via WASI. Core
59+
modules receive arguments via WASIp1 APIs and components receive arguments via
60+
WASIp2 or later APIs. Arguments, flags, etc, are passed to the WebAssembly file
61+
after the file itself. For example:
62+
63+
```console
64+
wasmtime foo.wasm --bar baz
65+
```
66+
67+
Will pass `["foo.wasm", "--bar", "baz"]` as the list of arguments to the module.
68+
Note that flags for Wasmtime must be passed before the WebAssembly file, not
69+
afterwards. For example
70+
71+
```console
72+
wasmtime foo.wasm --dir .
73+
```
74+
75+
will pass `--dir .` to the `foo.wasm` program, not Wasmtime. If you want to
76+
mount the current directory you instead need to invoke
77+
78+
```console
79+
wasmtime --dir . foo.wasm
80+
```
81+
82+
All Wasmtime options must come before the WebAssembly file provided. All
83+
arguments afterwards are passed to the WebAssembly file itself.
84+
85+
#### Running Custom Module exports
86+
87+
If you're not running a "command" but want to run a specific export of a
88+
WebAssembly core module you can use the `--invoke` argument:
5089

5190
```console
5291
wasmtime run --invoke initialize foo.wasm
5392
```
5493

55-
**Wasm Components**
94+
This will invoke the `initialize` export of the `foo.wasm` module.
5695

57-
A Wasm **component** uses typed interfaces defined by [the component model](https://component-model.bytecodealliance.org/design/components.html). The `run` command also accepts the optional `--invoke` argument for calling an exported function of a **component**. However, the calling of an exported function of a component uses [WAVE](https://github.com/bytecodealliance/wasm-tools/tree/a56e8d3d2a0b754e0465c668f8e4b68bad97590f/crates/wasm-wave#readme)(a human-oriented text encoding of Wasm Component Model values). For example:
96+
When invoking a WebAssembly function arguments to the function itself are parsed
97+
from CLI arguments. For example an `i32` argument to a WebAssembly module is
98+
parsed as a CLI argument for the module:
99+
100+
```console
101+
wasmtime run --invoke add add.wasm 1 2
102+
```
103+
104+
Note though that this syntax is unstable at this time and may change in the
105+
future. If you'd like to rely on this please open an issue, otherwise we request
106+
that you please don't rely on the exact output here.
107+
108+
#### Running Custom Component exports
109+
110+
Like core modules Wasmtime supports invoking arbitrary component exports.
111+
Components can export typed interfaces defined by [the component
112+
model](https://component-model.bytecodealliance.org/design/components.html). The
113+
`--invoke` argument is supported to skip calling `wasi:cli/run` and invoke a
114+
specific typed export instead. Arguments are passed with
115+
[WAVE](https://github.com/bytecodealliance/wasm-tools/tree/main/crates/wasm-wave#readme)
116+
,a human-oriented text encoding of Wasm Component Model values. For example:
58117

59118
```console
60119
wasmtime run --invoke 'initialize()' foo.wasm
61120
```
62121

63-
You will notice that (when using WAVE) the exported function's name and exported function's parentheses are both enclosed in one set of single quotes, i.e. `'initialize()'`. This treats the exported function as a single argument, prevents issues with shell interpretation and signifies function invocation (as apposed to the function name just being referenced). Using WAVE (when calling exported functions of Wasm components) helps to distinguish function calls from other kinds of string arguments. Below are some more examples:
122+
You will notice that (when using WAVE) the exported function's name and exported
123+
function's parentheses are both enclosed in one set of single quotes, i.e.
124+
`'initialize()'`. This treats the exported function as a single argument in a
125+
Unix shell and prevents issues with shell interpretation and signifies function
126+
invocation (as apposed to the function name just being referenced). Using WAVE
127+
(when calling exported functions of Wasm components) helps to distinguish
128+
function calls from other kinds of string arguments. Below are some more
129+
examples:
64130

65-
If your function takes a string argument, you surround the string argument in double quotes:
131+
If your function takes a string argument, you surround the string argument in
132+
double quotes:
66133

67134
```console
68135
wasmtime run --invoke 'initialize("hello")' foo.wasm
@@ -75,7 +142,11 @@ wasmtime run --invoke 'initialize("Pi", 3.14)' foo.wasm
75142
wasmtime run --invoke 'add(1, 2)' foo.wasm
76143
```
77144

78-
**Please note:** If you enclose your whole function call using double quotes, your string argument will require its double quotes to be escaped (escaping quotes is more complicated and harder to read and therefore not ideal). For example:
145+
**Please note:** If you enclose your whole function call using double quotes,
146+
your string argument will require its double quotes to be escaped (escaping
147+
quotes is more complicated and harder to read and therefore not ideal). For
148+
example:
149+
79150
```bash
80151
wasmtime run - invoke "initialize(\"hello\")" foo.wasm
81152
```

0 commit comments

Comments
 (0)