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
New %(flag)s format specifier for argparse.add_argument help string #78925
Comments
Sometimes you want to provide format description in help message for argparse.add_argument, e.g. for comma-separated key/value set "--flag key1=value1,key2=value2". At this moment there is no format specifier for flag or name of arguments. |
In your example, what is 'flag'? There's no Action attribute of that name. There is a 'dest' and a 'option_strings' list. All the 'help' '%(...)s' does is display one of the Action object attributes, the most common one is 'default'. Try this: a = parser.add_argument(...)
print(vars(a)) to see all the attributes of such an object. |
In your proposed change: params = dict(vars(action), prog=self._prog)
+ if action.option_strings:
+ params['flag'] = action.option_strings[0] 'params', as I noted earlier already includes the 'dest' and 'option_strings' list. 'option_strings' is already being used in '_format_action_invocation()'. So I don't see why this flag needs to appear again in the help line. Though the user can certainly hard code it into the line when defining the 'add_argument' e.g. cmdline.add_argument('--complex-argument', help='format --complex-argument key1=value1,key2=value2') What's special about the comma-separated key/value input? A patch/pull-request needs needs documentation, and unittest. It appears to be innocuous from a testing stand point, but good documentation could be problem - it could easily end up being confusing without adding much value. I propose closing this because I don't think it is needed. |
'option_strings' is available as a whole list, you can not use just first element of it
Same logic can be applied to other members of this dictionary, but it's purpose
|
The preferred way of adding new features to the Formatter is to subclass it, and modify one or more methods. That's what the existing alternative formatters do. A user can easily create such a subclass, and use it with their own parser, without having to modify the stock Given that flexibility, adding new features (as opposed to bug fixes) to the help formatter should have a low priority. |
https://bugs.python.org/issue13280, argparse should use the new Formatter class Raymond Hettinger argued against making such a switch. However there may be some value in allowing its use in parallel with the '%' style of formatting. That is, if the 'help' string has '%(...)' use the '%' formatting, if it has '{}' compatible formats use the '.format' expression. I think that can be done transparently; but I haven't tested it. The reason I bring it up here, is that I think `.format' can provide the functionality this issue is asking for. If I define an Action like: a1 = parser.add_argument('--foo', '-f', default='foobar'.
help='help for {dest} or {option_strings[0]}, default is {default}' Then: a1.help.format(**vars(a1)) produces:
So if there is another reason to add new style formatting to help lines, it's worth keeping this issue in mind. |
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:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: