cli: insert dummy -h/--help flag when parsing early args
Some checks are pending
binaries / Build binary artifacts (linux-aarch64-gnu, ubuntu-24.04, aarch64-unknown-linux-gnu) (push) Waiting to run
binaries / Build binary artifacts (linux-aarch64-musl, ubuntu-24.04, aarch64-unknown-linux-musl) (push) Waiting to run
binaries / Build binary artifacts (linux-x86_64-gnu, ubuntu-24.04, x86_64-unknown-linux-gnu) (push) Waiting to run
binaries / Build binary artifacts (linux-x86_64-musl, ubuntu-24.04, x86_64-unknown-linux-musl) (push) Waiting to run
binaries / Build binary artifacts (macos-aarch64, macos-14, aarch64-apple-darwin) (push) Waiting to run
binaries / Build binary artifacts (macos-x86_64, macos-13, x86_64-apple-darwin) (push) Waiting to run
binaries / Build binary artifacts (win-x86_64, windows-2022, x86_64-pc-windows-msvc) (push) Waiting to run
nix / flake check (macos-14) (push) Waiting to run
nix / flake check (ubuntu-latest) (push) Waiting to run
build / build (, macos-13) (push) Waiting to run
build / build (, macos-14) (push) Waiting to run
build / build (, ubuntu-latest) (push) Waiting to run
build / build (, windows-latest) (push) Waiting to run
build / build (--all-features, ubuntu-latest) (push) Waiting to run
build / Build jj-lib without Git support (push) Waiting to run
build / Check protos (push) Waiting to run
build / Check formatting (push) Waiting to run
build / Check that MkDocs can build the docs (push) Waiting to run
build / Check that MkDocs can build the docs with Poetry 1.8 (push) Waiting to run
build / cargo-deny (advisories) (push) Waiting to run
build / cargo-deny (bans licenses sources) (push) Waiting to run
build / Clippy check (push) Waiting to run
Codespell / Codespell (push) Waiting to run
website / prerelease-docs-build-deploy (ubuntu-latest) (push) Waiting to run
Scorecards supply-chain security / Scorecards analysis (push) Waiting to run

Fixes #4746
This commit is contained in:
Yuya Nishihara 2024-11-02 10:57:05 +09:00
parent 8e1eb23246
commit 15da697616
2 changed files with 19 additions and 0 deletions

View file

@ -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();

View file

@ -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] <COMMAND>");
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.