@@ -113,31 +113,28 @@ To learn more about a subcommand, run `./x.py <subcommand> -h`");
113
113
process:: exit ( exit_code) ;
114
114
} ;
115
115
116
- // Get subcommand
117
- let matches = opts. parse ( & args[ ..] ) . unwrap_or_else ( |e| {
118
- // Invalid argument/option format
119
- println ! ( "\n {}\n " , e) ;
120
- usage ( 1 , & opts, & subcommand_help, & extra_help) ;
121
- } ) ;
122
- let subcommand = match matches. free . get ( 0 ) {
123
- Some ( s) => { s } ,
124
- None => {
125
- // No subcommand -- lets only show the general usage and subcommand help in this case.
116
+ // We can't use getopt to parse the options until we have completed specifying which
117
+ // options are valid, but under the current implementation, some options are conditional on
118
+ // the subcommand. Therefore we must manually identify the subcommand first, so that we can
119
+ // complete the definition of the options. Then we can use the getopt::Matches object from
120
+ // there on out.
121
+ let mut possible_subcommands = args. iter ( ) . collect :: < Vec < _ > > ( ) ;
122
+ possible_subcommands. retain ( |& s| !s. starts_with ( '-' ) ) ;
123
+ let subcommand = match possible_subcommands. first ( ) {
124
+ Some ( s) => s,
125
+ None => {
126
+ // No subcommand -- show the general usage and subcommand help
126
127
println ! ( "{}\n " , subcommand_help) ;
127
128
process:: exit ( 0 ) ;
128
129
}
129
130
} ;
130
131
131
- // Get any optional paths which occur after the subcommand
132
- let cwd = t ! ( env:: current_dir( ) ) ;
133
- let paths = matches. free [ 1 ..] . iter ( ) . map ( |p| cwd. join ( p) ) . collect :: < Vec < _ > > ( ) ;
134
-
135
132
// Some subcommands have specific arguments help text
136
133
match subcommand. as_str ( ) {
137
134
"build" => {
138
135
subcommand_help. push_str ( "\n
139
136
Arguments:
140
- This subcommand accepts a number of paths to directories to the crates
137
+ This subcommand accepts a number of paths to directories to the crates
141
138
and/or artifacts to compile. For example:
142
139
143
140
./x.py build src/libcore
@@ -195,6 +192,17 @@ Arguments:
195
192
_ => { }
196
193
} ;
197
194
195
+ // Done specifying what options are possible, so do the getopts parsing
196
+ let matches = opts. parse ( & args[ ..] ) . unwrap_or_else ( |e| {
197
+ // Invalid argument/option format
198
+ println ! ( "\n {}\n " , e) ;
199
+ usage ( 1 , & opts, & subcommand_help, & extra_help) ;
200
+ } ) ;
201
+ // Get any optional paths which occur after the subcommand
202
+ let cwd = t ! ( env:: current_dir( ) ) ;
203
+ let paths = matches. free [ 1 ..] . iter ( ) . map ( |p| cwd. join ( p) ) . collect :: < Vec < _ > > ( ) ;
204
+
205
+
198
206
// All subcommands can have an optional "Available paths" section
199
207
if matches. opt_present ( "verbose" ) {
200
208
let flags = Flags :: parse ( & [ "build" . to_string ( ) ] ) ;
0 commit comments