@@ -119,7 +119,13 @@ To learn more about a subcommand, run `./x.py <subcommand> -h`");
119
119
// complete the definition of the options. Then we can use the getopt::Matches object from
120
120
// there on out.
121
121
let mut possible_subcommands = args. iter ( ) . collect :: < Vec < _ > > ( ) ;
122
- possible_subcommands. retain ( |& s| !s. starts_with ( '-' ) ) ;
122
+ possible_subcommands. retain ( |& s|
123
+ ( s == "build" )
124
+ || ( s == "test" )
125
+ || ( s == "bench" )
126
+ || ( s == "doc" )
127
+ || ( s == "clean" )
128
+ || ( s == "dist" ) ) ;
123
129
let subcommand = match possible_subcommands. first ( ) {
124
130
Some ( s) => s,
125
131
None => {
@@ -129,7 +135,43 @@ To learn more about a subcommand, run `./x.py <subcommand> -h`");
129
135
}
130
136
} ;
131
137
132
- // Some subcommands have specific arguments help text
138
+ // Some subcommands get extra options
139
+ match subcommand. as_str ( ) {
140
+ "test" => opts. optmulti ( "" , "test-args" , "extra arguments" , "ARGS" ) ,
141
+ "bench" => opts. optmulti ( "" , "test-args" , "extra arguments" , "ARGS" ) ,
142
+ "dist" => opts. optflag ( "" , "install" , "run installer as well" ) ,
143
+ _ => { }
144
+ } ;
145
+
146
+ // Done specifying what options are possible, so do the getopts parsing
147
+ let matches = opts. parse ( & args[ ..] ) . unwrap_or_else ( |e| {
148
+ // Invalid argument/option format
149
+ println ! ( "\n {}\n " , e) ;
150
+ usage ( 1 , & opts, & subcommand_help, & extra_help) ;
151
+ } ) ;
152
+ // Extra sanity check to make sure we didn't hit this crazy corner case:
153
+ //
154
+ // ./x.py --frobulate clean build
155
+ // ^-- option ^ ^- actual subcommand
156
+ // \_ arg to option could be mistaken as subcommand
157
+ let mut pass_sanity_check = true ;
158
+ match matches. free . get ( 0 ) {
159
+ Some ( check_subcommand) => {
160
+ if & check_subcommand != subcommand {
161
+ pass_sanity_check = false ;
162
+ }
163
+ } ,
164
+ None => {
165
+ pass_sanity_check = false ;
166
+ }
167
+ }
168
+ if !pass_sanity_check {
169
+ println ! ( "{}\n " , subcommand_help) ;
170
+ println ! ( "Sorry, I couldn't figure out which subcommand you were trying to specify.\n \
171
+ You may need to move some options to after the subcommand.\n ") ;
172
+ process:: exit ( 1 ) ;
173
+ }
174
+ // Extra help text for some commands
133
175
match subcommand. as_str ( ) {
134
176
"build" => {
135
177
subcommand_help. push_str ( "\n
@@ -152,7 +194,6 @@ Arguments:
152
194
./x.py build --stage 1 src/libtest" ) ;
153
195
}
154
196
"test" => {
155
- opts. optmulti ( "" , "test-args" , "extra arguments" , "ARGS" ) ;
156
197
subcommand_help. push_str ( "\n
157
198
Arguments:
158
199
This subcommand accepts a number of paths to directories to tests that
@@ -168,9 +209,6 @@ Arguments:
168
209
./x.py test
169
210
./x.py test --stage 1" ) ;
170
211
}
171
- "bench" => {
172
- opts. optmulti ( "" , "test-args" , "extra arguments" , "ARGS" ) ;
173
- }
174
212
"doc" => {
175
213
subcommand_help. push_str ( "\n
176
214
Arguments:
@@ -186,18 +224,8 @@ Arguments:
186
224
./x.py doc
187
225
./x.py doc --stage 1" ) ;
188
226
}
189
- "dist" => {
190
- opts. optflag ( "" , "install" , "run installer as well" ) ;
191
- }
192
227
_ => { }
193
228
} ;
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
229
// Get any optional paths which occur after the subcommand
202
230
let cwd = t ! ( env:: current_dir( ) ) ;
203
231
let paths = matches. free [ 1 ..] . iter ( ) . map ( |p| cwd. join ( p) ) . collect :: < Vec < _ > > ( ) ;
0 commit comments