diff options
author | steven.bethard <devnull@localhost> | 2010-02-28 08:37:14 +0000 |
---|---|---|
committer | steven.bethard <devnull@localhost> | 2010-02-28 08:37:14 +0000 |
commit | 3be7b3eb9fc55ddad0a5950602deabcff5f046d9 (patch) | |
tree | 064cf4da25c2d9930963c9dad7205fa00c1f8d6d | |
parent | f34cf60cfcb22c8929543411c15fe662849e4a6a (diff) | |
download | argparse-3be7b3eb9fc55ddad0a5950602deabcff5f046d9.tar.gz |
Better error messages for errors like type='int'. This actually caught a bug in one of the tests and as I result, this patch also makes sure that types display as their type name when %(type)s is specified.
-rw-r--r-- | NEWS.txt | 1 | ||||
-rw-r--r-- | argparse.py | 9 | ||||
-rw-r--r-- | test/test_argparse.py | 5 |
3 files changed, 14 insertions, 1 deletions
@@ -9,6 +9,7 @@ What's New in argparse 1.1 - Issue #39: Better error messages for invalid actions. - Issue #43: Deprecate ArgumentParser(..., version=XXX, ...) and friends. - Issue #46: Allow single character options, e.g. '-' and '+'. +- Issue #51: Better error messages for errors like type='int'. - Namespace objects now support "in" (i.e. __contains__). - Usage and help (but not version) messages are written to stdout, not stderr. diff --git a/argparse.py b/argparse.py index c89618b..4966b14 100644 --- a/argparse.py +++ b/argparse.py @@ -621,6 +621,9 @@ class HelpFormatter(object): for name in list(params): if params[name] is SUPPRESS: del params[name] + for name in list(params): + if hasattr(params[name], '__name__'): + params[name] = params[name].__name__ if params.get('choices') is not None: choices_str = ', '.join([str(c) for c in params['choices']]) params['choices'] = choices_str @@ -1297,6 +1300,12 @@ class _ActionsContainer(object): if not _callable(action_class): raise ValueError('unknown action "%s"' % action_class) action = action_class(**kwargs) + + # raise an error if the action type is not callable + type_func = self._registry_get('type', action.type, action.type) + if not _callable(type_func): + raise ValueError('%r is not callable' % type_func) + return self._add_action(action) def add_argument_group(self, *args, **kwargs): diff --git a/test/test_argparse.py b/test/test_argparse.py index 968843c..4392e42 100644 --- a/test/test_argparse.py +++ b/test/test_argparse.py @@ -3300,7 +3300,7 @@ class TestHelpVariableExpansion(HelpTestCase): parser_signature = Sig(prog='PROG') argument_signatures = [ - Sig('-x', type='int', + Sig('-x', type=int, help='x %(prog)s %(default)s %(type)s %%'), Sig('-y', action='store_const', default=42, const='XXX', help='y %(prog)s %(default)s %(const)s'), @@ -3770,6 +3770,9 @@ class TestInvalidArgumentConstructors(TestCase): self.assertValueError('--') self.assertValueError('---') + def test_invalid_type(self): + self.assertValueError('--foo', type='int') + def test_invalid_action(self): self.assertValueError('-x', action='foo') self.assertValueError('foo', action='baz') |