Issue 11174: add argparse formatting option to display type names for metavar (original) (raw)

Created on 2011-02-10 15:45 by bethard, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
11174.patch gruszczy,2011-02-26 17:30 review
MetavarTypeHelpFormatter.diff bethard,2011-03-26 12:16 review
Messages (6)
msg128300 - (view) Author: Steven Bethard (bethard) * (Python committer) Date: 2011-02-10 15:45
Suggestion from a personal email: I generally like my command line arguments that take a value to specify the type, e.g., --runs int how many runs to do Naturally I can do this using the metavars argument in every add_argument() call, but that can become tedious. So I suggest adding a metavars argument to the ArgumentParser constructor; default metavars=None (current behaviour); metavars=type (show type names). And of course, allow metavars specified in add_argument() calls to override on a case by case basis.
msg129577 - (view) Author: Filip GruszczyƄski (gruszczy) Date: 2011-02-26 17:30
First draft of patch with this functionality. Test, patch and docs are included. I'll gladly work further on this, so I would appreciate some advice and suggestions.
msg132230 - (view) Author: Steven Bethard (bethard) * (Python committer) Date: 2011-03-26 12:16
Sorry about such a slow response on this. Thanks for the patch! I think rather than adding an ArgumentParser constructor parameter though, we should add a new formatter class. The attached patch allows you to write: >>> parser = argparse.ArgumentParser( ... prog='PROG', ... formatter_class=argparse.MetavarTypeHelpFormatter) >>> parser.add_argument('--foo', type=int) >>> parser.add_argument('bar', type=float) >>> parser.print_help() usage: PROG [-h] [--foo int] float positional arguments: float optional arguments: -h, --help show this help message and exit --foo int What do you think?
msg132231 - (view) Author: Filip GruszczyƄski (gruszczy) Date: 2011-03-26 12:23
Well, since you are the designer of the package, I believe you have better knowledge on how to extend it :-). I just provided a patch according to what you described in the first message. Anyway having separate help formatter seems better than bloating parser's constructor.
msg132237 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2011-03-26 13:48
New changeset a15d65d8f269 by Steven Bethard in branch 'default': Issue #11174: Add argparse.MetavarTypeHelpFormatter, which uses type names http://hg.python.org/cpython/rev/a15d65d8f269
msg253197 - (view) Author: paul j3 (paul.j3) * (Python triager) Date: 2015-10-19 21:33
This formatter produces an error if one or more of the arguments uses the default `None` type (a string). This is because `None` does not have a `.__name__`. This HelpFormatter probably has been rarely, if ever, used. The metavar parameter works just as well.
History
Date User Action Args
2022-04-11 14:57:12 admin set github: 55383
2015-10-19 21:33:22 paul.j3 set nosy: + paul.j3messages: +
2011-03-26 13:52:54 bethard set status: open -> closedassignee: bethardresolution: fixedstage: needs patch -> resolved
2011-03-26 13:48:31 python-dev set nosy: + python-devmessages: +
2011-03-26 12:23:13 gruszczy set messages: +
2011-03-26 12:16:42 bethard set files: + MetavarTypeHelpFormatter.diffmessages: +
2011-02-26 17:30:23 gruszczy set files: + 11174.patchnosy: + gruszczymessages: + keywords: + patch
2011-02-10 15:45:51 bethard create