diff --git a/src/bin/cargo/commands/add.rs b/src/bin/cargo/commands/add.rs index 1c389f6082f..43e485b6742 100644 --- a/src/bin/cargo/commands/add.rs +++ b/src/bin/cargo/commands/add.rs @@ -1,4 +1,5 @@ use cargo::sources::CRATES_IO_REGISTRY; +use cargo::util::print_available_packages; use indexmap::IndexMap; use indexmap::IndexSet; @@ -73,14 +74,7 @@ Example uses: - Depend on crates with the same name from different registries"), ]) .arg_manifest_path() - .args([ - clap::Arg::new("package") - .short('p') - .long("package") - .action(ArgAction::Set) - .value_name("SPEC") - .help("Package to modify"), - ]) + .arg_package("Package to modify") .arg_quiet() .arg_dry_run("Don't actually write the manifest") .next_help_heading("Source") @@ -161,20 +155,31 @@ pub fn exec(config: &mut Config, args: &ArgMatches) -> CliResult { let section = parse_section(args); let ws = args.workspace(config)?; + + if args.is_present_with_zero_values("package") { + print_available_packages(&ws)?; + } + let packages = args.packages_from_flags()?; let packages = packages.get_packages(&ws)?; let spec = match packages.len() { 0 => { return Err(CliError::new( - anyhow::format_err!("no packages selected. Please specify one with `-p `"), + anyhow::format_err!( + "no packages selected to modify. Please specify one with `-p `" + ), 101, )); } 1 => packages[0], - len => { + _ => { + let names = packages.iter().map(|p| p.name()).collect::>(); return Err(CliError::new( anyhow::format_err!( - "{len} packages selected. Please specify one with `-p `", + "`cargo add` could not determine which package to modify. \ + Use the `--package` option to specify a package. \n\ + available packages: {}", + names.join(", ") ), 101, )); diff --git a/src/bin/cargo/commands/remove.rs b/src/bin/cargo/commands/remove.rs index c80889280d8..66be50024ad 100644 --- a/src/bin/cargo/commands/remove.rs +++ b/src/bin/cargo/commands/remove.rs @@ -4,6 +4,7 @@ use cargo::ops::cargo_remove::remove; use cargo::ops::cargo_remove::RemoveOptions; use cargo::ops::resolve_ws; use cargo::util::command_prelude::*; +use cargo::util::print_available_packages; use cargo::util::toml_mut::manifest::DepTable; use cargo::util::toml_mut::manifest::LocalManifest; use cargo::CargoResult; @@ -50,20 +51,31 @@ pub fn exec(config: &mut Config, args: &ArgMatches) -> CliResult { let dry_run = args.dry_run(); let workspace = args.workspace(config)?; + + if args.is_present_with_zero_values("package") { + print_available_packages(&workspace)?; + } + let packages = args.packages_from_flags()?; let packages = packages.get_packages(&workspace)?; let spec = match packages.len() { 0 => { return Err(CliError::new( - anyhow::format_err!("no packages selected. Please specify one with `-p `"), + anyhow::format_err!( + "no packages selected to modify. Please specify one with `-p `" + ), 101, )); } 1 => packages[0], - len => { + _ => { + let names = packages.iter().map(|p| p.name()).collect::>(); return Err(CliError::new( anyhow::format_err!( - "{len} packages selected. Please specify one with `-p `", + "`cargo remove` could not determine which package to modify. \ + Use the `--package` option to specify a package. \n\ + available packages: {}", + names.join(", ") ), 101, )); diff --git a/tests/testsuite/cargo_remove/invalid_package_multiple/stderr.log b/tests/testsuite/cargo_remove/invalid_package_multiple/stderr.log index b18ca3b8b0e..8a03c9e5b05 100644 --- a/tests/testsuite/cargo_remove/invalid_package_multiple/stderr.log +++ b/tests/testsuite/cargo_remove/invalid_package_multiple/stderr.log @@ -1 +1,2 @@ -error: 2 packages selected. Please specify one with `-p ` +error: `cargo remove` could not determine which package to modify. Use the `--package` option to specify a package. +available packages: dep-a, dep-b