11use anyhow:: { bail, Context } ;
2- use clap :: { arg , command } ;
2+ use argh :: FromArgs ;
33use rustfmt_wrapper:: rustfmt;
44use serde:: { Deserialize , Serialize } ;
55use std:: collections:: BTreeMap ;
@@ -167,31 +167,48 @@ fn find_config_file() -> anyhow::Result<PathBuf> {
167167 ) )
168168}
169169
170+ #[ derive( FromArgs , PartialEq , Debug ) ]
171+ /// Codegen - Generate Rust code from templates
172+ struct CliArgs {
173+ /// output file paths (glob pattern)
174+ #[ argh( positional, hidden_help) ]
175+ glob : Option < String > ,
176+
177+ /// force overwrite existing files
178+ #[ argh( switch, short = 'f' ) ]
179+ force : bool ,
180+
181+ /// output to stdout
182+ #[ argh( switch, short = 's' ) ]
183+ stdout : bool ,
184+
185+ /// skip formatting output
186+ #[ argh( switch, short = 'n' ) ]
187+ nofmt : bool ,
188+
189+ /// check mode - compare generated files with existing
190+ #[ argh( switch) ]
191+ check : bool ,
192+
193+ /// verbose output
194+ #[ argh( switch, short = 'v' ) ]
195+ verbose : bool ,
196+ }
197+
170198fn main ( ) -> anyhow:: Result < ( ) > {
171- let matches = command ! ( )
172- . arg ( arg ! ( [ GLOB ] ) )
173- . arg ( arg ! ( -f - -force) )
174- . arg ( arg ! ( -s - -stdout) )
175- . arg ( arg ! ( -n - -nofmt) )
176- . arg ( arg ! ( --check) )
177- . arg ( arg ! ( -v - -verbose) )
178- . get_matches ( ) ;
179-
180- let force = matches. is_present ( "force" ) ;
181- let stdout = matches. is_present ( "stdout" ) ;
182- let fmt_output = !matches. is_present ( "nofmt" ) ;
183- let output_path_glob = matches. value_of ( "GLOB" ) ;
184- let check = matches. is_present ( "check" ) ;
185- let verbose = matches. is_present ( "verbose" ) ;
186-
187- if stdout && output_path_glob. is_none ( ) {
199+ let args: CliArgs = argh:: from_env ( ) ;
200+
201+ let fmt_output = !args. nofmt ;
202+ let output_path_glob = args. glob ;
203+
204+ if args. stdout && output_path_glob. is_none ( ) {
188205 // TODO: What if the glob matches multiple files?
189206 bail ! ( "specify a single file to output to stdout." ) ;
190207 }
191208
192- let glob = if let Some ( output_path_glob) = output_path_glob {
209+ let glob = if let Some ( ref output_path_glob) = output_path_glob {
193210 Some (
194- globset:: Glob :: new ( output_path_glob)
211+ globset:: Glob :: new ( output_path_glob. as_str ( ) )
195212 . context ( "failed to compile glob" ) ?
196213 . compile_matcher ( ) ,
197214 )
@@ -255,14 +272,14 @@ fn main() -> anyhow::Result<()> {
255272
256273 let mut output_differences = 0 ;
257274 for output_path in output_paths {
258- if !check {
275+ if !args . check {
259276 println ! ( "generating {output_path}" ) ;
260277 }
261278
262279 let context = output_pairs. get ( output_path) . unwrap ( ) ;
263280 let template_path = context. get ( "template_path" ) . unwrap ( ) . as_str ( ) . unwrap ( ) ;
264281
265- if !( check || force || stdout) && is_modified ( & repo, output_path) ? {
282+ if !( args . check || args . force || args . stdout ) && is_modified ( & repo, output_path) ? {
266283 bail ! (
267284 "{} is already modified, use `-f` to force overwrite or revert local changes." ,
268285 output_path
@@ -271,7 +288,7 @@ fn main() -> anyhow::Result<()> {
271288
272289 let mut output_str = generate_file ( & tera, context, template_path) ?;
273290
274- if fmt_output || check {
291+ if fmt_output || args . check {
275292 output_str = rustfmt ( & output_str) . context ( "rustfmt failed" ) ?;
276293 }
277294
@@ -280,13 +297,13 @@ fn main() -> anyhow::Result<()> {
280297 let output_dir = full_output_path. parent ( ) . unwrap ( ) ;
281298 std:: fs:: create_dir_all ( output_dir) ?;
282299
283- if check {
300+ if args . check {
284301 match std:: fs:: read_to_string ( & full_output_path) {
285302 Ok ( original_str) => {
286303 if output_str != original_str {
287304 println ! ( "'{output_path}' is different" ) ;
288305 output_differences += 1 ;
289- } else if verbose {
306+ } else if args . verbose {
290307 println ! ( "'{output_path}' is the same" ) ;
291308 }
292309 }
@@ -298,7 +315,7 @@ fn main() -> anyhow::Result<()> {
298315 continue ;
299316 }
300317
301- if stdout {
318+ if args . stdout {
302319 print ! ( "{output_str}" ) ;
303320 continue ;
304321 }
@@ -307,7 +324,7 @@ fn main() -> anyhow::Result<()> {
307324 . with_context ( || format ! ( "failed to write {}" , full_output_path. display( ) ) ) ?;
308325 }
309326
310- if check && output_differences > 0 {
327+ if args . check && output_differences > 0 {
311328 bail ! ( "{output_differences} files were different" ) ;
312329 }
313330
0 commit comments