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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

yuja
Copy link
Collaborator

@yuja yuja commented Nov 2, 2024

#4746

Checklist

If applicable:

  • I have updated CHANGELOG.md
  • I have updated the documentation (README.md, docs/, demos/)
  • I have updated the config schema (cli/src/config-schema.json)
  • I have added tests to cover my changes

Since 536c629 "cli: Explicitly add a Help command to accept the early args
after it", "jj help" is a normal subcommand.
@@ -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
Copy link
Collaborator

@ilyagr ilyagr Nov 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Optional) Should we also test --color=always -h?Or is that completely equivalent to --color=always --help? (Or is it tested somewhere else?)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They're equivalent in implementation wise.

Copy link
Collaborator

@ilyagr ilyagr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you very much! This looks good to me, I'll also ping @Grillo-0 in case they have any thoughts.

@@ -3032,8 +3032,15 @@ fn handle_early_args(
let early_matches = app
.clone()
.disable_version_flag(true)
// Ignore any appearance of -h/--help and continue parsing
Copy link
Collaborator

@ilyagr ilyagr Nov 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Non-blocking) I am a bit confused about why this works (Update: or, to be more precise, why it didn't work before). Shouldn't .disable_help_flag(true).ignore_errors(true) have been sufficient to ignore -h while parsing?

Depending on the answer, it might be nice to add it to the comment.

Copy link
Collaborator Author

@yuja yuja Nov 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In clap, help is an "error" (or a thing that triggers exceptional code path.) That's my understanding.

Copy link
Collaborator

@ilyagr ilyagr Nov 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, but shouldn't ignore_errors ignore this error, especially since the help flag is disabled? I guess it clearly doesn't. Maybe it's a documentation bug in clap.

Thanks again for figuring it out.

Copy link
Collaborator

@ilyagr ilyagr Nov 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe the comment should be moved to the next line and say (if this is true):

Without this, parsing will stop on the first -h or --help, despite .disable_help_flag() and .ignore_errors().

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, but shouldn't ignore_errors ignore this error, especially since the help flag is disabled?

If the default -h is disabled, -h triggers a normal parse error. If it's enabled, -h triggers a "help requested error". In both cases, any remaining args won't be parsed. We do need to skip over -h. And that's why I put the comment there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants