Skip to content

Commit

Permalink
cli: insert dummy -h/--help flag when parsing early args
Browse files Browse the repository at this point in the history
  • Loading branch information
yuja committed Nov 3, 2024
1 parent 76d4b59 commit dafa3b1
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
9 changes: 9 additions & 0 deletions cli/src/cli_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
10 changes: 10 additions & 0 deletions cli/tests/test_global_opts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit dafa3b1

Please sign in to comment.