-
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.
| subparsers_actions = [action for action in parser._actions if isinstance(action, argparse._SubParsersAction)] | ||
| current_subparsers_action = subparsers_actions[0] |
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.
This code will raise an IndexError when a parser has no subparsers. The list subparsers_actions could be empty if the parser doesn't define any subcommands, causing subparsers_actions[0] to fail.
You should check if subparsers_actions is non-empty before accessing it. Consider wrapping the subparser processing logic in a conditional check.
| 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.
| def test_all_style_options_recognized(): | ||
| """Verify all style options are recognized by the config.""" | ||
| styles = RichArgparseStyles() | ||
|
|
||
| # All these should be valid attributes | ||
| expected_styles = ["prog", "args", "groups", "help", "metavar", "syntax", "text", "default"] | ||
| for style_name in expected_styles: | ||
| assert hasattr(styles, style_name), f"Missing style option: {style_name}" |
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 tests don't cover the case where a parser has no subparsers at all. This is a common scenario - many command-line tools don't use subcommands. The lack of test coverage for this case allowed the IndexError bug on line 67 of init.py to go undetected.
Consider adding a test case for a simple parser without any subparsers to ensure the function handles this scenario correctly.
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)