Merge pull request #3909 from emersonford/fix-mut-subcommand-lifetime

fix: loosen lifetime constraint on mut_subcommand
This commit is contained in:
Ed Page 2022-07-11 21:45:01 -05:00 committed by GitHub
commit 3802a35a43
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 2 deletions

View file

@ -298,10 +298,10 @@ impl<'help> App<'help> {
/// assert!(res.is_ok());
/// ```
#[must_use]
pub fn mut_subcommand<T, F>(mut self, subcmd_id: T, f: F) -> Self
pub fn mut_subcommand<'a, T, F>(mut self, subcmd_id: T, f: F) -> Self
where
F: FnOnce(App<'help>) -> App<'help>,
T: Into<&'help str>,
T: Into<&'a str>,
{
let subcmd_id: &str = subcmd_id.into();
let id = Id::from(subcmd_id);

View file

@ -445,6 +445,25 @@ fn mut_subcommand_all() {
);
}
#[test]
fn mut_subcommand_with_alias_resolve() {
let mut cmd =
Command::new("foo").subcommand(Command::new("bar").alias("baz").about("test subcmd"));
assert_eq!(
cmd.find_subcommand("baz").unwrap().get_about().unwrap(),
"test subcmd"
);
let true_name = cmd.find_subcommand("baz").unwrap().get_name().to_string();
assert_eq!(true_name, "bar");
cmd = cmd.mut_subcommand(&*true_name, |subcmd| subcmd.about("modified about"));
assert_eq!(
cmd.find_subcommand("baz").unwrap().get_about().unwrap(),
"modified about"
);
}
#[test]
fn issue_3669_command_build_recurses() {
let mut cmd = Command::new("ctest").subcommand(