fix: more information when -p value is absent

This commit is contained in:
Weihang Lo 2020-10-27 22:03:54 +08:00
parent 4d5a56d7d0
commit fa522ccec1
No known key found for this signature in database
GPG Key ID: D7DBF189825E82E7
3 changed files with 27 additions and 20 deletions

View File

@ -502,8 +502,9 @@ pub trait ArgMatchesExt {
// As for cargo 0.50.0, this won't occur but if someone sneaks in // As for cargo 0.50.0, this won't occur but if someone sneaks in
// we can still provide this informative message for them. // we can still provide this informative message for them.
anyhow::bail!( anyhow::bail!(
"\"--package <SPEC>\" requires a SPEC format value.\n\ "\"--package <SPEC>\" requires a SPEC format value, \n\
Run `cargo help pkgid` for more infomation about SPEC format." which can be any package ID specifier in the dependency graph.\n\
Run `cargo help pkgid` for more information about SPEC format."
) )
} }

View File

@ -39,17 +39,12 @@ fn print_available_targets(
let mut output = String::new(); let mut output = String::new();
writeln!(output, "\"{}\" takes one argument.", option_name)?; writeln!(output, "\"{}\" takes one argument.", option_name)?;
print_availables(output, &targets, plural_name) if targets.is_empty() {
}
fn print_availables(output: String, availables: &[&str], plural_name: &str) -> CargoResult<()> {
let mut output = output;
if availables.is_empty() {
writeln!(output, "No {} available.", plural_name)?; writeln!(output, "No {} available.", plural_name)?;
} else { } else {
writeln!(output, "Available {}:", plural_name)?; writeln!(output, "Available {}:", plural_name)?;
for available in availables { for target in targets {
writeln!(output, " {}", available)?; writeln!(output, " {}", target)?;
} }
} }
bail!("{}", output) bail!("{}", output)
@ -61,15 +56,24 @@ pub fn print_available_packages(ws: &Workspace<'_>) -> CargoResult<()> {
.map(|pkg| pkg.name().as_str()) .map(|pkg| pkg.name().as_str())
.collect::<Vec<_>>(); .collect::<Vec<_>>();
let mut output = String::new(); let mut output = "\"--package <SPEC>\" requires a SPEC format value, \
writeln!( which can be any package ID specifier in the dependency graph.\n\
output, Run `cargo help pkgid` for more information about SPEC format.\n\n"
"\"--package <SPEC>\" requires a SPEC format value.\n\ .to_string();
Run `cargo help pkgid` for more infomation about SPEC format."
)?;
print_availables(output, &packages, "packages") if packages.is_empty() {
// This would never happen.
// Just in case something regresses we covers it here.
writeln!(output, "No packages available.")?;
} else {
writeln!(output, "Possible packages/workspace members:")?;
for package in packages {
writeln!(output, " {}", package)?;
} }
}
bail!("{}", output)
}
pub fn print_available_examples(ws: &Workspace<'_>, options: &CompileOptions) -> CargoResult<()> { pub fn print_available_examples(ws: &Workspace<'_>, options: &CompileOptions) -> CargoResult<()> {
print_available_targets(Target::is_example, ws, options, "--example", "examples") print_available_targets(Target::is_example, ws, options, "--example", "examples")

View File

@ -88,9 +88,11 @@ Available tests:
.cargo(&format!("{} -p", command)) .cargo(&format!("{} -p", command))
.with_stderr( .with_stderr(
"\ "\
[ERROR] \"--package <SPEC>\" requires a SPEC format value. [ERROR] \"--package <SPEC>\" requires a SPEC format value, \
Run `cargo help pkgid` for more infomation about SPEC format. which can be any package ID specifier in the dependency graph.
Available packages: Run `cargo help pkgid` for more information about SPEC format.
Possible packages/workspace members:
foo foo
", ",