@@ -45,6 +45,8 @@ use workspace::determine_destination;
4545use context:: { Context , BuildContext ,
4646 RustcFlags , Trans , Link , Nothing , Pretty , Analysis , Assemble ,
4747 LLVMAssemble , LLVMCompileBitcode } ;
48+ use context:: { Command , BuildCmd , CleanCmd , DoCmd , InfoCmd , InstallCmd , ListCmd ,
49+ PreferCmd , TestCmd , InitCmd , UninstallCmd , UnpreferCmd } ;
4850use crate_id:: CrateId ;
4951use package_source:: PkgSrc ;
5052use target:: { WhatToBuild , Everything , is_lib, is_main, is_test, is_bench} ;
@@ -205,7 +207,7 @@ impl<'a> PkgScript<'a> {
205207}
206208
207209pub trait CtxMethods {
208- fn run ( & self , cmd : & str , args : ~[ ~str ] ) ;
210+ fn run ( & self , cmd : Command , args : ~[ ~str ] ) ;
209211 fn do_cmd ( & self , _cmd : & str , _pkgname : & str ) ;
210212 /// Returns a pair of the selected package ID, and the destination workspace
211213 fn build_args ( & self , args : ~[ ~str ] , what : & WhatToBuild ) -> Option < ( CrateId , Path ) > ;
@@ -281,13 +283,13 @@ impl CtxMethods for BuildContext {
281283 Some ( ( crateid, dest_ws) )
282284 }
283285 }
284- fn run ( & self , cmd : & str , args : ~[ ~str ] ) {
286+ fn run ( & self , cmd : Command , args : ~[ ~str ] ) {
285287 let cwd = os:: getcwd ( ) ;
286288 match cmd {
287- "build" => {
289+ BuildCmd => {
288290 self . build_args ( args, & WhatToBuild :: new ( MaybeCustom , Everything ) ) ;
289291 }
290- "clean" => {
292+ CleanCmd => {
291293 if args. len ( ) < 1 {
292294 match cwd_to_workspace ( ) {
293295 None => { usage:: clean ( ) ; return }
@@ -304,17 +306,17 @@ impl CtxMethods for BuildContext {
304306 self . clean ( & cwd, & crateid) ; // tjc: should use workspace, not cwd
305307 }
306308 }
307- "do" => {
309+ DoCmd => {
308310 if args. len ( ) < 2 {
309311 return usage:: do_cmd ( ) ;
310312 }
311313
312314 self . do_cmd ( args[ 0 ] . clone ( ) , args[ 1 ] . clone ( ) ) ;
313315 }
314- "info" => {
316+ InfoCmd => {
315317 self . info ( ) ;
316318 }
317- "install" => {
319+ InstallCmd => {
318320 if args. len ( ) < 1 {
319321 match cwd_to_workspace ( ) {
320322 None if dir_has_crate_file ( & cwd) => {
@@ -360,21 +362,21 @@ impl CtxMethods for BuildContext {
360362 }
361363 }
362364 }
363- "list" => {
365+ ListCmd => {
364366 println ( "Installed packages:" ) ;
365367 installed_packages:: list_installed_packages ( |pkg_id| {
366368 pkg_id. path . display ( ) . with_str ( |s| println ( s) ) ;
367369 true
368370 } ) ;
369371 }
370- "prefer" => {
372+ PreferCmd => {
371373 if args. len ( ) < 1 {
372374 return usage:: uninstall ( ) ;
373375 }
374376
375377 self . prefer ( args[ 0 ] , None ) ;
376378 }
377- "test" => {
379+ TestCmd => {
378380 // Build the test executable
379381 let maybe_id_and_workspace = self . build_args ( args,
380382 & WhatToBuild :: new ( MaybeCustom , Tests ) ) ;
@@ -388,14 +390,14 @@ impl CtxMethods for BuildContext {
388390 }
389391 }
390392 }
391- "init" => {
393+ InitCmd => {
392394 if args. len ( ) != 0 {
393395 return usage:: init ( ) ;
394396 } else {
395397 self . init ( ) ;
396398 }
397399 }
398- "uninstall" => {
400+ UninstallCmd => {
399401 if args. len ( ) < 1 {
400402 return usage:: uninstall ( ) ;
401403 }
@@ -417,14 +419,13 @@ impl CtxMethods for BuildContext {
417419 } ) ;
418420 }
419421 }
420- "unprefer" => {
422+ UnpreferCmd => {
421423 if args. len ( ) < 1 {
422424 return usage:: unprefer ( ) ;
423425 }
424426
425427 self . unprefer ( args[ 0 ] , None ) ;
426428 }
427- _ => fail ! ( "I don't know the command `{}`" , cmd)
428429 }
429430 }
430431
@@ -864,38 +865,19 @@ pub fn main_args(args: &[~str]) -> int {
864865 experimental_features : experimental_features
865866 } ;
866867
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) {
875870 None => {
876871 usage:: general ( ) ;
877872 return 0 ;
878873 }
879874 Some ( cmd) => {
880875 let bad_option = context:: flags_forbidden_for_cmd ( & rustc_flags,
881876 cfgs,
882- * cmd,
877+ cmd,
883878 user_supplied_opt_level) ;
884879 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) ;
899881 if bad_option {
900882 return BAD_FLAG_CODE ;
901883 }
@@ -909,9 +891,10 @@ pub fn main_args(args: &[~str]) -> int {
909891 } ;
910892
911893 // 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 ( ) ;
915898 remaining_args. shift ( ) ;
916899 let sroot = match supplied_sysroot {
917900 Some ( s) => Path :: new ( s) ,
@@ -923,7 +906,6 @@ pub fn main_args(args: &[~str]) -> int {
923906 debug ! ( "Will store workcache in {}" , ws. display( ) ) ;
924907
925908 let rm_args = remaining_args. clone ( ) ;
926- let sub_cmd = cmd. clone ( ) ;
927909 // Wrap the rest in task::try in case of a condition failure in a task
928910 let result = do task:: try {
929911 BuildContext {
@@ -935,7 +917,7 @@ pub fn main_args(args: &[~str]) -> int {
935917 } ,
936918 workcache_context : api:: default_context ( sroot. clone ( ) ,
937919 default_workspace ( ) ) . workcache_context
938- } . run ( sub_cmd , rm_args. clone ( ) )
920+ } . run ( command , rm_args. clone ( ) )
939921 } ;
940922 // FIXME #9262: This is using the same error code for all errors,
941923 // and at least one test case succeeds if rustpkg returns COPY_FAILED_CODE,
0 commit comments