Files
wasmtime/src/bin/wasmtime.rs
Peter Huene 59258730c2 Use structopt instead of docopt.
This commit refactors the Wasmtime CLI tools to use `structopt` instead of
`docopt`.

The `wasmtime` tool now has the following subcommands:

* `config new` - creates a new Wasmtime configuration file.
* `run` - runs a WebAssembly module.
* `wasm2obj` - translates a Wasm module to native object file.
* `wast` - runs a test script file.

If no subcommand is specified, the `run` subcommand is used. Thus,
`wasmtime foo.wasm` should continue to function as expected.

The `wasm2obj` and `wast` tools still exist, but delegate to the same
implementation as the `wasmtime` subcommands.  The standalone `wasm2obj` and
`wast` tools may be removed in the future in favor of simply using `wasmtime`.

Included in this commit is a breaking change to the default Wasmtime
configuration file: it has been renamed from `wasmtime-cache-config.toml` to
simply `config.toml`.  The new name is less specific which will allow for
additional (non-cache-related) settings in the future.

There are some breaking changes to improve command line UX:

* The `--cache-config` option has been renamed to `--config`.
* The `--create-config-file` option has moved to the `config new` subcommand.
As a result, the `wasm2obj` and `wast` tools cannot be used to create a new
config file.
* The short form of the `--optimize` option has changed from
`-o` to `-O` for consistency.
* The `wasm2obj` command takes the output object file as a
required positional argument rather than the former required output *option*
(e.g. `wasmtime wasm2obj foo.wasm foo.obj`).
2020-01-07 13:15:28 -08:00

74 lines
2.4 KiB
Rust

//! The `wasmtime` command line tool.
//!
//! Primarily used to run WebAssembly modules.
//! See `wasmtime --help` for usage.
use anyhow::Result;
use structopt::{clap::AppSettings, clap::ErrorKind, StructOpt};
use wasmtime_cli::commands::{
ConfigCommand, RunCommand, WasmToObjCommand, WastCommand, WASM2OBJ_AFTER_HELP,
};
/// Wasmtime WebAssembly Runtime
#[derive(StructOpt)]
#[structopt(
name = "wasmtime",
version = env!("CARGO_PKG_VERSION"),
global_settings = &[
AppSettings::VersionlessSubcommands,
AppSettings::ColoredHelp
],
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 example.wasm --invoke add 1 2\n"
)]
enum WasmtimeApp {
// !!! IMPORTANT: if subcommands are added or removed, update `parse_module` in `src/commands/run.rs`. !!!
/// Controls Wasmtime configuration settings
Config(ConfigCommand),
/// Runs a WebAssembly module
Run(RunCommand),
/// Translates a WebAssembly module to native object file
#[structopt(name = "wasm2obj", after_help = WASM2OBJ_AFTER_HELP)]
WasmToObj(WasmToObjCommand),
/// Runs a WebAssembly test script file
Wast(WastCommand),
}
impl WasmtimeApp {
/// Executes the command.
pub fn execute(&self) -> Result<()> {
match self {
Self::Config(c) => c.execute(),
Self::Run(c) => c.execute(),
Self::WasmToObj(c) => c.execute(),
Self::Wast(c) => c.execute(),
}
}
}
fn main() -> Result<()> {
WasmtimeApp::from_iter_safe(std::env::args())
.unwrap_or_else(|e| match e.kind {
ErrorKind::HelpDisplayed
| ErrorKind::VersionDisplayed
| ErrorKind::MissingArgumentOrSubcommand => e.exit(),
_ => WasmtimeApp::Run(
RunCommand::from_iter_safe(std::env::args()).unwrap_or_else(|_| e.exit()),
),
})
.execute()
}