Skip to content

Commit c84c152

Browse files
committed
factor grabbing of cargo options into separate function and make it better
1 parent bccadeb commit c84c152

File tree

1 file changed

+24
-10
lines changed

1 file changed

+24
-10
lines changed

src/bin/cargo-miri.rs

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -53,25 +53,39 @@ fn show_error(msg: String) -> ! {
5353
std::process::exit(1)
5454
}
5555

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> {
5777
// 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);
6179

6280
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),
6482
)
6583
{
6684
metadata
6785
} else {
6886
show_error(format!("error: Could not obtain cargo metadata."));
6987
};
7088

71-
let manifest_path = manifest_path_arg.map(|arg| {
72-
PathBuf::from(Path::new(&arg["--manifest-path=".len()..]))
73-
});
74-
7589
let current_dir = std::env::current_dir();
7690

7791
let package_index = metadata
@@ -232,7 +246,7 @@ fn main() {
232246
}
233247

234248
// Now run the command.
235-
for target in list_targets(std::env::args().skip(skip)) {
249+
for target in list_targets() {
236250
let args = std::env::args().skip(skip);
237251
let kind = target.kind.get(0).expect(
238252
"badly formatted cargo metadata: target::kind is an empty array",

0 commit comments

Comments
 (0)