Skip to content

Commit b3c57f1

Browse files
committed
main: get more useful help by requiring subcommand
fake required arguments are no good
1 parent d415ae6 commit b3c57f1

2 files changed

Lines changed: 37 additions & 36 deletions

File tree

tools/wasi-headers/src/lib.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,23 @@ use anyhow::{anyhow, Result};
44
pub use c_header::to_c_header;
55
use std::fs;
66
use std::io;
7-
use std::path::PathBuf;
7+
use std::path::{Path, PathBuf};
88
use witx::load;
99

10-
pub fn generate(inputs: &[PathBuf]) -> Result<String> {
10+
pub fn generate<P: AsRef<Path>>(inputs: &[P]) -> Result<String> {
1111
// TODO: drop the anyhow! part once witx switches to anyhow.
1212
let doc = load(&inputs).map_err(|e| anyhow!(e.to_string()))?;
1313

1414
let inputs_str = &inputs
1515
.iter()
16-
.map(|p| p.file_name().unwrap().to_str().unwrap().to_string())
16+
.map(|p| {
17+
p.as_ref()
18+
.file_name()
19+
.unwrap()
20+
.to_str()
21+
.unwrap()
22+
.to_string()
23+
})
1724
.collect::<Vec<_>>()
1825
.join(", ");
1926

tools/wasi-headers/src/main.rs

Lines changed: 27 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -5,56 +5,50 @@ use anyhow::Result;
55
use clap::{Arg, SubCommand};
66
use std::fs::File;
77
use std::io::Write;
8-
use std::path::PathBuf;
8+
use std::path::Path;
99
use wasi_headers::{generate, libc_wasi_api_header, snapshot_witx_files};
1010

11-
struct GenerateCommand {
12-
/// Input witx file
13-
inputs: Vec<PathBuf>,
14-
/// Output header file
15-
output: PathBuf,
16-
}
17-
18-
impl GenerateCommand {
19-
pub fn execute(&self) -> Result<()> {
20-
let c_header = generate(&self.inputs)?;
21-
let mut file = File::create(&self.output)?;
22-
file.write_all(c_header.as_bytes())?;
23-
Ok(())
24-
}
11+
pub fn run<P: AsRef<Path>, Q: AsRef<Path>>(inputs: &[P], output: Q) -> Result<()> {
12+
let c_header = generate(inputs)?;
13+
let mut file = File::create(output)?;
14+
file.write_all(c_header.as_bytes())?;
15+
Ok(())
2516
}
2617

2718
fn main() -> Result<()> {
2819
let matches = app_from_crate!()
29-
.arg(Arg::with_name("inputs").required(false).multiple(true))
30-
.arg(
31-
Arg::with_name("output")
32-
.short("o")
33-
.long("output")
34-
.takes_value(true)
35-
.required(false),
20+
.setting(clap::AppSettings::SubcommandRequiredElseHelp)
21+
.subcommand(
22+
SubCommand::with_name("generate")
23+
.arg(Arg::with_name("inputs").required(true).multiple(true))
24+
.arg(
25+
Arg::with_name("output")
26+
.short("o")
27+
.long("output")
28+
.takes_value(true)
29+
.required(true),
30+
),
3631
)
3732
.subcommand(
3833
SubCommand::with_name("generate-libc")
3934
.about("generate libc api.h from current snapshot"),
4035
)
4136
.get_matches();
4237

43-
let cmd = if matches.subcommand_matches("generate-libc").is_some() {
38+
if matches.subcommand_matches("generate-libc").is_some() {
4439
let inputs = snapshot_witx_files()?;
4540
let output = libc_wasi_api_header();
46-
GenerateCommand { inputs, output }
41+
run(&inputs, &output)?;
42+
} else if let Some(generate) = matches.subcommand_matches("generate") {
43+
let inputs = generate
44+
.values_of("inputs")
45+
.expect("required inputs arg")
46+
.collect::<Vec<_>>();
47+
let output = generate.value_of("output").expect("required output arg");
48+
run(&inputs, output)?;
4749
} else {
48-
GenerateCommand {
49-
inputs: matches
50-
.values_of("inputs")
51-
.expect("inputs required")
52-
.map(PathBuf::from)
53-
.collect(),
54-
output: PathBuf::from(matches.value_of("output").expect("output required")),
55-
}
50+
unreachable!("a subcommand must be provided")
5651
};
5752

58-
cmd.execute()?;
5953
Ok(())
6054
}

0 commit comments

Comments
 (0)