From 6b862c6f23939ff16342e1b6faba2893c5030a05 Mon Sep 17 00:00:00 2001 From: Yuya Nishihara Date: Sun, 24 Dec 2023 11:49:36 +0900 Subject: [PATCH] cli: don't panic on empty alias substitution This partially reverts 6c627fb30d76 "cli: default to log when no subcommand is provided." We could reject an empty alias at all, but we would still need to ensure that the expanded alias contained a subcommand name. The help output is a bit odd as the can be omitted, but I think that's acceptable. If we do care about that, maybe we can override_usage(). --- cli/src/cli_util.rs | 6 +++++- cli/tests/test_alias.rs | 34 +++++++++++++++++++++++++++++++--- 2 files changed, 36 insertions(+), 4 deletions(-) diff --git a/cli/src/cli_util.rs b/cli/src/cli_util.rs index de87c4dcc..ee2b87c0e 100644 --- a/cli/src/cli_util.rs +++ b/cli/src/cli_util.rs @@ -2777,7 +2777,11 @@ pub fn parse_args( layered_configs: &mut LayeredConfigs, ) -> Result<(ArgMatches, Args), CommandError> { handle_early_args(ui, app, string_args, layered_configs)?; - let matches = app.clone().try_get_matches_from(string_args)?; + let matches = app + .clone() + .arg_required_else_help(true) + .subcommand_required(true) + .try_get_matches_from(string_args)?; let args: Args = Args::from_arg_matches(&matches).unwrap(); if args.global_args.verbose { diff --git a/cli/tests/test_alias.rs b/cli/tests/test_alias.rs index 146739c39..74b727327 100644 --- a/cli/tests/test_alias.rs +++ b/cli/tests/test_alias.rs @@ -71,12 +71,40 @@ fn test_alias_bad_name() { insta::assert_snapshot!(stderr, @r###" error: unrecognized subcommand 'foo.' - Usage: jj [OPTIONS] [COMMAND] + Usage: jj [OPTIONS] For more information, try '--help'. "###); } +#[test] +fn test_alias_calls_empty_command() { + let test_env = TestEnvironment::default(); + test_env.jj_cmd_ok(test_env.env_root(), &["init", "repo", "--git"]); + let repo_path = test_env.env_root().join("repo"); + + test_env.add_config( + r#" + aliases.empty = [] + aliases.empty_command_with_opts = ["--no-pager"] + "#, + ); + let stderr = test_env.jj_cmd_cli_error(&repo_path, &["empty"]); + insta::assert_snapshot!(stderr.lines().take(3).join("\n"), @r###" + Jujutsu (An experimental VCS) + + Usage: jj [OPTIONS] + "###); + let stderr = test_env.jj_cmd_cli_error(&repo_path, &["empty", "--no-pager"]); + insta::assert_snapshot!(stderr.lines().next().unwrap_or_default(), @r###" + error: 'jj' requires a subcommand but one was not provided + "###); + let stderr = test_env.jj_cmd_cli_error(&repo_path, &["empty_command_with_opts"]); + insta::assert_snapshot!(stderr.lines().next().unwrap_or_default(), @r###" + error: 'jj' requires a subcommand but one was not provided + "###); +} + #[test] fn test_alias_calls_unknown_command() { let test_env = TestEnvironment::default(); @@ -90,7 +118,7 @@ fn test_alias_calls_unknown_command() { tip: a similar subcommand exists: 'next' - Usage: jj [OPTIONS] [COMMAND] + Usage: jj [OPTIONS] For more information, try '--help'. "###); @@ -127,7 +155,7 @@ fn test_alias_calls_help() { To get started, see the tutorial at https://github.com/martinvonz/jj/blob/main/docs/tutorial.md. - Usage: jj [OPTIONS] [COMMAND] + Usage: jj [OPTIONS] "###); }