wasmer_compiler_cli/
cli.rs

1//! The logic for the Wasmer CLI tool.
2
3use crate::commands::Compile;
4use crate::commands::{Config, Validate};
5use crate::error::PrettyError;
6use anyhow::Result;
7
8use clap::{Parser, error::ErrorKind};
9
10#[derive(Parser)]
11#[clap(
12    name = "wasmer-compiler",
13    about = "WebAssembly standalone Compiler.",
14    author
15)]
16/// The options for the wasmer Command Line Interface
17enum WasmerCLIOptions {
18    /// Validate a WebAssembly binary
19    #[clap(name = "validate")]
20    Validate(Validate),
21
22    /// Compile a WebAssembly binary
23    #[clap(name = "compile")]
24    Compile(Compile),
25
26    /// Get various configuration information needed
27    /// to compile programs which use Wasmer
28    #[clap(name = "config")]
29    Config(Config),
30}
31
32impl WasmerCLIOptions {
33    fn execute(&self) -> Result<()> {
34        match self {
35            Self::Validate(validate) => validate.execute(),
36            Self::Compile(compile) => compile.execute(),
37            Self::Config(config) => config.execute(),
38        }
39    }
40}
41
42/// The main function for the Wasmer CLI tool.
43pub fn wasmer_main() {
44    // We allow windows to print properly colors
45    #[cfg(windows)]
46    colored::control::set_virtual_terminal(true).unwrap();
47
48    // We try to run wasmer with the normal arguments.
49    // Eg. `wasmer <SUBCOMMAND>`
50    // In case that fails, we fallback trying the Run subcommand directly.
51    // Eg. `wasmer myfile.wasm --dir=.`
52    //
53    // In case we've been run as wasmer-binfmt-interpreter myfile.wasm args,
54    // we assume that we're registered via binfmt_misc
55    let args = std::env::args().collect::<Vec<_>>();
56    let command = args.get(1);
57    let options = {
58        match command.unwrap_or(&"".to_string()).as_ref() {
59            "compile" | "config" | "help" | "inspect" | "validate" => WasmerCLIOptions::parse(),
60            _ => {
61                WasmerCLIOptions::try_parse_from(args.iter()).unwrap_or_else(|e| {
62                    match e.kind() {
63                        // This fixes a issue that:
64                        // 1. Shows the version twice when doing `wasmer -V`
65                        // 2. Shows the run help (instead of normal help) when doing `wasmer --help`
66                        ErrorKind::DisplayVersion | ErrorKind::DisplayHelp => e.exit(),
67                        _ => WasmerCLIOptions::Compile(Compile::parse()),
68                    }
69                })
70            }
71        }
72    };
73
74    PrettyError::report(options.execute());
75}