@@ -99,8 +99,6 @@ impl Default for Subcommand {
99
99
100
100
impl Flags {
101
101
pub fn parse ( args : & [ String ] ) -> Flags {
102
- dbg ! ( args) ;
103
- let mut extra_help = String :: new ( ) ;
104
102
let mut subcommand_help = String :: from (
105
103
"\
106
104
Usage: x.py <subcommand> [options] [<paths>...]
@@ -172,16 +170,6 @@ To learn more about a subcommand, run `./x.py <subcommand> -h`",
172
170
"VALUE" ,
173
171
) ;
174
172
175
- // fn usage()
176
- let usage =
177
- |exit_code : i32 , opts : & Options , subcommand_help : & str , extra_help : & str | -> ! {
178
- println ! ( "{}" , opts. usage( subcommand_help) ) ;
179
- if !extra_help. is_empty ( ) {
180
- println ! ( "{}" , extra_help) ;
181
- }
182
- process:: exit ( exit_code) ;
183
- } ;
184
-
185
173
// We can't use getopt to parse the options until we have completed specifying which
186
174
// options are valid, but under the current implementation, some options are conditional on
187
175
// the subcommand. Therefore we must manually identify the subcommand first, so that we can
@@ -265,12 +253,38 @@ To learn more about a subcommand, run `./x.py <subcommand> -h`",
265
253
_ => { }
266
254
} ;
267
255
256
+ // fn usage()
257
+ let usage = |exit_code : i32 , opts : & Options , verbose : bool , subcommand_help : & str | -> ! {
258
+ let mut extra_help = String :: new ( ) ;
259
+
260
+ // All subcommands except `clean` can have an optional "Available paths" section
261
+ if verbose {
262
+ let config = Config :: parse ( & [ "build" . to_string ( ) ] ) ;
263
+ let build = Build :: new ( config) ;
264
+
265
+ let maybe_rules_help = Builder :: get_help ( & build, subcommand. as_str ( ) ) ;
266
+ extra_help. push_str ( maybe_rules_help. unwrap_or_default ( ) . as_str ( ) ) ;
267
+ } else if !( subcommand. as_str ( ) == "clean" || subcommand. as_str ( ) == "fmt" ) {
268
+ extra_help. push_str (
269
+ format ! ( "Run `./x.py {} -h -v` to see a list of available paths." , subcommand)
270
+ . as_str ( ) ,
271
+ ) ;
272
+ }
273
+
274
+ println ! ( "{}" , opts. usage( subcommand_help) ) ;
275
+ if !extra_help. is_empty ( ) {
276
+ println ! ( "{}" , extra_help) ;
277
+ }
278
+ process:: exit ( exit_code) ;
279
+ } ;
280
+
268
281
// Done specifying what options are possible, so do the getopts parsing
269
282
let matches = opts. parse ( & args[ ..] ) . unwrap_or_else ( |e| {
270
283
// Invalid argument/option format
271
284
println ! ( "\n {}\n " , e) ;
272
- usage ( 1 , & opts, & subcommand_help , & extra_help ) ;
285
+ usage ( 1 , & opts, false , & subcommand_help ) ;
273
286
} ) ;
287
+
274
288
// Extra sanity check to make sure we didn't hit this crazy corner case:
275
289
//
276
290
// ./x.py --frobulate clean build
@@ -438,24 +452,11 @@ Arguments:
438
452
let paths = matches. free [ 1 ..] . iter ( ) . map ( |p| p. into ( ) ) . collect :: < Vec < PathBuf > > ( ) ;
439
453
440
454
let cfg_file = env:: var_os ( "BOOTSTRAP_CONFIG" ) . map ( PathBuf :: from) ;
441
-
442
- // All subcommands except `clean` can have an optional "Available paths" section
443
- if matches. opt_present ( "verbose" ) {
444
- let config = Config :: parse ( & [ "build" . to_string ( ) ] ) ;
445
- let build = Build :: new ( config) ;
446
-
447
- let maybe_rules_help = Builder :: get_help ( & build, subcommand. as_str ( ) ) ;
448
- extra_help. push_str ( maybe_rules_help. unwrap_or_default ( ) . as_str ( ) ) ;
449
- } else if !( subcommand. as_str ( ) == "clean" || subcommand. as_str ( ) == "fmt" ) {
450
- extra_help. push_str (
451
- format ! ( "Run `./x.py {} -h -v` to see a list of available paths." , subcommand)
452
- . as_str ( ) ,
453
- ) ;
454
- }
455
+ let verbose = matches. opt_present ( "verbose" ) ;
455
456
456
457
// User passed in -h/--help?
457
458
if matches. opt_present ( "help" ) {
458
- usage ( 0 , & opts, & subcommand_help , & extra_help ) ;
459
+ usage ( 0 , & opts, verbose , & subcommand_help ) ;
459
460
}
460
461
461
462
let cmd = match subcommand. as_str ( ) {
@@ -485,7 +486,7 @@ Arguments:
485
486
"clean" => {
486
487
if !paths. is_empty ( ) {
487
488
println ! ( "\n clean does not take a path argument\n " ) ;
488
- usage ( 1 , & opts, & subcommand_help , & extra_help ) ;
489
+ usage ( 1 , & opts, verbose , & subcommand_help ) ;
489
490
}
490
491
491
492
Subcommand :: Clean { all : matches. opt_present ( "all" ) }
@@ -496,12 +497,12 @@ Arguments:
496
497
"run" | "r" => {
497
498
if paths. is_empty ( ) {
498
499
println ! ( "\n run requires at least a path!\n " ) ;
499
- usage ( 1 , & opts, & subcommand_help , & extra_help ) ;
500
+ usage ( 1 , & opts, verbose , & subcommand_help ) ;
500
501
}
501
502
Subcommand :: Run { paths }
502
503
}
503
504
_ => {
504
- usage ( 1 , & opts, & subcommand_help , & extra_help ) ;
505
+ usage ( 1 , & opts, verbose , & subcommand_help ) ;
505
506
}
506
507
} ;
507
508
0 commit comments