forked from bytecodealliance/wasm-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwasm2wat-rs.rs
More file actions
35 lines (31 loc) · 970 Bytes
/
Copy pathwasm2wat-rs.rs
File metadata and controls
35 lines (31 loc) · 970 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
use anyhow::Context;
use getopts::Options;
use std::env;
fn main() -> anyhow::Result<()> {
let mut opts = Options::new();
opts.optopt("o", "", "set output file name", "NAME");
opts.optflag("h", "help", "print this help menu");
let matches = opts.parse(env::args_os().skip(1))?;
if matches.opt_present("h") {
print_usage(opts);
return Ok(());
}
let input = if matches.free.len() == 1 {
matches.free[0].clone()
} else {
print_usage(opts);
std::process::exit(1);
};
let wit = wasmprinter::print_file(&input)?;
if let Some(output) = matches.opt_str("o") {
std::fs::write(&output, wit).context(format!("failed to write `{}`", output))?;
} else {
println!("{}", wit);
}
Ok(())
}
fn print_usage(opts: Options) {
let program = env::args().next().unwrap();
let brief = format!("Usage: {} FILE [options]", program);
print!("{}", opts.usage(&brief));
}