Skip to content

Commit 46fa867

Browse files
committed
Auto merge of rust-lang#9848 - nipunn1313:desc, r=alexcrichton
Show desc of well known subcommands (fmt, clippy) in cargo --list Fixes rust-lang#8680 An approach to rust-lang#8680 that shows these in `cargo --list` without showing them directly in the `cargo --help`. ``` ➜ cargo git:(desc) target/debug/cargo --list | grep clippy clippy Checks a package to catch common mistakes and improve your Rust code. ``` Here's what mine looks like visually now: ![image](https://user-images.githubusercontent.com/1300387/131178775-2255ef0d-1993-47dd-bc73-9015394b967c.png)
2 parents f559c10 + 1edd863 commit 46fa867

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

src/bin/cargo/cli.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,23 @@ use cargo::core::{features, CliUnstable};
33
use cargo::{self, drop_print, drop_println, CliResult, Config};
44
use clap::{AppSettings, Arg, ArgMatches};
55
use itertools::Itertools;
6+
use std::collections::HashMap;
67

78
use super::commands;
89
use super::list_commands;
910
use crate::command_prelude::*;
1011
use cargo::core::features::HIDDEN;
1112

13+
lazy_static::lazy_static! {
14+
// Maps from commonly known external commands (not builtin to cargo) to their
15+
// description, for the help page. Reserved for external subcommands that are
16+
// core within the rust ecosystem (esp ones that might become internal in the future).
17+
static ref KNOWN_EXTERNAL_COMMAND_DESCRIPTIONS: HashMap<&'static str, &'static str> = vec![
18+
("clippy", "Checks a package to catch common mistakes and improve your Rust code."),
19+
("fmt", "Formats all bin and lib files of the current crate using rustfmt."),
20+
].into_iter().collect();
21+
}
22+
1223
pub fn main(config: &mut Config) -> CliResult {
1324
// CAUTION: Be careful with using `config` until it is configured below.
1425
// In general, try to avoid loading config values unless necessary (like
@@ -100,14 +111,22 @@ Run with 'cargo -Z [FLAG] [SUBCOMMAND]'",
100111
if args.is_present("list") {
101112
drop_println!(config, "Installed Commands:");
102113
for (name, command) in list_commands(config) {
114+
let known_external_desc = KNOWN_EXTERNAL_COMMAND_DESCRIPTIONS.get(name.as_str());
103115
match command {
104116
CommandInfo::BuiltIn { about } => {
117+
assert!(
118+
known_external_desc.is_none(),
119+
"KNOWN_EXTERNAL_COMMANDS shouldn't contain builtin \"{}\"",
120+
name
121+
);
105122
let summary = about.unwrap_or_default();
106123
let summary = summary.lines().next().unwrap_or(&summary); // display only the first line
107124
drop_println!(config, " {:<20} {}", name, summary);
108125
}
109126
CommandInfo::External { path } => {
110-
if is_verbose {
127+
if let Some(desc) = known_external_desc {
128+
drop_println!(config, " {:<20} {}", name, desc);
129+
} else if is_verbose {
111130
drop_println!(config, " {:<20} {}", name, path.display());
112131
} else {
113132
drop_println!(config, " {}", name);

tests/testsuite/cargo_command.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,31 @@ fn list_command_looks_at_path() {
101101
);
102102
}
103103

104+
#[cargo_test]
105+
fn list_command_handles_known_external_commands() {
106+
let p = project()
107+
.executable(Path::new("path-test").join("cargo-fmt"), "")
108+
.build();
109+
110+
let fmt_desc = " fmt Formats all bin and lib files of the current crate using rustfmt.";
111+
112+
// Without path - fmt isn't there
113+
p.cargo("--list")
114+
.env("PATH", "")
115+
.with_stdout_does_not_contain(fmt_desc)
116+
.run();
117+
118+
// With path - fmt is there with known description
119+
let mut path = path();
120+
path.push(p.root().join("path-test"));
121+
let path = env::join_paths(path.iter()).unwrap();
122+
123+
p.cargo("--list")
124+
.env("PATH", &path)
125+
.with_stdout_contains(fmt_desc)
126+
.run();
127+
}
128+
104129
#[cargo_test]
105130
fn list_command_resolves_symlinks() {
106131
let proj = project()

0 commit comments

Comments
 (0)