Refactors _Namespace to make the code clearer

The original implentation of _Namespace._get_cli_value relied on the
fact that _Namespace.default doesn't exist and accessing it would raise
an AttributeError. This attribute was being caught for another reason so
the logic just worked.

The more correct thing to do would have been to just raise a KeyError.
This change does that and removes the need to catch an AttributeErrors.

Change-Id: I963b31ea6ea8ab5180314be7cd2450395263e1af
Closes-bug: #1284968
This commit is contained in:
David Stanek 2014-06-13 03:10:34 +00:00
parent f18797ab7d
commit ef6f9bc46a

View File

@ -1447,15 +1447,14 @@ class _Namespace(argparse.Namespace):
"""
for group_name, name in names:
name = name if group_name is None else group_name + '_' + name
try:
value = getattr(self, name)
if value is not None:
# argparse ignores default=None for nargs='*'
if positional and not value:
value = self.default
return value
except AttributeError:
pass
value = getattr(self, name, None)
if value is not None:
# argparse ignores default=None for nargs='*' and returns []
if positional and not value:
continue
return value
raise KeyError
def _get_value(self, names, multi, positional):