5
5
6
6
import rustc:: syntax:: { ast, codemap} ;
7
7
import rustc:: syntax:: parse:: parser;
8
- import rustc:: util:: filesearch:: get_cargo_root;
8
+ import rustc:: util:: filesearch:: { get_cargo_root, get_cargo_root_nearest } ;
9
9
import rustc:: driver:: diagnostic;
10
10
11
11
import std:: fs;
@@ -19,6 +19,8 @@ import std::run;
19
19
import str;
20
20
import std:: tempfile;
21
21
import vec;
22
+ import std:: getopts;
23
+ import getopts:: { optflag, opt_present} ;
22
24
23
25
enum _src {
24
26
/* Break cycles in package <-> source */
@@ -53,7 +55,7 @@ type cargo = {
53
55
workdir : str ,
54
56
sourcedir : str ,
55
57
sources : map:: hashmap < str , source > ,
56
- mutable test : bool
58
+ opts : options
57
59
} ;
58
60
59
61
type pkg = {
@@ -65,6 +67,16 @@ type pkg = {
65
67
crate_type : option < str >
66
68
} ;
67
69
70
+ type options = {
71
+ test : bool ,
72
+ cwd : bool ,
73
+ free : [ str ] ,
74
+ } ;
75
+
76
+ fn opts ( ) -> [ getopts:: opt ] {
77
+ [ optflag ( "g" ) , optflag ( "global" ) , optflag ( "test" ) ]
78
+ }
79
+
68
80
fn info ( msg : str ) {
69
81
io:: stdout ( ) . write_line ( "info: " + msg) ;
70
82
}
@@ -322,8 +334,28 @@ fn load_source_packages(&c: cargo, &src: source) {
322
334
} ;
323
335
}
324
336
325
- fn configure ( ) -> cargo {
326
- let p = alt get_cargo_root ( ) {
337
+ fn build_cargo_options( argv : [ str ] ) -> options {
338
+ let match = alt getopts:: getopts ( argv, opts ( ) ) {
339
+ result:: ok ( m) { m }
340
+ result:: err ( f) {
341
+ fail #fmt[ "%s" , getopts:: fail_str ( f) ] ;
342
+ }
343
+ } ;
344
+
345
+ let test = opt_present ( match , "test" ) ;
346
+ let cwd = !( opt_present( match , "g" ) || opt_present( match , "global" ) ) ;
347
+
348
+ { test: test, cwd: cwd, free: match . free}
349
+ }
350
+
351
+ fn configure ( opts: options) -> cargo {
352
+ let get_cargo_dir = if opts. cwd {
353
+ get_cargo_root_nearest
354
+ } else {
355
+ get_cargo_root
356
+ } ;
357
+
358
+ let p = alt get_cargo_dir ( ) {
327
359
result:: ok( p) { p }
328
360
result:: err( e) { fail e }
329
361
} ;
@@ -339,7 +371,7 @@ fn configure() -> cargo {
339
371
workdir: fs:: connect ( p, "work" ) ,
340
372
sourcedir: fs:: connect ( p, "sources" ) ,
341
373
sources: sources,
342
- mutable test : false
374
+ opts : opts
343
375
} ;
344
376
345
377
need_dir( c. root ) ;
@@ -430,7 +462,7 @@ fn install_source(c: cargo, path: str) {
430
462
alt p {
431
463
none { cont; }
432
464
some ( _p) {
433
- if c. test {
465
+ if c. opts . test {
434
466
test_one_crate ( c, path, cf, _p) ;
435
467
}
436
468
install_one_crate ( c, path, cf, _p) ;
@@ -573,19 +605,14 @@ fn install_named_specific(c: cargo, wd: str, src: str, name: str) {
573
605
error ( "Can't find package " + src + "/" + name) ;
574
606
}
575
607
576
- fn cmd_install ( c : cargo , argv : [ str ] ) unsafe {
608
+ fn cmd_install ( c : cargo ) unsafe {
577
609
// cargo install <pkg>
578
- if vec:: len ( argv ) < 3 u {
610
+ if vec:: len ( c . opts . free ) < 3 u {
579
611
cmd_usage ( ) ;
580
612
ret;
581
613
}
582
614
583
- let target = argv[ 2 ] ;
584
- // TODO: getopts
585
- if vec:: len ( argv) > 3 u && argv[ 2 ] == "--test" {
586
- c. test = true ;
587
- target = argv[ 3 ] ;
588
- }
615
+ let target = c. opts . free [ 2 ] ;
589
616
590
617
let wd = alt tempfile:: mkdtemp ( c. workdir + fs:: path_sep ( ) , "" ) {
591
618
some ( _wd) { _wd }
@@ -671,9 +698,9 @@ fn sync_one(c: cargo, name: str, src: source) {
671
698
run:: run_program ( "cp" , [ pkgfile, destpkgfile] ) ;
672
699
}
673
700
674
- fn cmd_sync ( c : cargo , argv : [ str ] ) {
675
- if vec:: len ( argv ) == 3 u {
676
- sync_one ( c, argv [ 2 ] , c. sources . get ( argv [ 2 ] ) ) ;
701
+ fn cmd_sync ( c : cargo ) {
702
+ if vec:: len ( c . opts . free ) == 3 u {
703
+ sync_one ( c, c . opts . free [ 2 ] , c. sources . get ( c . opts . free [ 2 ] ) ) ;
677
704
} else {
678
705
cargo_suggestion ( c, true , { || } ) ;
679
706
c. sources . items { |k, v|
@@ -721,22 +748,22 @@ fn print_pkg(s: source, p: package) {
721
748
print ( " >> " + p. description + "\n " )
722
749
}
723
750
}
724
- fn cmd_list ( c : cargo , argv : [ str ] ) {
751
+ fn cmd_list ( c : cargo ) {
725
752
for_each_package ( c, { |s, p|
726
- if vec:: len ( argv ) <= 2 u || argv [ 2 ] == s. name {
753
+ if vec:: len ( c . opts . free ) <= 2 u || c . opts . free [ 2 ] == s. name {
727
754
print_pkg ( s, p) ;
728
755
}
729
756
} ) ;
730
757
}
731
758
732
- fn cmd_search ( c : cargo , argv : [ str ] ) {
733
- if vec:: len ( argv ) < 3 u {
759
+ fn cmd_search ( c : cargo ) {
760
+ if vec:: len ( c . opts . free ) < 3 u {
734
761
cmd_usage ( ) ;
735
762
ret;
736
763
}
737
764
let n = 0 ;
738
- let name = argv [ 2 ] ;
739
- let tags = vec:: slice ( argv , 3 u, vec:: len ( argv ) ) ;
765
+ let name = c . opts . free [ 2 ] ;
766
+ let tags = vec:: slice ( c . opts . free , 3 u, vec:: len ( c . opts . free ) ) ;
740
767
for_each_package ( c, { |s, p|
741
768
if ( str:: contains ( p. name , name) || name == "*" ) &&
742
769
vec:: all ( tags, { |t| vec:: member ( t, p. tags ) } ) {
@@ -749,7 +776,7 @@ fn cmd_search(c: cargo, argv: [str]) {
749
776
750
777
fn cmd_usage ( ) {
751
778
print ( "Usage: cargo <verb> [args...]" ) ;
752
- print ( " init Set up ~/ .cargo" ) ;
779
+ print ( " init Set up .cargo" ) ;
753
780
print ( " install [--test] [source/]package-name Install by name" ) ;
754
781
print ( " install [--test] uuid:[source/]package-uuid Install by uuid" ) ;
755
782
print ( " list [source] List packages" ) ;
@@ -759,17 +786,21 @@ fn cmd_usage() {
759
786
}
760
787
761
788
fn main ( argv : [ str ] ) {
762
- if vec:: len ( argv) < 2 u {
789
+ let o = build_cargo_options ( argv) ;
790
+
791
+ if vec:: len ( o. free ) < 2 u {
763
792
cmd_usage ( ) ;
764
793
ret;
765
794
}
766
- let c = configure ( ) ;
767
- alt argv[ 1 ] {
795
+
796
+ let c = configure ( o) ;
797
+
798
+ alt o. free [ 1 ] {
768
799
"init" { cmd_init ( c) ; }
769
- "install" { cmd_install ( c, argv ) ; }
770
- "list" { cmd_list ( c, argv ) ; }
771
- "search" { cmd_search ( c, argv ) ; }
772
- "sync" { cmd_sync ( c, argv ) ; }
800
+ "install" { cmd_install ( c) ; }
801
+ "list" { cmd_list ( c) ; }
802
+ "search" { cmd_search ( c) ; }
803
+ "sync" { cmd_sync ( c) ; }
773
804
"usage" { cmd_usage ( ) ; }
774
805
_ { cmd_usage( ) ; }
775
806
}
0 commit comments