Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Display builtin aliases with cargo --list #8542

Merged
merged 5 commits into from
Jul 28, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 29 additions & 6 deletions src/bin/cargo/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,25 @@ fn main() {
}
}

const BUILTIN_ALIASES: [(&str, &str); 4] = [
("b", "alias: build"),
("c", "alias: check"),
("r", "alias: run"),
("t", "alias: test"),
];

/// Function which contains the list of all of the builtin aliases and it's
/// corresponding execs represented as &str.
fn builtin_aliases_execs(cmd: &str) -> Option<&str> {
match cmd {
"b" => Some("build"),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of defining the set of aliases twice, perhaps just place them all in a single table? Like a 3-element tuple (alias, command, description), and then builtin_aliases_execs could just iterate over the array and find a match.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's brilliant!! Will modify it!!

"c" => Some("check"),
"r" => Some("run"),
"t" => Some("test"),
_ => None,
}
}

fn aliased_command(config: &Config, command: &str) -> CargoResult<Option<Vec<String>>> {
let alias_name = format!("alias.{}", command);
let user_alias = match config.get_string(&alias_name) {
Expand All @@ -60,12 +79,10 @@ fn aliased_command(config: &Config, command: &str) -> CargoResult<Option<Vec<Str
Ok(None) => None,
Err(_) => config.get::<Option<Vec<String>>>(&alias_name)?,
};
let result = user_alias.or_else(|| match command {
"b" => Some(vec!["build".to_string()]),
"c" => Some(vec!["check".to_string()]),
"r" => Some(vec!["run".to_string()]),
"t" => Some(vec!["test".to_string()]),
_ => None,

let result = user_alias.or_else(|| match builtin_aliases_execs(command) {
Some(command_str) => Some(vec![command_str.to_string()]),
None => None,
});
Ok(result)
}
Expand Down Expand Up @@ -105,6 +122,12 @@ fn list_commands(config: &Config) -> BTreeSet<CommandInfo> {
about: cmd.p.meta.about.map(|s| s.to_string()),
});
}
for command in &BUILTIN_ALIASES {
commands.insert(CommandInfo::BuiltIn {
name: command.0.to_string(),
about: Some(command.1.to_string()),
});
}

commands
}
Expand Down
11 changes: 11 additions & 0 deletions tests/testsuite/cargo_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,17 @@ fn list_commands_with_descriptions() {
.run();
}

#[cargo_test]
fn list_aliases_with_descriptions() {
let p = project().build();
p.cargo("--list")
.with_stdout_contains(" b alias: build")
.with_stdout_contains(" c alias: check")
.with_stdout_contains(" r alias: run")
.with_stdout_contains(" t alias: test")
.run();
}

#[cargo_test]
fn list_command_looks_at_path() {
let proj = project().build();
Expand Down