Title: add argparse formatting option to display type names for metavar
Type: enhancement Stage: resolved
Components: Library (Lib) Versions: Python 3.3
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: bethard Nosy List: bethard, gruszczy, paul.j3, python-dev
Priority: low Keywords: patch

Created on 2011-02-10 15:45 by bethard, last changed 2015-10-19 21:33 by paul.j3. This issue is now closed.

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:

   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
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.
Date User Action Args
2015-10-19 21:33:22paul.j3setnosy: + paul.j3
messages: + msg253197
2011-03-26 13:52:54bethardsetstatus: open -> closed
assignee: bethard
resolution: fixed
stage: needs patch -> resolved
2011-03-26 13:48:31python-devsetnosy: + python-dev
messages: + msg132237
2011-03-26 12:23:13gruszczysetmessages: + msg132231
2011-03-26 12:16:42bethardsetfiles: + MetavarTypeHelpFormatter.diff

messages: + msg132230
2011-02-26 17:30:23gruszczysetfiles: + 11174.patch

nosy: + gruszczy
messages: + msg129577

keywords: + patch
2011-02-10 15:45:51bethardcreate