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] "###); }