paul j3 added the comment: This patch generally deals with the choices option, and specifically the problems with formatting long lists, or objects that have __contains__ but not __iter__. But it also incorporates issues 9849 (better add_argument testing) and 9625 (choices with *). It may be too broad for this issue, but the changes all relate to 'choices'.
As noted by other posters, there are 3 places where choices is formatted with a comprehension. I have refactored these into one _format_choices function. _format_choices() is a utility function that formats the choices by iteration, and failing that using repr(). It raises an error if choices does not even have a __contains__. It also has a summarize option ({1,2,3,...,19,20}). I did not make this an action method because it only uses the choices object. _metavar_formatter() - calls _format_choices for Usage with the default compact form. Its use of metavar gives the user full control of the choices display. _expand_help() - calls _format_choices with the looser format. This form is used only if the user puts '%(choices)s' in the help string. This is not documented, and only appears a few times in the test file. Again the user has ultimate control over the contents. _check_value() - calls _format_choices with a 'summarize=15' option. Normally this error message appears with the usage message. So it does not need to use the metavar. The MetavarTypeHelpFormatter subclass is an example of how formats can be customized without changing normal behavior. Such a subclass could even be used to set custom parameters, or modify any of the above methods. -------------------- other changes: formatter _format_actions_usage() - I tweaked the regex that trims excess notation from mutually exclusive groups. This removed '()' from other parts of the usage line, for example a metavar like 'range(20)'. Issue 18349. formatter _format_args() - I included issue 9849 changes which improve testing for nargs, and array metavars. This calls the _metavar_formatter. Thus any errors in formatting choices pass back through this. Issue 9849 also changes container add_argument() to call the parser _check_argument(). This in turn calls _format_args() to test action options like nargs, metavars, and now choices. If there are problems it raises an ArgumentError. parser _get_values() - issue 9625 changes this to correctly handle choices when nargs='*'. parser _check_value() - I rewrote this to give better errors if there are problems with __contains__. If choices is a string (e.g. 'abc') it converts it to a list, so __contains__ is more consistent. For example, 'bc' in 'abc' is True, but 'bc' in ['a','b','c'] is False (issue 16977) ---------------------- test_argparse change examples with string choices to lists class TestAddArgumentMetavar change EXPECTED_MESSAGE and EXPECTED_ERROR to reflect issue 9849 changes class TestMetavarWithParen tests 'range(n)' choices makes sure () in the metavar are preserved tests that metavar is used in Usage as given tests summarized list of choices in the error message tests the %(choices)s help line case class TestNonIterableChoices tests a choices container that has __contains__ but not __iter__ tests that repr() is used as needed class TestBareChoices tests a class without even __contains__ tests for an add_argument error class TestStringChoices tests the expansion of 'abc' to ['a','b','c'] ---------- Added file: http://bugs.python.org/file30872/choice2.patch _______________________________________ 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