@@ -5,56 +5,50 @@ use anyhow::Result;
55use clap:: { Arg , SubCommand } ;
66use std:: fs:: File ;
77use std:: io:: Write ;
8- use std:: path:: PathBuf ;
8+ use std:: path:: Path ;
99use 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
2718fn 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