Skip to content

Commit 139247e

Browse files
committed
feat: list availables for rustc,rustdoc,run,pkgid
1 parent fa522cc commit 139247e

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

src/bin/cargo/commands/pkgid.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use crate::command_prelude::*;
22

33
use cargo::ops;
4+
use cargo::util::print_available_packages;
45

56
pub fn cli() -> App {
67
subcommand("pkgid")
@@ -14,7 +15,12 @@ pub fn cli() -> App {
1415

1516
pub fn exec(config: &mut Config, args: &ArgMatches<'_>) -> CliResult {
1617
let ws = args.workspace(config)?;
17-
let spec = args.value_of("spec").or_else(|| args.value_of("package"));
18+
if !args.is_present("spec") && args.is_present_with_zero_values("package") {
19+
print_available_packages(&ws)?
20+
}
21+
let spec = args.value_of("spec").or_else(|| {
22+
args.value_of("package")
23+
});
1824
let spec = ops::pkgid(&ws, spec)?;
1925
cargo::drop_println!(config, "{}", spec);
2026
Ok(())

src/cargo/util/command_prelude.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ pub trait AppExt: Sized {
5656
}
5757

5858
fn arg_package(self, package: &'static str) -> Self {
59-
self._arg(opt("package", package).short("p").value_name("SPEC"))
59+
self._arg(optinal_opt("package", package).short("p").value_name("SPEC"))
6060
}
6161

6262
fn arg_jobs(self) -> Self {
@@ -220,6 +220,10 @@ pub fn opt(name: &'static str, help: &'static str) -> Arg<'static, 'static> {
220220
Arg::with_name(name).long(name).help(help)
221221
}
222222

223+
pub fn optinal_opt(name: &'static str, help: &'static str) -> Arg<'static, 'static> {
224+
opt(name, help).min_values(0)
225+
}
226+
223227
pub fn optional_multi_opt(
224228
name: &'static str,
225229
value_name: &'static str,
@@ -502,7 +506,7 @@ pub trait ArgMatchesExt {
502506
// As for cargo 0.50.0, this won't occur but if someone sneaks in
503507
// we can still provide this informative message for them.
504508
anyhow::bail!(
505-
"\"--package <SPEC>\" requires a SPEC format value, \n\
509+
"\"--package <SPEC>\" requires a SPEC format value, \
506510
which can be any package ID specifier in the dependency graph.\n\
507511
Run `cargo help pkgid` for more information about SPEC format."
508512
)

0 commit comments

Comments
 (0)