GeezerVison is showing github.com — Argparse: Add a "display" arg · Issue #86189 · python/cpython
Argparse: Add a "display" arg #86189Description
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
assignee = None
closed_at = None
created_at = <Date 2020-10-12.21:18:16.139>
labels = ['type-feature', 'library', '3.9', '3.10']
title = 'Argparse: Add a "display" arg'
updated_at = <Date 2020-10-14.19:40:18.135>
user = 'https://github.com/ThatXliner'
bugs.python.org fields:
activity = <Date 2020-10-14.19:40:18.135>
actor = 'ThatXliner'
assignee = 'none'
closed = False
closed_date = None
closer = None
components = ['Library (Lib)']
creation = <Date 2020-10-12.21:18:16.139>
creator = 'ThatXliner'
dependencies = []
files = []
hgrepos = []
issue_num = 42023
keywords = []
message_count = 10.0
messages = ['378526', '378599', '378601', '378632', '378644', '378646', '378647', '378649', '378650', '378651']
nosy_count = 3.0
nosy_names = ['rhettinger', 'paul.j3', 'ThatXliner']
pr_nums = []
priority = 'normal'
resolution = None
stage = None
status = 'open'
superseder = None
type = 'enhancement'
url = 'https://bugs.python.org/issue42023'
versions = ['Python 3.9', 'Python 3.10']
ActivityThatXliner commented on Oct 12, 2020 MannequinAuthor More actions 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. rhettinger commented on Oct 14, 2020
and the metavar arg isn't really helpful.
Is your issue with *metavar* that it changes the help() output in two places? paulj3 commented on Oct 14, 2020 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. ThatXliner commented on Oct 14, 2020 MannequinAuthor More actions
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=) paulj3 commented on Oct 14, 2020 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. ThatXliner commented on Oct 14, 2020 MannequinAuthor More actions Should I rename this to "Argparse: finer control on help txt" ThatXliner commented on Oct 14, 2020 MannequinAuthor More actions
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")) paulj3 commented on Oct 14, 2020 ThatXliner commented on Oct 14, 2020 MannequinAuthor More actions 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. ThatXliner commented on Oct 14, 2020 MannequinAuthor More actions In the end, I think python's argparse needs more built-in help formatters...
Should I close this?
MetadataMetadataStatusShow more project fields
DevelopmentNo branches or pull requestsIssue actions
|