From dafa3b163f367086e470bb00faf26a67e9702310 Mon Sep 17 00:00:00 2001 From: Yuya Nishihara Date: Sat, 2 Nov 2024 10:57:05 +0900 Subject: [PATCH] cli: insert dummy -h/--help flag when parsing early args Fixes #4746 --- cli/src/cli_util.rs | 9 +++++++++ cli/tests/test_global_opts.rs | 10 ++++++++++ 2 files changed, 19 insertions(+) diff --git a/cli/src/cli_util.rs b/cli/src/cli_util.rs index 9f730d8bbf..8342fa8fec 100644 --- a/cli/src/cli_util.rs +++ b/cli/src/cli_util.rs @@ -3032,7 +3032,16 @@ fn handle_early_args( let early_matches = app .clone() .disable_version_flag(true) + // Do not emit DisplayHelp error .disable_help_flag(true) + // Do not stop parsing at -h/--help + .arg( + clap::Arg::new("help") + .short('h') + .long("help") + .global(true) + .action(ArgAction::Count), + ) .ignore_errors(true) .try_get_matches_from(args)?; let mut args: EarlyArgs = EarlyArgs::from_arg_matches(&early_matches).unwrap(); diff --git a/cli/tests/test_global_opts.rs b/cli/tests/test_global_opts.rs index c3eaf146d6..fc469e5414 100644 --- a/cli/tests/test_global_opts.rs +++ b/cli/tests/test_global_opts.rs @@ -559,6 +559,16 @@ fn test_early_args() { let stdout = test_env.jj_cmd_success(test_env.env_root(), &["help", "--color=always"]); insta::assert_snapshot!(stdout.lines().find(|l| l.contains("Commands:")).unwrap(), @"Commands:"); + // Check that early args are accepted after -h/--help + let stdout = test_env.jj_cmd_success(test_env.env_root(), &["-h", "--color=always"]); + insta::assert_snapshot!( + stdout.lines().find(|l| l.contains("Usage:")).unwrap(), + @"Usage: jj [OPTIONS] "); + let stdout = test_env.jj_cmd_success(test_env.env_root(), &["log", "--help", "--color=always"]); + insta::assert_snapshot!( + stdout.lines().find(|l| l.contains("Usage:")).unwrap(), + @"Usage: jj log [OPTIONS] [PATHS]..."); + // Early args are parsed with clap's ignore_errors(), but there is a known // bug that causes defaults to be unpopulated. Test that the early args are // tolerant of this bug and don't cause a crash.