from samba.logger import get_samba_logger
from samba.samdb import SamDB
from samba.dcerpc.security import SDDLValueError
+from samba import getopt as options
from .encoders import JSONEncoder
option_class=Option)
parser.add_options(self.takes_options)
optiongroups = {}
+
+ # let samba-tool subcommands process --version
+ if "versionopts" not in self.takes_optiongroups:
+ self.takes_optiongroups["_versionopts"] = options.VersionOptions
+
for name in sorted(self.takes_optiongroups.keys()):
optiongroup = self.takes_optiongroups[name]
optiongroups[name] = optiongroup(parser)
parser.add_option_group(optiongroups[name])
+
if self.use_colour:
parser.add_option("--color",
help="use colour if available (default: auto)",
return -1
# Filter out options from option groups
+ #
+ # run() methods on subclasses receive all direct options as
+ # keyword arguments, but options that come from OptionGroups
+ # (for example, --configfile from SambaOpts group) are removed
+ # from the direct keyword arguments list, and the option group
+ # itself becomes a keyword argument. The option can be
+ # accessed as an attribute of that (e.g. sambaopts.configfile).
+ #
+ # This allows for option groups to grow without needing to
+ # change the signature for all samba-tool tools.
+ #
+ # _versionopts special case.
+ # ==========================
+ #
+ # The _versionopts group was added automatically, and is
+ # removed here. It only has the -V/--version option, and that
+ # will have triggered already if given (as will --help, and
+ # errors on unknown options).
+ #
+ # Some subclasses take 'versionopts' which they expect to
+ # receive but never use.
+
kwargs = dict(opts.__dict__)
- for option_group in parser.option_groups:
+
+ for og_name, option_group in optiongroups.items():
for option in option_group.option_list:
if option.dest is not None and option.dest in kwargs:
del kwargs[option.dest]
- kwargs.update(optiongroups)
+ if og_name != '_versionopts':
+ kwargs[og_name] = option_group
if kwargs.get('output_format') == 'json':
self.preferred_output_format = 'json'