Skip to content

Commit 2d26633

Browse files
committed
Auto merge of #8542 - CPerezz:aliases, r=ehuss
Display builtin aliases with `cargo --list` As stated in #8486 it would help to the discovery of the builtin aliases the facto of printing them with the `cargo --list` command. - Extracted the builtin aliases currently implemented to a separated `const`. - Make all of the functions that interact with these aliases point to that function. - Refactored the `list_commands` fn in order to include with the builtin and external commands, the builtin aliases that come with cargo by default. - Added a test that checks that the aliases that currently are builtin with cargo are indeed being printed with the rest of the commands when `cargo --list` is called. The output on my machine looks like this: ``` $ cargo --list Installed Commands: b alias: build bench Execute all benchmarks of a local package build Compile a local package and all of its dependencies c alias: check check Check a local package and all of its dependencies for errors clean Remove artifacts that cargo has generated in the past doc Build a package's documentation fetch Fetch dependencies of a package from the network fix Automatically fix lint warnings reported by rustc generate-lockfile Generate the lockfile for a package git-checkout This subcommand has been removed init Create a new cargo package in an existing directory install Install a Rust binary. Default location is $HOME/.cargo/bin locate-project Print a JSON representation of a Cargo.toml file's location login Save an api token from the registry locally. If token is not specified, it will be read from stdin. metadata Output the resolved dependencies of a package, the concrete used versions including overrides, in machine-readable format new Create a new cargo package at <path> owner Manage the owners of a crate on the registry package Assemble the local package into a distributable tarball pkgid Print a fully qualified package specification publish Upload a package to the registry r alias: run read-manifest Print a JSON representation of a Cargo.toml manifest. run Run a binary or example of the local package rustc Compile a package, and pass extra options to the compiler rustdoc Build a package's documentation, using specified custom flags. search Search packages in crates.io t alias: test test Execute all unit and integration tests and build examples of a local package tree Display a tree visualization of a dependency graph uninstall Remove a Rust binary update Update dependencies as recorded in the local lock file vendor Vendor all dependencies for a project locally verify-project Check correctness of crate manifest version Show version information yank Remove a pushed crate from the index clippy clippy clippy clippy flamegraph fmt fmt fmt fmt miri miri miri miri outdated tree ``` As discussed with @ehuss the `BTreeSet` enforces `Ord` therefore, the aliases get mixed with the commands since they're passed through the same function. It can be refactored to appear separately, but, the code will be more spread and now it's all in just one file (which I believe is easier to maintain and review). Closes #8486
2 parents cdbd9b7 + 2ae8df6 commit 2d26633

File tree

2 files changed

+39
-6
lines changed

2 files changed

+39
-6
lines changed

src/bin/cargo/main.rs

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,21 @@ fn main() {
4747
}
4848
}
4949

50+
/// Table for defining the aliases which come builtin in `Cargo`.
51+
/// The contents are structured as: `(alias, aliased_command, description)`.
52+
const BUILTIN_ALIASES: [(&str, &str, &str); 4] = [
53+
("b", "build", "alias: build"),
54+
("c", "check", "alias: check"),
55+
("r", "run", "alias: run"),
56+
("t", "test", "alias: test"),
57+
];
58+
59+
/// Function which contains the list of all of the builtin aliases and it's
60+
/// corresponding execs represented as &str.
61+
fn builtin_aliases_execs(cmd: &str) -> Option<&(&str, &str, &str)> {
62+
BUILTIN_ALIASES.iter().find(|alias| alias.0 == cmd)
63+
}
64+
5065
fn aliased_command(config: &Config, command: &str) -> CargoResult<Option<Vec<String>>> {
5166
let alias_name = format!("alias.{}", command);
5267
let user_alias = match config.get_string(&alias_name) {
@@ -60,12 +75,10 @@ fn aliased_command(config: &Config, command: &str) -> CargoResult<Option<Vec<Str
6075
Ok(None) => None,
6176
Err(_) => config.get::<Option<Vec<String>>>(&alias_name)?,
6277
};
63-
let result = user_alias.or_else(|| match command {
64-
"b" => Some(vec!["build".to_string()]),
65-
"c" => Some(vec!["check".to_string()]),
66-
"r" => Some(vec!["run".to_string()]),
67-
"t" => Some(vec!["test".to_string()]),
68-
_ => None,
78+
79+
let result = user_alias.or_else(|| match builtin_aliases_execs(command) {
80+
Some(command_str) => Some(vec![command_str.1.to_string()]),
81+
None => None,
6982
});
7083
Ok(result)
7184
}
@@ -106,6 +119,15 @@ fn list_commands(config: &Config) -> BTreeSet<CommandInfo> {
106119
});
107120
}
108121

122+
// Add the builtin_aliases and them descriptions to the
123+
// `commands` `BTreeSet`.
124+
for command in &BUILTIN_ALIASES {
125+
commands.insert(CommandInfo::BuiltIn {
126+
name: command.0.to_string(),
127+
about: Some(command.2.to_string()),
128+
});
129+
}
130+
109131
commands
110132
}
111133

tests/testsuite/cargo_command.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,17 @@ fn list_commands_with_descriptions() {
7878
.run();
7979
}
8080

81+
#[cargo_test]
82+
fn list_aliases_with_descriptions() {
83+
let p = project().build();
84+
p.cargo("--list")
85+
.with_stdout_contains(" b alias: build")
86+
.with_stdout_contains(" c alias: check")
87+
.with_stdout_contains(" r alias: run")
88+
.with_stdout_contains(" t alias: test")
89+
.run();
90+
}
91+
8192
#[cargo_test]
8293
fn list_command_looks_at_path() {
8394
let proj = project().build();

0 commit comments

Comments
 (0)