Package: rich_argparse_plus
rich-argparse-plusFormatargparsehelp output withrich. This is a fork of the awesomerich-argparsethat adds a few features:Select from manypreconfigured color themes.Render to various image/web formats by setting an variable when you run--help.PNG, PDF, HTML, SVG, PS, EPS, colored text are supported. Show off your fancy stuff.Displays default argument values by default.Displays the range of acceptable values for integer arguments limited bychoices=range(n).Attn: PyPi Users:To see the imagesview this README on github(That's theprincetheme, for obvious reasons).Installationpipinstallrich-argparse-plus# To render to image formats like PNG or PDF you need the optional dependencies:pipinstallrich-argparse-plus[optional]UsagePass theformatter_classto the argument parser, optionally choosing a theme.importargparsefromrich_argparse_plusimportRichHelpFormatterPlusRichHelpFormatterPlus.choose_theme('prince')parser=argparse.ArgumentParser(...,formatter_class=RichHelpFormatterPlus)Rendering Help Text As ImageryFormats supported arehtml,png,pdf,ps,svg,eps, andtxt(colored text). To actually render send theRENDER_HELP_FORMATenvironment variable while you run your program with--help:# Render a png to the current directoryRENDER_HELP_FORMAT=pngmy_awesome_program.py--help# Set RENDER_HELP_OUTPUT_DIR to send the output somewhere elseRENDER_HELP_FORMAT=pdfRENDER_HELP_OUTPUT_DIR=doc/themes/my_awesome_program--helpPerusing ThemesYou can view images of all the themeshere in the repo. Alternatively you can runrich_argparse_plus_show_themesto print them to your terminal.Here's a couple of them:mother_earththe_pinkblack_and_whitemorning_glorydraculagrey_areaThe Random Theme Stream ChannelTry runningrich_argparse_plus_random_theme_stream.Recipesargparse's subparsersargparsesubparsers do not inherit the formatter class from the parent parser. To have the help
text of subparsers formatted with rich, you have to explicitly passformatter_classto the
subparsers:you can pass it to all subparsers at once:subparsers=parser.add_subparsers(...,parser_class=lambda**k:type(parser)(**k,formatter_class=parser.formatter_class),)p1=subparsers.add_parser(...)p2=subparsers.add_parser(...)or to each subparser individually:subparsers=parser.add_subparsers(...)p1=subparsers.add_parser(...,formatter_class=parser.formatter_class)p2=subparsers.add_parser(...,formatter_class=parser.formatter_class)django's commandsdjangouses argparse for its built in commands as well as for extension libraries and user
defined commands. To use rich_argparse with these commands, change yourmanage.pyfile as
follows:diff --git a/my_project/manage.py b/my_project/manage.pyindex 7fb6855..5e5d48a 100755--- a/my_project/manage.py+++ b/my_project/manage.py@@ -1,22 +1,38 @@#!/usr/bin/env python"""Django's command-line utility for administrative tasks."""import osimport sysdef main():"""Run administrative tasks."""os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'my_project.settings')try:from django.core.management import execute_from_command_lineexcept ImportError as exc:raise ImportError("Couldn't import Django. Are you sure it's installed and ""available on your PYTHONPATH environment variable? Did you ""forget to activate a virtual environment?") from exc++    from django.core.management.base import BaseCommand, DjangoHelpFormatter+    from rich_argparse_plus import RichHelpFormatterPlus++    class RichDjangoHelpFormatter(DjangoHelpFormatter, RichHelpFormatterPlus):  # django first+        """A rich-based help formatter for django commands."""++    original_create_parser = BaseCommand.create_parser++    def create_parser(*args, **kwargs):+        parser = original_create_parser(*args, **kwargs)+        parser.formatter_class = RichDjangoHelpFormatter  # set the formatter_class+        return parser++    BaseCommand.create_parser = create_parser+execute_from_command_line(sys.argv)if __name__ == '__main__':main()Now try out some command like:python manage.py runserver --helpSpecial text highlightingYou can highlight patterns in the help text of your CLI. By default,RichHelpFormatterPlusdefines
the following styles:>>>pprint(RichHelpFormatterPlus.styles){'argparse.args': 'cyan','argparse.groups': 'dark_orange','argparse.help': 'default','argparse.metavar': 'dark_cyan','argparse.syntax': 'bold','argparse.text': 'default'}The following example highlights all occurrences ofpyproject.tomlin green.# add a style called `pyproject` which applies a green style (any rich style works)RichHelpFormatterPlus.styles["argparse.pyproject"]="green"# add the highlight regex (the regex group name must match an existing style name)RichHelpFormatterPlus.highlights.append(r"\b(?P<pyproject>pyproject\.toml)\b")# pass the formatter class to argparseparser=argparse.ArgumentParser(...,formatter_class=RichHelpFormatterPlus)...Custom group name formattingYou can change the formatting of the group name (like'positional arguments'and'options') by
setting theRichHelpFormatterPlus.group_name_formatterto any function that takes the group name as
an input and returns a str. By default,RichHelpFormatterPlussets the function tostr.upper.RichHelpFormatterPlus.group_name_formatter=str.title