-
-
Notifications
You must be signed in to change notification settings - Fork 31.8k
gh-130645: Add colour to argparse
help
#132323
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
Conversation
🤖 New build scheduled with the buildbot fleet by @hugovk for commit 3d174b7 🤖 Results will be shown at: https://buildbot.python.org/all/#/grid?branch=refs%2Fpull%2F132323%2Fmerge If you want to schedule another build, you need to add the 🔨 test-with-buildbots label again. |
When you're done making the requested changes, leave the 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.
A comment about the approach and whether we can simplify a bit.
Many argparse CLIs I see already use keywords for most parameters, but I think we should make these new ones keyword-only, especially for new bools, as a long list of
What do you think? |
Commandeering this to make the beta 1 cut. Code owner's feedback can be addressed in follow-up PRs.
python/cpython#132323 added an optional `color` argument to ArgumentParser. As a side effect the help formatters are now called with two new keyword arguments `prefix_chars` and `color`. Add `**kwargs` to the custom `AugmentedHelpFormatter` and pass it through to the super class.
this patch breaks $ ./prefix/bin/python3 virtualenv.pyz venvvvv
Traceback (most recent call last):
File "<frozen runpy>", line 198, in _run_module_as_main
File "<frozen runpy>", line 88, in _run_code
File "/home/asottile/workspace/cpython/virtualenv.pyz/__main__.py", line 178, in <module>
run()
~~~^^
File "/home/asottile/workspace/cpython/virtualenv.pyz/__main__.py", line 174, in run
run_virtualenv()
~~~~~~~~~~~~~~^^
File "/home/asottile/workspace/cpython/virtualenv.pyz/virtualenv/__main__.py", line 20, in run
session = cli_run(args, options, env)
File "/home/asottile/workspace/cpython/virtualenv.pyz/virtualenv/run/__init__.py", line 31, in cli_run
of_session = session_via_cli(args, options, setup_logging, env)
File "/home/asottile/workspace/cpython/virtualenv.pyz/virtualenv/run/__init__.py", line 49, in session_via_cli
parser, elements = build_parser(args, options, setup_logging, env)
~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/asottile/workspace/cpython/virtualenv.pyz/virtualenv/run/__init__.py", line 64, in build_parser
add_version_flag(parser)
~~~~~~~~~~~~~~~~^^^^^^^^
File "/home/asottile/workspace/cpython/virtualenv.pyz/virtualenv/run/__init__.py", line 138, in add_version_flag
parser.add_argument(
~~~~~~~~~~~~~~~~~~~^
"--version",
^^^^^^^^^^^^
...<2 lines>...
help="display the version of the virtualenv package and its location, then exit",
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
)
^
File "/home/asottile/workspace/cpython/prefix/lib/python3.14/argparse.py", line 1566, in add_argument
formatter = self._get_formatter()
File "/home/asottile/workspace/cpython/prefix/lib/python3.14/argparse.py", line 2733, in _get_formatter
return self.formatter_class(
~~~~~~~~~~~~~~~~~~~~^
prog=self.prog,
^^^^^^^^^^^^^^^
prefix_chars=self.prefix_chars,
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
color=self.color,
^^^^^^^^^^^^^^^^^
)
^
TypeError: HelpFormatter.__init__() got an unexpected keyword argument 'prefix_chars' it looks like adding the parameters to the help formatter here breaks the subclass there cc @gaborbernat |
Add
color
parameter toargparse.ArgumentParser
, which by default isFalse
. When set toTrue
, the help text is in colour if allowed (for example, we're not piping,NO_COLOR=1
is not set).Example output
Using this script:
Performance
With the same script and PGO+LTO on macOS, running it once (
time ./python.exe argparse-cli.py --help
) and 100 times (time ./100.sh
):main
(3feac7a):PR and colour not enabled:
PR and colour enabled:
Something like 0.65 ms difference per run between before and after, averaging from
100.sh
.argparse
help #130645📚 Documentation preview 📚: https://cpython-previews--132323.org.readthedocs.build/