@@ -98,7 +98,6 @@ impl Default for Subcommand {
98
98
99
99
impl Flags {
100
100
pub fn parse ( args : & [ String ] ) -> Flags {
101
- let mut extra_help = String :: new ( ) ;
102
101
let mut subcommand_help = String :: from (
103
102
"\
104
103
Usage: x.py <subcommand> [options] [<paths>...]
@@ -170,16 +169,6 @@ To learn more about a subcommand, run `./x.py <subcommand> -h`",
170
169
"VALUE" ,
171
170
) ;
172
171
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
-
183
172
// We can't use getopt to parse the options until we have completed specifying which
184
173
// options are valid, but under the current implementation, some options are conditional on
185
174
// 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`",
263
252
_ => { }
264
253
} ;
265
254
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
+
266
280
// Done specifying what options are possible, so do the getopts parsing
267
281
let matches = opts. parse ( & args[ ..] ) . unwrap_or_else ( |e| {
268
282
// Invalid argument/option format
269
283
println ! ( "\n {}\n " , e) ;
270
- usage ( 1 , & opts, & subcommand_help , & extra_help ) ;
284
+ usage ( 1 , & opts, false , & subcommand_help ) ;
271
285
} ) ;
286
+
272
287
// Extra sanity check to make sure we didn't hit this crazy corner case:
273
288
//
274
289
// ./x.py --frobulate clean build
@@ -436,24 +451,11 @@ Arguments:
436
451
let paths = matches. free [ 1 ..] . iter ( ) . map ( |p| p. into ( ) ) . collect :: < Vec < PathBuf > > ( ) ;
437
452
438
453
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" ) ;
453
455
454
456
// User passed in -h/--help?
455
457
if matches. opt_present ( "help" ) {
456
- usage ( 0 , & opts, & subcommand_help , & extra_help ) ;
458
+ usage ( 0 , & opts, verbose , & subcommand_help ) ;
457
459
}
458
460
459
461
let cmd = match subcommand. as_str ( ) {
@@ -483,7 +485,7 @@ Arguments:
483
485
"clean" => {
484
486
if !paths. is_empty ( ) {
485
487
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 ) ;
487
489
}
488
490
489
491
Subcommand :: Clean { all : matches. opt_present ( "all" ) }
@@ -494,12 +496,12 @@ Arguments:
494
496
"run" | "r" => {
495
497
if paths. is_empty ( ) {
496
498
println ! ( "\n run requires at least a path!\n " ) ;
497
- usage ( 1 , & opts, & subcommand_help , & extra_help ) ;
499
+ usage ( 1 , & opts, verbose , & subcommand_help ) ;
498
500
}
499
501
Subcommand :: Run { paths }
500
502
}
501
503
_ => {
502
- usage ( 1 , & opts, & subcommand_help , & extra_help ) ;
504
+ usage ( 1 , & opts, verbose , & subcommand_help ) ;
503
505
}
504
506
} ;
505
507
0 commit comments