This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Argparse: Add a "display" arg
Type: enhancement Stage:
Components: Library (Lib) Versions: Python 3.10, Python 3.9
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: ThatXliner, paul.j3, rhettinger
Priority: normal Keywords:

Created on 2020-10-12 21:18 by ThatXliner, last changed 2022-04-11 14:59 by admin.

Messages (10)
msg378526 - (view) Author: ThatXliner (ThatXliner) Date: 2020-10-12 21:18
In argparse, I've always wanted a way to make colored help text like those of poetry and pipenv. But argparse's show help isn't well documented (Therefore, I can't find a way to completely modify the help text), and the metavar arg isn't really helpful. I want to do something like this:

>>> import argparse
>>> parser = argparse.ArgumentParser()
>>> parser.add_argument("foo", help="To foo a bar", display="FOO!!!!")
>>> parser.print_help()

usage: main.py [-h] foo

positional arguments:
  FOO!!!!     To foo a bar

optional arguments:
  -h, --help  show this help message and exit

>>>

That way, I can colorize the help output.
msg378599 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2020-10-14 02:54
> and the metavar arg isn't really helpful.

Is your issue with *metavar* that it changes the help() output in two places?
msg378601 - (view) Author: paul j3 (paul.j3) * (Python triager) Date: 2020-10-14 03:13
I'm not following this request either, especially the colored part.

However, I can imagine adding a variable that gives the user full control over the action-invocation.  In:

    argparse.HelpFormatter._format_action_invocation method.

Currently it accepts the metavar (but not the tuple version) for a positional, but for optionals constructs:

  -s ARGS, --long ARGS

This can be quite long and ugly if the ARGS is a long choices, or there are many flag alternatives.  A number of SO users have asked for something simpler, usually with just on flag or

  -s, --long ARGS

There may even be a bug/issue(s) related to this already.
msg378632 - (view) Author: ThatXliner (ThatXliner) Date: 2020-10-14 16:00
>However, I can imagine adding a variable that gives the user full control over the action-invocation.  In:

    argparse.HelpFormatter._format_action_invocation method.

That's what I'm trying to change. If there was a public API/method for manipulating the left side of the help menu.

  (This part)     (Manipulatable via help=)
msg378644 - (view) Author: paul j3 (paul.j3) * (Python triager) Date: 2020-10-14 18:23
That method could be customized in a HelpFormatter subclass.  There already are several subclasses (documented), and writing your own is allowed, if not actually encouraged.

https://docs.python.org/3/library/argparse.html#formatter-class

Adding an extra parameter to 'add_argument', and passing that on through the Action class (and subclasses) is, potentially, a bigger task.  

So if you can get by with just customizing _format_action_invocation, there's no need for further action here.

I'll look for some examples, here or on SO, of customizing this method.
msg378646 - (view) Author: ThatXliner (ThatXliner) Date: 2020-10-14 18:35
Should I rename this to "Argparse: finer control on help txt"
msg378647 - (view) Author: ThatXliner (ThatXliner) Date: 2020-10-14 18:45
> So if you can get by with just customizing _format_action_invocation, there's no need for further action here.

Then maybe we could just make a new function that takes 2 arguments: Name of another action class (or the action class itself), and a tuple of the arg names to be displayed. It returns a new action class that contains the help text properties of the given action class with customized displayed arg names.


Maybe like

parser.add_argument("-f", "--foo", action=argparse.CustomArgName(action="store_true", name=("-F!!", "--FOO!"), help="TO foo a bar"))
msg378649 - (view) Author: paul j3 (paul.j3) * (Python triager) Date: 2020-10-14 19:02
Some StackOverFlow answers cusomizing this help method:

https://stackoverflow.com/questions/23936145/python-argparse-help-message-disable-metavar-for-short-options

https://stackoverflow.com/questions/18275023/dont-show-long-options-twice-in-print-help-from-argparse
msg378650 - (view) Author: ThatXliner (ThatXliner) Date: 2020-10-14 19:39
Editing the description, etc. seems like a good way to do this. But it seems "hacky". The other answer on StackOverflow implements a custom class which does nice things, but not what I want.
msg378651 - (view) Author: ThatXliner (ThatXliner) Date: 2020-10-14 19:40
In the end, I think python's argparse needs more built-in help formatters...


Should I close this?
History
Date User Action Args
2022-04-11 14:59:36adminsetgithub: 86189
2020-10-14 19:40:18ThatXlinersetmessages: + msg378651
2020-10-14 19:39:46ThatXlinersetmessages: + msg378650
2020-10-14 19:02:29paul.j3setmessages: + msg378649
2020-10-14 18:45:07ThatXlinersetmessages: + msg378647
2020-10-14 18:35:25ThatXlinersetmessages: + msg378646
2020-10-14 18:23:34paul.j3setmessages: + msg378644
2020-10-14 16:00:22ThatXlinersetmessages: + msg378632
2020-10-14 03:13:23paul.j3setmessages: + msg378601
2020-10-14 02:54:14rhettingersetmessages: + msg378599
2020-10-13 01:26:34xtreaksetnosy: + rhettinger, paul.j3
2020-10-12 21:18:16ThatXlinercreate