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

Brackets around optional positional args and options are a different color #100

Closed
dbohdan opened this issue Oct 19, 2023 · 3 comments · Fixed by #101
Closed

Brackets around optional positional args and options are a different color #100

dbohdan opened this issue Oct 19, 2023 · 3 comments · Fixed by #101
Labels
argparse Concerns argparse bug Something isn't working

Comments

@dbohdan
Copy link

dbohdan commented Oct 19, 2023

I get a help message like this from the code below.

screenshot

The square brackets around the optional positional argument are red, while those around the options are not. I am not sure whether this is intentional, but it looks incorrect. I would expect all sets of brackets to be the same color (default, not red in this example).

import argparse
from rich_argparse import RichHelpFormatter


def main() -> None:
    parser = argparse.ArgumentParser(
        formatter_class=RichHelpFormatter,
    )

    RichHelpFormatter.styles["argparse.args"] = "red"
    parser.add_argument(
        "positional",
        nargs=argparse.OPTIONAL,
    )
    parser.add_argument(
        "--option",
    )

    args = parser.parse_args()


if __name__ == "__main__":
    main()
@hamdanal hamdanal added the bug Something isn't working label Oct 21, 2023
@hamdanal
Copy link
Owner

This is not "intentional" but rather a known issue. I ignored it because it requires some tedious metavar parsing and special casing argparse internals. It gets more complicated with other nargs values like argparse.ZERO_OR_MORE, argparse.ONE_OR_MORE and others.
This is the the second request to fix this issue so maybe its time to reevaluate my position on the topic.

@hamdanal hamdanal added the argparse Concerns argparse label Oct 21, 2023
@dbohdan
Copy link
Author

dbohdan commented Oct 21, 2023

Sorry, I missed the first issue when I opened mine: #59.

If this is difficult to fix, and especially if the fix would be fragile, maybe it is not worth it. (At least not until argparse is fixed.) A note in the readme explaining the behavior may suffice. The person who opened the first issue was also confused about whether the difference was intentional, and a note would help with such confusion. I'd add a note even if you intended to fix this later.

@hamdanal
Copy link
Owner

Sorry, I missed the first issue when I opened mine: #59.

No worries. Yes this is the one.

If this is difficult to fix, and especially if the fix would be fragile, maybe it is not worth it. (At least not until argparse is fixed.) A note in the readme explaining the behavior may suffice. The person who opened the first issue was also confused about whether the difference was intentional, and a note would help with such confusion. I'd add a note even if you intended to fix this later.

I might be able to handle the positional arguments brackets without hitting the edge cases of argparse. However adding a style for the symbols as requested in the linked issue is more complicated.

hamdanal added a commit that referenced this issue Oct 21, 2023
Only color the metavar, not the symbols surrounding, it for positional
arguments in the usage. This makes positional arguments style more
consistent with optional arguments.

Fixes #100
hamdanal added a commit that referenced this issue Oct 21, 2023
…101)

Only color the metavar, not the symbols surrounding it, for positional
arguments in the usage. This makes positional arguments style more
consistent with optional arguments.

Fixes #100
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
argparse Concerns argparse bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants