From: Rob van der Linde Date: Thu, 5 Oct 2023 23:58:46 +0000 (+1300) Subject: python: getopt: implement required flag on options and OptionParser X-Git-Tag: talloc-2.4.2~1126 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=e60f3afcc5fca7ec1d8aacfc7abf19009b3d831c;p=thirdparty%2Fsamba.git python: getopt: implement required flag on options and OptionParser Signed-off-by: Rob van der Linde Reviewed-by: Douglas Bagnall Reviewed-by: Andrew Bartlett --- diff --git a/python/samba/getopt.py b/python/samba/getopt.py index 4fae3c07700..11af22f496e 100644 --- a/python/samba/getopt.py +++ b/python/samba/getopt.py @@ -73,6 +73,26 @@ def check_bytes(option, opt, value): raise optparse.OptionValueError(msg) +class OptionMissingError(optparse.OptionValueError): + """One or more Options with required=True is missing.""" + + def __init__(self, options): + """Raised when required Options are missing from the command line. + + :param options: list of 1 or more option + """ + self.options = options + + def __str__(self): + if len(self.options) == 1: + missing = self.options[0] + return f"Argument {missing} is required." + else: + options = sorted([str(option) for option in self.options]) + missing = ", ".join(options) + return f"The arguments {missing} are required." + + class ValidationError(Exception): """ValidationError is the exception raised by validators. @@ -93,7 +113,7 @@ class Validator(metaclass=ABCMeta): class Option(optparse.Option): - ATTRS = optparse.Option.ATTRS + ["validators"] + ATTRS = optparse.Option.ATTRS + ["required", "validators"] TYPES = optparse.Option.TYPES + ("bytes",) TYPE_CHECKER = copy(optparse.Option.TYPE_CHECKER) TYPE_CHECKER["bytes"] = check_bytes @@ -136,6 +156,20 @@ class OptionParser(optparse.OptionParser): conflict_handler, description, formatter, add_help_option, prog, epilog) + def check_values(self, values, args): + """Loop through required options if value is missing raise exception.""" + missing = [] + for option in self._get_all_options(): + if option.required: + value = getattr(values, option.dest) + if value is None: + missing.append(option) + + if missing: + raise OptionMissingError(missing) + + return super().check_values(values, args) + class SambaOptions(optparse.OptionGroup): """General Samba-related command line options."""