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.

Author chris.jerdonek
Recipients bethard, chris.jerdonek, r.david.murray, terry.reedy
Date 2013-01-18.00:51:04
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <>
I have a new suggestion that I hope will satisfy Terry.

After looking at the code more, I noticed that add_argument() does currently work for non-iterable choices provided that metavar is passed.  This was also noted in the report for the duplicate issue 16697.

On reflection, this makes sense because that's what metavar is for -- providing a replacement string for the usual formatting in the help text.  The only thing that doesn't work in this case is error message formatting.

With that, I'd like to propose the following:

(1) Change the add_argument() error raised for non-iterable choices from:

    ValueError("length of metavar tuple does not match nargs")

to something like: 

    ValueError("metavar must be provided for non-iterable choices")

This provides the help string representation for non-iterable choices (in the spirit of "Explicit is better than implicit").

(2) Make the error text the following for non-iterable choices (the error message currently breaks for non-iterable choices):

    PROG: error: argument FOO: invalid choice: 'xxx'

compared with (for iterable choices)--

    PROG: error: argument FOO: invalid choice: 'xxx' (choose from ...)

I think this is preferable to inserting the str() or repr() (especially for maintenance releases) because str() and repr() may not be meant for displaying to the end-users of a script.  The instructions/description of such choices is already in the add_argument() "help" string.  We could consider appending that to provide substantive guidance.
Date User Action Args
2013-01-18 00:51:05chris.jerdoneksetrecipients: + chris.jerdonek, terry.reedy, bethard, r.david.murray
2013-01-18 00:51:05chris.jerdoneksetmessageid: <>
2013-01-18 00:51:05chris.jerdoneklinkissue16468 messages
2013-01-18 00:51:04chris.jerdonekcreate