5
5
6
6
import rustc:: syntax:: { ast, codemap} ;
7
7
import rustc:: syntax:: parse:: parser;
8
- import rustc:: util:: filesearch:: { get_cargo_root, get_cargo_root_nearest} ;
8
+ import rustc:: util:: filesearch:: { get_cargo_root, get_cargo_root_nearest,
9
+ get_cargo_sysroot} ;
9
10
import rustc:: driver:: diagnostic;
10
11
11
12
import std:: fs;
@@ -20,7 +21,7 @@ import str;
20
21
import std:: tempfile;
21
22
import vec;
22
23
import std:: getopts;
23
- import getopts:: { optflag, opt_present} ;
24
+ import getopts:: { optflag, optopt , opt_present} ;
24
25
25
26
enum _src {
26
27
/* Break cycles in package <-> source */
@@ -69,12 +70,14 @@ type pkg = {
69
70
70
71
type options = {
71
72
test : bool ,
72
- cwd : bool ,
73
+ mode : mode ,
73
74
free : [ str ] ,
74
75
} ;
75
76
77
+ enum mode { system_mode, user_mode, local_mode }
78
+
76
79
fn opts ( ) -> [ getopts:: opt ] {
77
- [ optflag ( "g" ) , optflag ( "global " ) , optflag ( "test" ) ]
80
+ [ optflag ( "g" ) , optflag ( "G" ) , optopt ( "mode ") , optflag ( "test" ) ]
78
81
}
79
82
80
83
fn info ( msg : str ) {
@@ -343,21 +346,40 @@ fn build_cargo_options(argv: [str]) -> options {
343
346
} ;
344
347
345
348
let test = opt_present ( match , "test" ) ;
346
- let cwd = !( opt_present( match , "g" ) || opt_present( match , "global" ) ) ;
349
+ let mode = if opt_present ( match , "G" ) {
350
+ if opt_present( match , "mode" ) { fail "--mode and -G both provided" ; }
351
+ if opt_present( match, "g" ) { fail "-G and -g both provided" ; }
352
+ system_mode
353
+ } else if opt_present( match , "g" ) {
354
+ if opt_present( match, "mode" ) { fail "--mode and -g both provided" ; }
355
+ if opt_present( match, "G" ) { fail "-G and -g both provided" ; }
356
+ user_mode
357
+ } else if opt_present ( match , "mode" ) {
358
+ alt getopts:: opt_str( match, "mode" ) {
359
+ "system" { system_mode }
360
+ "user" { user_mode }
361
+ "local" { local_mode }
362
+ _ { fail "argument to `mode` must be one of `system`" +
363
+ ", `user`, or `normal`" ;
364
+ }
365
+ }
366
+ } else {
367
+ local_mode
368
+ } ;
347
369
348
- { test: test, cwd : cwd , free: match . free}
370
+ { test: test, mode : mode , free: match . free }
349
371
}
350
372
351
373
fn configure( opts: options) -> cargo {
352
- let get_cargo_dir = if opts. cwd {
353
- get_cargo_root_nearest
354
- } else {
355
- get_cargo_root
374
+ let get_cargo_dir = alt opts. mode {
375
+ system_mode { get_cargo_sysroot }
376
+ user_mode { get_cargo_root }
377
+ local_mode { get_cargo_root_nearest }
356
378
} ;
357
379
358
380
let p = alt get_cargo_dir( ) {
359
- result:: ok( p) { p }
360
- result:: err( e) { fail e }
381
+ result:: ok ( p) { p }
382
+ result:: err ( e) { fail e }
361
383
} ;
362
384
363
385
let sources = map:: new_str_hash :: < source > ( ) ;
@@ -736,6 +758,8 @@ fn cmd_init(c: cargo) {
736
758
}
737
759
info ( #fmt[ "signature ok for sources.json" ] ) ;
738
760
run:: run_program ( "cp" , [ srcfile, destsrcfile] ) ;
761
+
762
+ info ( #fmt[ "Initialized .cargo in %s" , c. root ] ) ;
739
763
}
740
764
741
765
fn print_pkg ( s : source , p : package ) {
@@ -775,14 +799,28 @@ fn cmd_search(c: cargo) {
775
799
}
776
800
777
801
fn cmd_usage ( ) {
778
- print ( "Usage: cargo <verb> [args...]" ) ;
779
- print ( " init Set up .cargo" ) ;
780
- print ( " install [--test] [source/]package-name Install by name" ) ;
781
- print ( " install [--test] uuid:[source/]package-uuid Install by uuid" ) ;
782
- print ( " list [source] List packages" ) ;
783
- print ( " search <name | '*'> [tags...] Search packages" ) ;
784
- print ( " sync Sync all sources" ) ;
785
- print ( " usage This" ) ;
802
+ print ( "Usage: cargo <verb> [options] [args...]" +
803
+ "
804
+
805
+ init Set up .cargo
806
+ install [--test] [source/]package-name Install by name
807
+ install [--test] uuid:[source/]package-uuid Install by uuid
808
+ list [source] List packages
809
+ search <name | '*'> [tags...] Search packages
810
+ sync Sync all sources
811
+ usage This
812
+
813
+ Options:
814
+
815
+ --mode=[system,user,local] change mode as (system/user/local)
816
+ -g equivalent to --mode=user
817
+ -G equivalent to --mode=system
818
+
819
+ NOTE:
820
+ This command creates/uses local-level .cargo by default.
821
+ To create/use user-level .cargo, use option -g/--mode=user.
822
+ To create/use system-level .cargo, use option -G/--mode=system.
823
+ " ) ;
786
824
}
787
825
788
826
fn main ( argv : [ str ] ) {
0 commit comments