Skip to content

Commit e8f37da

Browse files
committedDec 5, 2018
Fix built-in aliases taking arguments.
1 parent d27b47b commit e8f37da

File tree

8 files changed

+52
-59
lines changed

8 files changed

+52
-59
lines changed
 

‎src/bin/cargo/cli.rs

+6-12
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use clap::{AppSettings, Arg, ArgMatches};
44

55
use cargo::{self, CliResult, Config};
66

7-
use super::commands::{self, BuiltinExec};
7+
use super::commands;
88
use super::list_commands;
99
use command_prelude::*;
1010

@@ -107,28 +107,22 @@ fn expand_aliases(
107107
commands::builtin_exec(cmd),
108108
super::aliased_command(config, cmd)?,
109109
) {
110-
(
111-
Some(BuiltinExec {
112-
alias_for: None, ..
113-
}),
114-
Some(_),
115-
) => {
110+
(Some(_), Some(_)) => {
116111
// User alias conflicts with a built-in subcommand
117112
config.shell().warn(format!(
118113
"user-defined alias `{}` is ignored, because it is shadowed by a built-in command",
119114
cmd,
120115
))?;
121116
}
122-
(_, Some(mut user_alias)) => {
123-
// User alias takes precedence over built-in aliases
124-
user_alias.extend(
117+
(_, Some(mut alias)) => {
118+
alias.extend(
125119
args.values_of("")
126120
.unwrap_or_default()
127121
.map(|s| s.to_string()),
128122
);
129123
let args = cli()
130124
.setting(AppSettings::NoBinaryName)
131-
.get_matches_from_safe(user_alias)?;
125+
.get_matches_from_safe(alias)?;
132126
return expand_aliases(config, args);
133127
}
134128
(_, None) => {}
@@ -165,7 +159,7 @@ fn execute_subcommand(config: &mut Config, args: &ArgMatches) -> CliResult {
165159
.unwrap_or_default(),
166160
)?;
167161

168-
if let Some(BuiltinExec { exec, .. }) = commands::builtin_exec(cmd) {
162+
if let Some(exec) = commands::builtin_exec(cmd) {
169163
return exec(config, subcommand_args);
170164
}
171165

‎src/bin/cargo/commands/build.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use cargo::ops;
44

55
pub fn cli() -> App {
66
subcommand("build")
7-
// subcommand aliases are handled in commands::builtin_exec() and cli::expand_aliases()
7+
// subcommand aliases are handled in aliased_command()
88
// .alias("b")
99
.about("Compile a local package and all of its dependencies")
1010
.arg_package_spec(

‎src/bin/cargo/commands/check.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use cargo::ops;
44

55
pub fn cli() -> App {
66
subcommand("check")
7-
// subcommand aliases are handled in commands::builtin_exec() and cli::expand_aliases()
7+
// subcommand aliases are handled in aliased_command()
88
// .alias("c")
99
.about("Check a local package and all of its dependencies for errors")
1010
.arg_package_spec(

‎src/bin/cargo/commands/mod.rs

+7-21
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,11 @@ pub fn builtin() -> Vec<App> {
3535
]
3636
}
3737

38-
pub struct BuiltinExec<'a> {
39-
pub exec: fn(&'a mut Config, &'a ArgMatches) -> CliResult,
40-
pub alias_for: Option<&'static str>,
41-
}
42-
43-
pub fn builtin_exec(cmd: &str) -> Option<BuiltinExec> {
44-
let exec = match cmd {
38+
pub fn builtin_exec(cmd: &str) -> Option<fn(&mut Config, &ArgMatches) -> CliResult> {
39+
let f = match cmd {
4540
"bench" => bench::exec,
46-
"build" | "b" => build::exec,
47-
"check" | "c" => check::exec,
41+
"build" => build::exec,
42+
"check" => check::exec,
4843
"clean" => clean::exec,
4944
"doc" => doc::exec,
5045
"fetch" => fetch::exec,
@@ -62,28 +57,19 @@ pub fn builtin_exec(cmd: &str) -> Option<BuiltinExec> {
6257
"pkgid" => pkgid::exec,
6358
"publish" => publish::exec,
6459
"read-manifest" => read_manifest::exec,
65-
"run" | "r" => run::exec,
60+
"run" => run::exec,
6661
"rustc" => rustc::exec,
6762
"rustdoc" => rustdoc::exec,
6863
"search" => search::exec,
69-
"test" | "t" => test::exec,
64+
"test" => test::exec,
7065
"uninstall" => uninstall::exec,
7166
"update" => update::exec,
7267
"verify-project" => verify_project::exec,
7368
"version" => version::exec,
7469
"yank" => yank::exec,
7570
_ => return None,
7671
};
77-
78-
let alias_for = match cmd {
79-
"b" => Some("build"),
80-
"c" => Some("check"),
81-
"r" => Some("run"),
82-
"t" => Some("test"),
83-
_ => None,
84-
};
85-
86-
Some(BuiltinExec { exec, alias_for })
72+
Some(f)
8773
}
8874

8975
pub mod bench;

‎src/bin/cargo/commands/run.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use cargo::ops::{self, CompileFilter};
55

66
pub fn cli() -> App {
77
subcommand("run")
8-
// subcommand aliases are handled in commands::builtin_exec() and cli::expand_aliases()
8+
// subcommand aliases are handled in aliased_command()
99
// .alias("r")
1010
.setting(AppSettings::TrailingVarArg)
1111
.about("Run the main binary of the local package (src/main.rs)")

‎src/bin/cargo/commands/test.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use cargo::ops::{self, CompileFilter};
44

55
pub fn cli() -> App {
66
subcommand("test")
7-
// subcommand aliases are handled in commands::builtin_exec() and cli::expand_aliases()
7+
// subcommand aliases are handled in aliased_command()
88
// .alias("t")
99
.setting(AppSettings::TrailingVarArg)
1010
.about("Execute all unit and integration tests of a local package")

‎src/bin/cargo/main.rs

+21-22
Original file line numberDiff line numberDiff line change
@@ -63,28 +63,27 @@ fn main() {
6363

6464
fn aliased_command(config: &Config, command: &str) -> CargoResult<Option<Vec<String>>> {
6565
let alias_name = format!("alias.{}", command);
66-
let mut result = Ok(None);
67-
match config.get_string(&alias_name) {
68-
Ok(value) => {
69-
if let Some(record) = value {
70-
let alias_commands = record
71-
.val
72-
.split_whitespace()
73-
.map(|s| s.to_string())
74-
.collect();
75-
result = Ok(Some(alias_commands));
76-
}
77-
}
78-
Err(_) => {
79-
let value = config.get_list(&alias_name)?;
80-
if let Some(record) = value {
81-
let alias_commands: Vec<String> =
82-
record.val.iter().map(|s| s.0.to_string()).collect();
83-
result = Ok(Some(alias_commands));
84-
}
85-
}
86-
}
87-
result
66+
let user_alias = match config.get_string(&alias_name) {
67+
Ok(Some(record)) => Some(
68+
record
69+
.val
70+
.split_whitespace()
71+
.map(|s| s.to_string())
72+
.collect(),
73+
),
74+
Ok(None) => None,
75+
Err(_) => config
76+
.get_list(&alias_name)?
77+
.map(|record| record.val.iter().map(|s| s.0.to_string()).collect()),
78+
};
79+
let result = user_alias.or_else(|| match command {
80+
"b" => Some(vec!["build".to_string()]),
81+
"c" => Some(vec!["check".to_string()]),
82+
"r" => Some(vec!["run".to_string()]),
83+
"t" => Some(vec!["test".to_string()]),
84+
_ => None,
85+
});
86+
Ok(result)
8887
}
8988

9089
/// List all runnable commands

‎tests/testsuite/cargo_alias_config.rs

+14
Original file line numberDiff line numberDiff line change
@@ -148,3 +148,17 @@ fn alias_override_builtin_alias() {
148148
",
149149
).run();
150150
}
151+
152+
#[test]
153+
fn builtin_alias_takes_options() {
154+
// #6381
155+
let p = project()
156+
.file("src/lib.rs", "")
157+
.file(
158+
"examples/ex1.rs",
159+
r#"fn main() { println!("{}", std::env::args().skip(1).next().unwrap()) }"#,
160+
)
161+
.build();
162+
163+
p.cargo("r --example ex1 -- asdf").with_stdout("asdf").run();
164+
}

0 commit comments

Comments
 (0)
Please sign in to comment.