mirror of
https://github.com/rust-lang/cargo.git
synced 2025-10-01 11:30:39 +00:00
Auto merge of #8675 - weihanglo:fix/name-help, r=Eh2406
Add --name suggestion for cargo new Resolves #8613 Since `check_name` have already got a parameter to show name help, I reuse the logic and sync the behavior between `cargo init` and `cargo new`. The divergence seems to be intentionally made in #7959: _...Only print the --name suggestion for `cargo init`._ Feel free to discuss.
This commit is contained in:
commit
2c10f2611f
@ -154,7 +154,19 @@ fn get_name<'a>(path: &'a Path, opts: &'a NewOptions) -> CargoResult<&'a str> {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn check_name(name: &str, name_help: &str, has_bin: bool, shell: &mut Shell) -> CargoResult<()> {
|
fn check_name(
|
||||||
|
name: &str,
|
||||||
|
show_name_help: bool,
|
||||||
|
has_bin: bool,
|
||||||
|
shell: &mut Shell,
|
||||||
|
) -> CargoResult<()> {
|
||||||
|
// If --name is already used to override, no point in suggesting it
|
||||||
|
// again as a fix.
|
||||||
|
let name_help = if show_name_help {
|
||||||
|
"\nIf you need a crate name to not match the directory name, consider using --name flag."
|
||||||
|
} else {
|
||||||
|
""
|
||||||
|
};
|
||||||
restricted_names::validate_package_name(name, "crate name", name_help)?;
|
restricted_names::validate_package_name(name, "crate name", name_help)?;
|
||||||
|
|
||||||
if restricted_names::is_keyword(name) {
|
if restricted_names::is_keyword(name) {
|
||||||
@ -363,7 +375,12 @@ pub fn new(opts: &NewOptions, config: &Config) -> CargoResult<()> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let name = get_name(path, opts)?;
|
let name = get_name(path, opts)?;
|
||||||
check_name(name, "", opts.kind.is_bin(), &mut config.shell())?;
|
check_name(
|
||||||
|
name,
|
||||||
|
opts.name.is_none(),
|
||||||
|
opts.kind.is_bin(),
|
||||||
|
&mut config.shell(),
|
||||||
|
)?;
|
||||||
|
|
||||||
let mkopts = MkOptions {
|
let mkopts = MkOptions {
|
||||||
version_control: opts.version_control,
|
version_control: opts.version_control,
|
||||||
@ -411,13 +428,7 @@ pub fn init(opts: &NewOptions, config: &Config) -> CargoResult<()> {
|
|||||||
// user may mean "initialize for library, but also add binary target"
|
// user may mean "initialize for library, but also add binary target"
|
||||||
}
|
}
|
||||||
let has_bin = src_paths_types.iter().any(|x| x.bin);
|
let has_bin = src_paths_types.iter().any(|x| x.bin);
|
||||||
// If --name is already used to override, no point in suggesting it
|
check_name(name, opts.name.is_none(), has_bin, &mut config.shell())?;
|
||||||
// again as a fix.
|
|
||||||
let name_help = match opts.name {
|
|
||||||
Some(_) => "",
|
|
||||||
None => "\nuse --name to override crate name",
|
|
||||||
};
|
|
||||||
check_name(name, name_help, has_bin, &mut config.shell())?;
|
|
||||||
|
|
||||||
let mut version_control = opts.version_control;
|
let mut version_control = opts.version_control;
|
||||||
|
|
||||||
|
@ -305,7 +305,8 @@ fn invalid_dir_name() {
|
|||||||
.with_stderr(
|
.with_stderr(
|
||||||
"\
|
"\
|
||||||
[ERROR] invalid character `.` in crate name: `foo.bar`, [..]
|
[ERROR] invalid character `.` in crate name: `foo.bar`, [..]
|
||||||
use --name to override crate name",
|
If you need a crate name to not match the directory name, consider using --name flag.
|
||||||
|
",
|
||||||
)
|
)
|
||||||
.run();
|
.run();
|
||||||
|
|
||||||
@ -323,7 +324,7 @@ fn reserved_name() {
|
|||||||
.with_stderr(
|
.with_stderr(
|
||||||
"\
|
"\
|
||||||
[ERROR] the name `test` cannot be used as a crate name, it conflicts [..]\n\
|
[ERROR] the name `test` cannot be used as a crate name, it conflicts [..]\n\
|
||||||
use --name to override crate name
|
If you need a crate name to not match the directory name, consider using --name flag.
|
||||||
",
|
",
|
||||||
)
|
)
|
||||||
.run();
|
.run();
|
||||||
|
@ -116,7 +116,12 @@ fn existing() {
|
|||||||
fn invalid_characters() {
|
fn invalid_characters() {
|
||||||
cargo_process("new foo.rs")
|
cargo_process("new foo.rs")
|
||||||
.with_status(101)
|
.with_status(101)
|
||||||
.with_stderr("[ERROR] invalid character `.` in crate name: `foo.rs`, [..]")
|
.with_stderr(
|
||||||
|
"\
|
||||||
|
[ERROR] invalid character `.` in crate name: `foo.rs`, [..]
|
||||||
|
If you need a crate name to not match the directory name, consider using --name flag.
|
||||||
|
",
|
||||||
|
)
|
||||||
.run();
|
.run();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -124,7 +129,12 @@ fn invalid_characters() {
|
|||||||
fn reserved_name() {
|
fn reserved_name() {
|
||||||
cargo_process("new test")
|
cargo_process("new test")
|
||||||
.with_status(101)
|
.with_status(101)
|
||||||
.with_stderr("[ERROR] the name `test` cannot be used as a crate name, it conflicts [..]")
|
.with_stderr(
|
||||||
|
"\
|
||||||
|
[ERROR] the name `test` cannot be used as a crate name, it conflicts [..]
|
||||||
|
If you need a crate name to not match the directory name, consider using --name flag.
|
||||||
|
",
|
||||||
|
)
|
||||||
.run();
|
.run();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -133,7 +143,10 @@ fn reserved_binary_name() {
|
|||||||
cargo_process("new --bin incremental")
|
cargo_process("new --bin incremental")
|
||||||
.with_status(101)
|
.with_status(101)
|
||||||
.with_stderr(
|
.with_stderr(
|
||||||
"[ERROR] the name `incremental` cannot be used as a crate name, it conflicts [..]",
|
"\
|
||||||
|
[ERROR] the name `incremental` cannot be used as a crate name, it conflicts [..]
|
||||||
|
If you need a crate name to not match the directory name, consider using --name flag.
|
||||||
|
",
|
||||||
)
|
)
|
||||||
.run();
|
.run();
|
||||||
|
|
||||||
@ -153,7 +166,12 @@ it conflicts with cargo's build directory names
|
|||||||
fn keyword_name() {
|
fn keyword_name() {
|
||||||
cargo_process("new pub")
|
cargo_process("new pub")
|
||||||
.with_status(101)
|
.with_status(101)
|
||||||
.with_stderr("[ERROR] the name `pub` cannot be used as a crate name, it is a Rust keyword")
|
.with_stderr(
|
||||||
|
"\
|
||||||
|
[ERROR] the name `pub` cannot be used as a crate name, it is a Rust keyword
|
||||||
|
If you need a crate name to not match the directory name, consider using --name flag.
|
||||||
|
",
|
||||||
|
)
|
||||||
.run();
|
.run();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -522,7 +540,12 @@ fn restricted_windows_name() {
|
|||||||
cargo_process("new nul")
|
cargo_process("new nul")
|
||||||
.env("USER", "foo")
|
.env("USER", "foo")
|
||||||
.with_status(101)
|
.with_status(101)
|
||||||
.with_stderr("[ERROR] cannot use name `nul`, it is a reserved Windows filename")
|
.with_stderr(
|
||||||
|
"\
|
||||||
|
[ERROR] cannot use name `nul`, it is a reserved Windows filename
|
||||||
|
If you need a crate name to not match the directory name, consider using --name flag.
|
||||||
|
",
|
||||||
|
)
|
||||||
.run();
|
.run();
|
||||||
} else {
|
} else {
|
||||||
cargo_process("new nul")
|
cargo_process("new nul")
|
||||||
@ -559,8 +582,11 @@ fn non_ascii_name_invalid() {
|
|||||||
.env("USER", "foo")
|
.env("USER", "foo")
|
||||||
.with_status(101)
|
.with_status(101)
|
||||||
.with_stderr(
|
.with_stderr(
|
||||||
"[ERROR] invalid character `Ⓐ` in crate name: `ⒶⒷⒸ`, \
|
"\
|
||||||
the first character must be a Unicode XID start character (most letters or `_`)",
|
[ERROR] invalid character `Ⓐ` in crate name: `ⒶⒷⒸ`, \
|
||||||
|
the first character must be a Unicode XID start character (most letters or `_`)
|
||||||
|
If you need a crate name to not match the directory name, consider using --name flag.
|
||||||
|
",
|
||||||
)
|
)
|
||||||
.run();
|
.run();
|
||||||
|
|
||||||
@ -568,8 +594,11 @@ fn non_ascii_name_invalid() {
|
|||||||
.env("USER", "foo")
|
.env("USER", "foo")
|
||||||
.with_status(101)
|
.with_status(101)
|
||||||
.with_stderr(
|
.with_stderr(
|
||||||
"[ERROR] invalid character `¼` in crate name: `a¼`, \
|
"\
|
||||||
characters must be Unicode XID characters (numbers, `-`, `_`, or most letters)",
|
[ERROR] invalid character `¼` in crate name: `a¼`, \
|
||||||
|
characters must be Unicode XID characters (numbers, `-`, `_`, or most letters)
|
||||||
|
If you need a crate name to not match the directory name, consider using --name flag.
|
||||||
|
",
|
||||||
)
|
)
|
||||||
.run();
|
.run();
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user