Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cli: insert dummy -h/--help flag when parsing early args #4755

Merged
merged 2 commits into from
Nov 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions cli/src/cli_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2908,7 +2908,7 @@ fn resolve_default_command(
app: &Command,
mut string_args: Vec<String>,
) -> Result<Vec<String>, CommandError> {
const PRIORITY_FLAGS: &[&str] = &["help", "--help", "-h", "--version", "-V"];
const PRIORITY_FLAGS: &[&str] = &["--help", "-h", "--version", "-V"];

let has_priority_flag = string_args
.iter()
Expand Down Expand Up @@ -3032,8 +3032,16 @@ fn handle_early_args(
let early_matches = app
.clone()
.disable_version_flag(true)
// Do not emit DisplayHelp error
.disable_help_flag(true)
.disable_help_subcommand(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
yuja marked this conversation as resolved.
Show resolved Hide resolved
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