diff options
author | steven.bethard <devnull@localhost> | 2009-10-24 15:40:25 +0000 |
---|---|---|
committer | steven.bethard <devnull@localhost> | 2009-10-24 15:40:25 +0000 |
commit | bc9332c5e33065a637797d739363236b01eb57d3 (patch) | |
tree | 9faaa37bd4947021b098a2119be454bb91735a87 /argparse.py | |
parent | 03eef47787030206ee5b34a06b1f3e8084998838 (diff) | |
download | argparse-bc9332c5e33065a637797d739363236b01eb57d3.tar.gz |
Better error messages for invalid actions.
Diffstat (limited to 'argparse.py')
-rw-r--r-- | argparse.py | 13 |
1 files changed, 9 insertions, 4 deletions
diff --git a/argparse.py b/argparse.py index bfa966c..503cbaf 100644 --- a/argparse.py +++ b/argparse.py @@ -118,6 +118,10 @@ except NameError: result.reverse() return result + +def _callable(obj): + return hasattr(obj, '__call__') or hasattr(obj, '__bases__') + # silence Python 2.6 buggy warnings about Exception.message if _sys.version_info[:2] == (2, 6): import warnings @@ -1261,6 +1265,8 @@ class _ActionsContainer(object): # create the action object, and add it to the parser action_class = self._pop_action_class(kwargs) + if not _callable(action_class): + raise ValueError('unknown action "%s"' % action_class) action = action_class(**kwargs) return self._add_action(action) @@ -2166,10 +2172,9 @@ class ArgumentParser(_AttributeHolder, _ActionsContainer): def _get_value(self, action, arg_string): type_func = self._registry_get('type', action.type, action.type) - if not hasattr(type_func, '__call__'): - if not hasattr(type_func, '__bases__'): # classic classes - msg = _('%r is not callable') - raise ArgumentError(action, msg % type_func) + if not _callable(type_func): + msg = _('%r is not callable') + raise ArgumentError(action, msg % type_func) # convert the value to the appropriate type try: |