def __call__(self, parser, namespace, values, option_string=None): if values is None: values = True else: try: values = self.convert_default(values) except ValueError: raise configargparse.ArgumentError(self, "Requires boolean value") setattr(namespace, self.dest, values)
def _check_value(action, value): """Helper function to overload argparse's choice checker.""" if action.choices is not None and value not in action.choices: args = { "value": value, "choices": ", ".join(map(repr, action.choices)) } msg = _argparse.argparse._( "invalid choice: %(value)r (choose from %(choices)s)") # If the value is a string, then strip whitespace and try a case insensitive search. if type(value) is str: new_value = value.replace(" ", "").upper() choices = [x.replace(" ", "").upper() for x in action.choices] # Check whether we now have a match. if new_value not in choices: raise _argparse.ArgumentError(action, msg % args) else: raise _argparse.ArgumentError(action, msg % args)
def error(self, message): raise configargparse.ArgumentError(None, message)