Skip to content

Conversation

@kjanat
Copy link

@kjanat kjanat commented Jan 4, 2026

Summary

  • Add prog style option to customize program name color in usage text
  • Fix plugin to work with any argparse parser (not just those using RichHelpFormatter)
  • Add tests for plain parser support and style options
  • Update README to clarify compatibility and document style options

Problem

The plugin previously assumed the user's parser already used RichHelpFormatter. Most parsers use the default argparse.HelpFormatter, resulting in plain text with no colors.

Solution

Temporarily set RichHelpFormatter on the parser before calling format_help(), then restore the original formatter. This ensures colored output for any parser without requiring user modifications.

Changes

  1. _capture_help(): Temporarily swap formatter_class to RichHelpFormatter
  2. RichArgparseStyles: Add prog option (valid argparse.prog style in rich-argparse)
  3. Tests: Add coverage for plain parsers and style options
  4. README: Clarify any-parser support, document all style options
  5. Type fixes: Add cast() for choices dict values (fixes ty/mypy errors)

Copilot AI review requested due to automatic review settings January 4, 2026 08:12
Copy link

Copilot AI left a 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 RichHelpFormatter to any parser during help text capture to enable colored output
  • Adds prog style 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.

Comment on lines 66 to 67
subparsers_actions = [action for action in parser._actions if isinstance(action, argparse._SubParsersAction)]
current_subparsers_action = subparsers_actions[0]
Copy link

Copilot AI Jan 4, 2026

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.

Copilot uses AI. Check for mistakes.
Comment on lines +54 to +62
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
```
Copy link

Copilot AI Jan 4, 2026

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.

Copilot uses AI. Check for mistakes.
Comment on lines +161 to +168
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}"
Copy link

Copilot AI Jan 4, 2026

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.

Copilot uses AI. Check for mistakes.
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.

1 participant