Chris Jerdonek added the comment:

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.

----------

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue16468>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to