Open luto opened 6 years ago
We are deep in Argparser territory here. The strings are printed by the exit method form the ArgumentParser class. The logic that raises this is in the internal __getvalue methode:
def _get_value(self, action, arg_string):
type_func = self._registry_get('type', action.type, action.type)
if not _callable(type_func):
msg = _('%r is not callable')
raise ArgumentError(action, msg % type_func)
# convert the value to the appropriate type
try:
result = type_func(arg_string)
# ArgumentTypeErrors indicate errors
except ArgumentTypeError:
name = getattr(action.type, '__name__', repr(action.type))
msg = str(_sys.exc_info()[1])
raise ArgumentError(action, msg)
# TypeErrors or ValueErrors also indicate errors
except (TypeError, ValueError):
name = getattr(action.type, '__name__', repr(action.type))
msg = _('invalid %s value: %r')
raise ArgumentError(action, msg % (name, arg_string))
One workaround could be to subclass the ArgumentParser
class and override its error
method, to raise errors as exception instead of printing and exiting, so that we could handle the output ourself and restrict it for _noecho options: