@@ -45,6 +45,8 @@ use workspace::determine_destination;
45
45
use context:: { Context , BuildContext ,
46
46
RustcFlags , Trans , Link , Nothing , Pretty , Analysis , Assemble ,
47
47
LLVMAssemble , LLVMCompileBitcode } ;
48
+ use context:: { Command , BuildCmd , CleanCmd , DoCmd , InfoCmd , InstallCmd , ListCmd ,
49
+ PreferCmd , TestCmd , InitCmd , UninstallCmd , UnpreferCmd } ;
48
50
use crate_id:: CrateId ;
49
51
use package_source:: PkgSrc ;
50
52
use target:: { WhatToBuild , Everything , is_lib, is_main, is_test, is_bench} ;
@@ -205,7 +207,7 @@ impl<'a> PkgScript<'a> {
205
207
}
206
208
207
209
pub trait CtxMethods {
208
- fn run ( & self , cmd : & str , args : ~[ ~str ] ) ;
210
+ fn run ( & self , cmd : Command , args : ~[ ~str ] ) ;
209
211
fn do_cmd ( & self , _cmd : & str , _pkgname : & str ) ;
210
212
/// Returns a pair of the selected package ID, and the destination workspace
211
213
fn build_args ( & self , args : ~[ ~str ] , what : & WhatToBuild ) -> Option < ( CrateId , Path ) > ;
@@ -281,13 +283,13 @@ impl CtxMethods for BuildContext {
281
283
Some ( ( crateid, dest_ws) )
282
284
}
283
285
}
284
- fn run ( & self , cmd : & str , args : ~[ ~str ] ) {
286
+ fn run ( & self , cmd : Command , args : ~[ ~str ] ) {
285
287
let cwd = os:: getcwd ( ) ;
286
288
match cmd {
287
- "build" => {
289
+ BuildCmd => {
288
290
self . build_args ( args, & WhatToBuild :: new ( MaybeCustom , Everything ) ) ;
289
291
}
290
- "clean" => {
292
+ CleanCmd => {
291
293
if args. len ( ) < 1 {
292
294
match cwd_to_workspace ( ) {
293
295
None => { usage:: clean ( ) ; return }
@@ -304,17 +306,17 @@ impl CtxMethods for BuildContext {
304
306
self . clean ( & cwd, & crateid) ; // tjc: should use workspace, not cwd
305
307
}
306
308
}
307
- "do" => {
309
+ DoCmd => {
308
310
if args. len ( ) < 2 {
309
311
return usage:: do_cmd ( ) ;
310
312
}
311
313
312
314
self . do_cmd ( args[ 0 ] . clone ( ) , args[ 1 ] . clone ( ) ) ;
313
315
}
314
- "info" => {
316
+ InfoCmd => {
315
317
self . info ( ) ;
316
318
}
317
- "install" => {
319
+ InstallCmd => {
318
320
if args. len ( ) < 1 {
319
321
match cwd_to_workspace ( ) {
320
322
None if dir_has_crate_file ( & cwd) => {
@@ -360,21 +362,21 @@ impl CtxMethods for BuildContext {
360
362
}
361
363
}
362
364
}
363
- "list" => {
365
+ ListCmd => {
364
366
println ( "Installed packages:" ) ;
365
367
installed_packages:: list_installed_packages ( |pkg_id| {
366
368
pkg_id. path . display ( ) . with_str ( |s| println ( s) ) ;
367
369
true
368
370
} ) ;
369
371
}
370
- "prefer" => {
372
+ PreferCmd => {
371
373
if args. len ( ) < 1 {
372
374
return usage:: uninstall ( ) ;
373
375
}
374
376
375
377
self . prefer ( args[ 0 ] , None ) ;
376
378
}
377
- "test" => {
379
+ TestCmd => {
378
380
// Build the test executable
379
381
let maybe_id_and_workspace = self . build_args ( args,
380
382
& WhatToBuild :: new ( MaybeCustom , Tests ) ) ;
@@ -388,14 +390,14 @@ impl CtxMethods for BuildContext {
388
390
}
389
391
}
390
392
}
391
- "init" => {
393
+ InitCmd => {
392
394
if args. len ( ) != 0 {
393
395
return usage:: init ( ) ;
394
396
} else {
395
397
self . init ( ) ;
396
398
}
397
399
}
398
- "uninstall" => {
400
+ UninstallCmd => {
399
401
if args. len ( ) < 1 {
400
402
return usage:: uninstall ( ) ;
401
403
}
@@ -417,14 +419,13 @@ impl CtxMethods for BuildContext {
417
419
} ) ;
418
420
}
419
421
}
420
- "unprefer" => {
422
+ UnpreferCmd => {
421
423
if args. len ( ) < 1 {
422
424
return usage:: unprefer ( ) ;
423
425
}
424
426
425
427
self . unprefer ( args[ 0 ] , None ) ;
426
428
}
427
- _ => fail ! ( "I don't know the command `{}`" , cmd)
428
429
}
429
430
}
430
431
@@ -864,38 +865,19 @@ pub fn main_args(args: &[~str]) -> int {
864
865
experimental_features : experimental_features
865
866
} ;
866
867
867
- let mut cmd_opt = None ;
868
- for a in args. iter ( ) {
869
- if util:: is_cmd ( * a) {
870
- cmd_opt = Some ( a) ;
871
- break ;
872
- }
873
- }
874
- let cmd = match cmd_opt {
868
+ let cmd_opt = args. iter ( ) . filter_map ( |s| from_str ( s. clone ( ) ) ) . next ( ) ;
869
+ let command = match ( cmd_opt) {
875
870
None => {
876
871
usage:: general ( ) ;
877
872
return 0 ;
878
873
}
879
874
Some ( cmd) => {
880
875
let bad_option = context:: flags_forbidden_for_cmd ( & rustc_flags,
881
876
cfgs,
882
- * cmd,
877
+ cmd,
883
878
user_supplied_opt_level) ;
884
879
if help || bad_option {
885
- match * cmd {
886
- ~"build" => usage:: build ( ) ,
887
- ~"clean" => usage:: clean ( ) ,
888
- ~"do" => usage:: do_cmd ( ) ,
889
- ~"info" => usage:: info ( ) ,
890
- ~"install" => usage:: install ( ) ,
891
- ~"list" => usage:: list ( ) ,
892
- ~"prefer" => usage:: prefer ( ) ,
893
- ~"test" => usage:: test ( ) ,
894
- ~"init" => usage:: init ( ) ,
895
- ~"uninstall" => usage:: uninstall ( ) ,
896
- ~"unprefer" => usage:: unprefer ( ) ,
897
- _ => usage:: general ( )
898
- } ;
880
+ usage:: usage_for_command ( cmd) ;
899
881
if bad_option {
900
882
return BAD_FLAG_CODE ;
901
883
}
@@ -909,9 +891,10 @@ pub fn main_args(args: &[~str]) -> int {
909
891
} ;
910
892
911
893
// Pop off all flags, plus the command
912
- let remaining_args = args. iter ( ) . skip_while ( |s| !util:: is_cmd ( * * s) ) ;
913
- // I had to add this type annotation to get the code to typecheck
914
- let mut remaining_args: ~[ ~str ] = remaining_args. map ( |s| ( * s) . clone ( ) ) . collect ( ) ;
894
+ let mut remaining_args: ~[ ~str ] = args. iter ( ) . skip_while ( |& s| {
895
+ let maybe_command: Option < Command > = from_str ( * s) ;
896
+ maybe_command. is_none ( )
897
+ } ) . map ( |s| s. clone ( ) ) . collect ( ) ;
915
898
remaining_args. shift ( ) ;
916
899
let sroot = match supplied_sysroot {
917
900
Some ( s) => Path :: new ( s) ,
@@ -923,7 +906,6 @@ pub fn main_args(args: &[~str]) -> int {
923
906
debug ! ( "Will store workcache in {}" , ws. display( ) ) ;
924
907
925
908
let rm_args = remaining_args. clone ( ) ;
926
- let sub_cmd = cmd. clone ( ) ;
927
909
// Wrap the rest in task::try in case of a condition failure in a task
928
910
let result = do task:: try {
929
911
BuildContext {
@@ -935,7 +917,7 @@ pub fn main_args(args: &[~str]) -> int {
935
917
} ,
936
918
workcache_context : api:: default_context ( sroot. clone ( ) ,
937
919
default_workspace ( ) ) . workcache_context
938
- } . run ( sub_cmd , rm_args. clone ( ) )
920
+ } . run ( command , rm_args. clone ( ) )
939
921
} ;
940
922
// FIXME #9262: This is using the same error code for all errors,
941
923
// and at least one test case succeeds if rustpkg returns COPY_FAILED_CODE,
0 commit comments