@@ -98,7 +98,6 @@ impl Default for Subcommand {
9898
9999impl Flags {
100100 pub fn parse ( args : & [ String ] ) -> Flags {
101- let mut extra_help = String :: new ( ) ;
102101 let mut subcommand_help = String :: from (
103102 "\
104103 Usage: x.py <subcommand> [options] [<paths>...]
@@ -170,16 +169,6 @@ To learn more about a subcommand, run `./x.py <subcommand> -h`",
170169 "VALUE" ,
171170 ) ;
172171
173- // fn usage()
174- let usage =
175- |exit_code : i32 , opts : & Options , subcommand_help : & str , extra_help : & str | -> ! {
176- println ! ( "{}" , opts. usage( subcommand_help) ) ;
177- if !extra_help. is_empty ( ) {
178- println ! ( "{}" , extra_help) ;
179- }
180- process:: exit ( exit_code) ;
181- } ;
182-
183172 // We can't use getopt to parse the options until we have completed specifying which
184173 // options are valid, but under the current implementation, some options are conditional on
185174 // the subcommand. Therefore we must manually identify the subcommand first, so that we can
@@ -263,12 +252,38 @@ To learn more about a subcommand, run `./x.py <subcommand> -h`",
263252 _ => { }
264253 } ;
265254
255+ // fn usage()
256+ let usage = |exit_code : i32 , opts : & Options , verbose : bool , subcommand_help : & str | -> ! {
257+ let mut extra_help = String :: new ( ) ;
258+
259+ // All subcommands except `clean` can have an optional "Available paths" section
260+ if verbose {
261+ let config = Config :: parse ( & [ "build" . to_string ( ) ] ) ;
262+ let build = Build :: new ( config) ;
263+
264+ let maybe_rules_help = Builder :: get_help ( & build, subcommand. as_str ( ) ) ;
265+ extra_help. push_str ( maybe_rules_help. unwrap_or_default ( ) . as_str ( ) ) ;
266+ } else if !( subcommand. as_str ( ) == "clean" || subcommand. as_str ( ) == "fmt" ) {
267+ extra_help. push_str (
268+ format ! ( "Run `./x.py {} -h -v` to see a list of available paths." , subcommand)
269+ . as_str ( ) ,
270+ ) ;
271+ }
272+
273+ println ! ( "{}" , opts. usage( subcommand_help) ) ;
274+ if !extra_help. is_empty ( ) {
275+ println ! ( "{}" , extra_help) ;
276+ }
277+ process:: exit ( exit_code) ;
278+ } ;
279+
266280 // Done specifying what options are possible, so do the getopts parsing
267281 let matches = opts. parse ( & args[ ..] ) . unwrap_or_else ( |e| {
268282 // Invalid argument/option format
269283 println ! ( "\n {}\n " , e) ;
270- usage ( 1 , & opts, & subcommand_help , & extra_help ) ;
284+ usage ( 1 , & opts, false , & subcommand_help ) ;
271285 } ) ;
286+
272287 // Extra sanity check to make sure we didn't hit this crazy corner case:
273288 //
274289 // ./x.py --frobulate clean build
@@ -436,24 +451,11 @@ Arguments:
436451 let paths = matches. free [ 1 ..] . iter ( ) . map ( |p| p. into ( ) ) . collect :: < Vec < PathBuf > > ( ) ;
437452
438453 let cfg_file = env:: var_os ( "BOOTSTRAP_CONFIG" ) . map ( PathBuf :: from) ;
439-
440- // All subcommands except `clean` can have an optional "Available paths" section
441- if matches. opt_present ( "verbose" ) {
442- let config = Config :: parse ( & [ "build" . to_string ( ) ] ) ;
443- let build = Build :: new ( config) ;
444-
445- let maybe_rules_help = Builder :: get_help ( & build, subcommand. as_str ( ) ) ;
446- extra_help. push_str ( maybe_rules_help. unwrap_or_default ( ) . as_str ( ) ) ;
447- } else if !( subcommand. as_str ( ) == "clean" || subcommand. as_str ( ) == "fmt" ) {
448- extra_help. push_str (
449- format ! ( "Run `./x.py {} -h -v` to see a list of available paths." , subcommand)
450- . as_str ( ) ,
451- ) ;
452- }
454+ let verbose = matches. opt_present ( "verbose" ) ;
453455
454456 // User passed in -h/--help?
455457 if matches. opt_present ( "help" ) {
456- usage ( 0 , & opts, & subcommand_help , & extra_help ) ;
458+ usage ( 0 , & opts, verbose , & subcommand_help ) ;
457459 }
458460
459461 let cmd = match subcommand. as_str ( ) {
@@ -483,7 +485,7 @@ Arguments:
483485 "clean" => {
484486 if !paths. is_empty ( ) {
485487 println ! ( "\n clean does not take a path argument\n " ) ;
486- usage ( 1 , & opts, & subcommand_help , & extra_help ) ;
488+ usage ( 1 , & opts, verbose , & subcommand_help ) ;
487489 }
488490
489491 Subcommand :: Clean { all : matches. opt_present ( "all" ) }
@@ -494,12 +496,12 @@ Arguments:
494496 "run" | "r" => {
495497 if paths. is_empty ( ) {
496498 println ! ( "\n run requires at least a path!\n " ) ;
497- usage ( 1 , & opts, & subcommand_help , & extra_help ) ;
499+ usage ( 1 , & opts, verbose , & subcommand_help ) ;
498500 }
499501 Subcommand :: Run { paths }
500502 }
501503 _ => {
502- usage ( 1 , & opts, & subcommand_help , & extra_help ) ;
504+ usage ( 1 , & opts, verbose , & subcommand_help ) ;
503505 }
504506 } ;
505507
0 commit comments