@@ -53,25 +53,39 @@ fn show_error(msg: String) -> ! {
53
53
std:: process:: exit ( 1 )
54
54
}
55
55
56
- fn list_targets ( mut args : impl Iterator < Item =String > ) -> impl Iterator < Item =cargo_metadata:: Target > {
56
+ fn get_arg_flag_value ( name : & str ) -> Option < String > {
57
+ // stop searching at `--`
58
+ let mut args = std:: env:: args ( ) . skip_while ( |val| !( val. starts_with ( name) || val == "--" ) ) ;
59
+
60
+ match args. next ( ) {
61
+ Some ( ref p) if p == "--" => None ,
62
+ Some ( ref p) if p == name => args. next ( ) ,
63
+ Some ( p) => {
64
+ // Make sure this really starts with `$name=`, we didn't test for the `=` yet.
65
+ let v = & p[ name. len ( ) ..] ; // strip leading `$name`
66
+ if v. starts_with ( '=' ) {
67
+ Some ( v[ 1 ..] . to_owned ( ) ) // strip leading `=`
68
+ } else {
69
+ None
70
+ }
71
+ } ,
72
+ None => None ,
73
+ }
74
+ }
75
+
76
+ fn list_targets ( ) -> impl Iterator < Item =cargo_metadata:: Target > {
57
77
// We need to get the manifest, and then the metadata, to enumerate targets.
58
- let manifest_path_arg = args. find ( |val| {
59
- val. starts_with ( "--manifest-path=" )
60
- } ) ;
78
+ let manifest_path = get_arg_flag_value ( "--manifest-path" ) . map ( PathBuf :: from) ;
61
79
62
80
let mut metadata = if let Ok ( metadata) = cargo_metadata:: metadata (
63
- manifest_path_arg . as_ref ( ) . map ( AsRef :: as_ref) ,
81
+ manifest_path . as_ref ( ) . map ( AsRef :: as_ref) ,
64
82
)
65
83
{
66
84
metadata
67
85
} else {
68
86
show_error ( format ! ( "error: Could not obtain cargo metadata." ) ) ;
69
87
} ;
70
88
71
- let manifest_path = manifest_path_arg. map ( |arg| {
72
- PathBuf :: from ( Path :: new ( & arg[ "--manifest-path=" . len ( ) ..] ) )
73
- } ) ;
74
-
75
89
let current_dir = std:: env:: current_dir ( ) ;
76
90
77
91
let package_index = metadata
@@ -232,7 +246,7 @@ fn main() {
232
246
}
233
247
234
248
// Now run the command.
235
- for target in list_targets ( std :: env :: args ( ) . skip ( skip ) ) {
249
+ for target in list_targets ( ) {
236
250
let args = std:: env:: args ( ) . skip ( skip) ;
237
251
let kind = target. kind . get ( 0 ) . expect (
238
252
"badly formatted cargo metadata: target::kind is an empty array" ,
0 commit comments