Skip to content

Commit b0896a0

Browse files
authored
feat(tree): Add more native completions (#16296)
### What does this PR try to resolve? This adds `--prune SPEC` and `--edges KIND` completions ### How to test and review this PR?
2 parents 8e43074 + 8a186b8 commit b0896a0

File tree

3 files changed

+79
-71
lines changed

3 files changed

+79
-71
lines changed

src/bin/cargo/commands/tree.rs

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,21 @@ pub fn cli() -> Command {
2222
.arg_silent_suggestion()
2323
.arg(flag("no-dev-dependencies", "Deprecated, use -e=no-dev instead").hide(true))
2424
.arg(
25-
multi_opt(
26-
"edges",
27-
"KINDS",
28-
"The kinds of dependencies to display \
29-
(features, normal, build, dev, all, \
30-
no-normal, no-build, no-dev, no-proc-macro)",
31-
)
32-
.short('e'),
25+
multi_opt("edges", "KINDS", "The kinds of dependencies to display")
26+
.short('e')
27+
.value_delimiter(',')
28+
.value_parser([
29+
"all",
30+
"normal",
31+
"build",
32+
"dev",
33+
"features",
34+
"public",
35+
"no-normal",
36+
"no-build",
37+
"no-dev",
38+
"no-proc-macro",
39+
]),
3340
)
3441
.arg(
3542
optional_multi_opt(
@@ -42,11 +49,16 @@ pub fn cli() -> Command {
4249
get_pkg_id_spec_candidates,
4350
)),
4451
)
45-
.arg(multi_opt(
46-
"prune",
47-
"SPEC",
48-
"Prune the given package from the display of the dependency tree",
49-
))
52+
.arg(
53+
multi_opt(
54+
"prune",
55+
"SPEC",
56+
"Prune the given package from the display of the dependency tree",
57+
)
58+
.add(clap_complete::ArgValueCandidates::new(
59+
get_pkg_id_spec_candidates,
60+
)),
61+
)
5062
.arg(opt("depth", "Maximum display depth of the dependency tree").value_name("DEPTH"))
5163
.arg(flag("no-indent", "Deprecated, use --prefix=none instead").hide(true))
5264
.arg(flag("prefix-depth", "Deprecated, use --prefix=depth instead").hide(true))
@@ -266,7 +278,7 @@ fn parse_edge_kinds(
266278
let mut kinds = args.get_many::<String>("edges").map_or_else(
267279
|| Vec::new(),
268280
|es| {
269-
es.flat_map(|e| e.split(','))
281+
es.map(|e| e.as_str())
270282
.filter(|e| {
271283
if *e == "no-proc-macro" {
272284
no_proc_macro = true;
@@ -305,15 +317,6 @@ fn parse_edge_kinds(
305317
result.insert(EdgeKind::Dep(DepKind::Build));
306318
result.insert(EdgeKind::Dep(DepKind::Development));
307319
};
308-
let unknown = |k| {
309-
bail!(
310-
"unknown edge kind `{}`, valid values are \
311-
\"normal\", \"build\", \"dev\", \
312-
\"no-normal\", \"no-build\", \"no-dev\", \"no-proc-macro\", \
313-
\"features\", or \"all\"",
314-
k
315-
)
316-
};
317320
if kinds.iter().any(|k| k.starts_with("no-")) {
318321
insert_defaults(&mut result);
319322
for kind in &kinds {
@@ -330,7 +333,7 @@ fn parse_edge_kinds(
330333
kind
331334
)
332335
}
333-
k => return unknown(k),
336+
_ => unreachable!("`{kind}` was validated by clap"),
334337
};
335338
}
336339
return Ok((result, no_proc_macro, public));
@@ -353,7 +356,7 @@ fn parse_edge_kinds(
353356
"dev" => {
354357
result.insert(EdgeKind::Dep(DepKind::Development));
355358
}
356-
k => return unknown(k),
359+
_ => unreachable!("`{kind}` was validated by clap"),
357360
}
358361
}
359362
if kinds.len() == 1 && kinds[0] == "features" {

tests/testsuite/cargo_tree/deps.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1723,10 +1723,13 @@ fn unknown_edge_kind() {
17231723

17241724
p.cargo("tree -e unknown")
17251725
.with_stderr_data(str![[r#"
1726-
[ERROR] unknown edge kind `unknown`, valid values are "normal", "build", "dev", "no-normal", "no-build", "no-dev", "no-proc-macro", "features", or "all"
1726+
[ERROR] invalid value 'unknown' for '--edges <KINDS>'
1727+
[possible values: all, normal, build, dev, features, public, no-normal, no-build, no-dev, no-proc-macro]
1728+
1729+
For more information, try '--help'.
17271730
17281731
"#]])
1729-
.with_status(101)
1732+
.with_status(1)
17301733
.run();
17311734
}
17321735

0 commit comments

Comments
 (0)