-
Notifications
You must be signed in to change notification settings - Fork 1
Add prog style option and fix plain parser support #5
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
base: main
Are you sure you want to change the base?
Conversation
The plugin now temporarily sets RichHelpFormatter on any parser to ensure colored ANSI output, regardless of the parser's original formatter_class setting.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This PR enhances the mkdocs-rich-argparse plugin to work with any argparse parser by temporarily swapping formatter classes, adds a prog style configuration option, and improves documentation about parser compatibility.
- Temporarily applies
RichHelpFormatterto any parser during help text capture to enable colored output - Adds
progstyle option to customize program name coloring - Updates README to clarify that the plugin works with any ArgumentParser, not just those using RichHelpFormatter
- Adds type casting for subparser choices dictionary values to resolve type checking issues
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| src/mkdocs_rich_argparse/init.py | Implements formatter class swapping in _capture_help() with proper restoration, adds prog style config option, and adds type casting for subparser choices |
| tests/test_plugin.py | Adds tests verifying plain parsers get colors, formatter restoration for main and subparsers, prog style option application, and validates all style options exist |
| README.md | Updates description to emphasize compatibility with any argparse parser and documents all style configuration options with examples |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| styles: | ||
| prog: "#00ff00" # Program name in usage | ||
| args: "#ff0000" # Arguments and options | ||
| groups: "#00ffff" # Group headers | ||
| metavar: "#ff00ff" # Metavariables (e.g., FILE) | ||
| help: white # Help text | ||
| text: white # Description text | ||
| default: grey50 # Default values | ||
| ``` |
Copilot
AI
Jan 4, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The "syntax" style option is defined in the RichArgparseStyles config class but is missing from the README documentation. This creates an inconsistency between the available configuration options and the documentation.
Consider adding "syntax" to the list of style options in the README's Custom Styles section, or if it's intentionally excluded, add a comment explaining why.
sverhoeven
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the contribution and allowing this package to be used with just argparse.
I am able to see the green prog text
Also a parser with default formatter works.
However when someone has configured a class_formatter then it gets replaced by the RichHelpFormatter and causing loss of the users intent.
For example when using ArgumentDefaultsHelpFormatter the (default 42) text is gone with this PR.
At https://github.com/hamdanal/rich-argparse/blob/129fd67f7d92cffb3402422112fde6b3121352f6/rich_argparse/_argparse.py#L528-L549 there are replacements for the argparse builtin formatters.
Can you make swapping the formatter_class for RichHelpFormatter smarter so at least the argparse builtin formatters are still functional?
Also can you also add the prog style to example/mkdocs-custom.yml ?
Summary
progstyle option to customize program name color in usage textRichHelpFormatter)Problem
The plugin previously assumed the user's parser already used
RichHelpFormatter. Most parsers use the defaultargparse.HelpFormatter, resulting in plain text with no colors.Solution
Temporarily set
RichHelpFormatteron the parser before callingformat_help(), then restore the original formatter. This ensures colored output for any parser without requiring user modifications.Changes
_capture_help(): Temporarily swapformatter_classtoRichHelpFormatterRichArgparseStyles: Addprogoption (validargparse.progstyle in rich-argparse)cast()forchoicesdict values (fixes ty/mypy errors)