diff --git a/src/cli_util.rs b/src/cli_util.rs index 8230edd50..8c6ab4dfa 100644 --- a/src/cli_util.rs +++ b/src/cli_util.rs @@ -1414,8 +1414,8 @@ fn resolve_aliases( } loop { let app_clone = app.clone().allow_external_subcommands(true); - let matches = app_clone.try_get_matches_from(&string_args)?; - if let Some((command_name, submatches)) = matches.subcommand() { + let matches = app_clone.try_get_matches_from(&string_args).ok(); + if let Some((command_name, submatches)) = matches.as_ref().and_then(|m| m.subcommand()) { if !real_commands.contains(command_name) { let alias_name = command_name.to_string(); let alias_args = submatches @@ -1447,6 +1447,7 @@ fn resolve_aliases( } } } + // No more alias commands, or hit unknown option return Ok(string_args); } } diff --git a/tests/test_alias.rs b/tests/test_alias.rs index 052255566..6857bf111 100644 --- a/tests/test_alias.rs +++ b/tests/test_alias.rs @@ -12,6 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. +use itertools::Itertools as _; + use crate::common::TestEnvironment; pub mod common; @@ -72,6 +74,49 @@ fn test_alias_calls_unknown_command() { "###); } +#[test] +fn test_alias_calls_command_with_invalid_option() { + let test_env = TestEnvironment::default(); + test_env.jj_cmd_success(test_env.env_root(), &["init", "repo", "--git"]); + let repo_path = test_env.env_root().join("repo"); + + test_env.add_config( + br#"[alias] + foo = ["log", "--nonexistent"] + "#, + ); + let stderr = test_env.jj_cmd_cli_error(&repo_path, &["foo"]); + insta::assert_snapshot!(stderr, @r###" + error: Found argument '--nonexistent' which wasn't expected, or isn't valid in this context + + If you tried to supply '--nonexistent' as a value rather than a flag, use '-- --nonexistent' + + Usage: jj log [OPTIONS] [PATHS]... + + For more information try '--help' + "###); +} + +#[test] +fn test_alias_calls_help() { + let test_env = TestEnvironment::default(); + test_env.jj_cmd_success(test_env.env_root(), &["init", "repo", "--git"]); + let repo_path = test_env.env_root().join("repo"); + test_env.add_config( + br#"[alias] + h = ["--help"] + "#, + ); + let stdout = test_env.jj_cmd_success(&repo_path, &["h"]); + insta::assert_snapshot!(stdout.lines().take(5).join("\n"), @r###" + Jujutsu (An experimental VCS) + + To get started, see the tutorial at https://github.com/martinvonz/jj/blob/main/docs/tutorial.md. + + Usage: jj [OPTIONS] + "###); +} + #[test] fn test_alias_cannot_override_builtin() { let test_env = TestEnvironment::default();