From: Douglas Bagnall Date: Fri, 9 Sep 2022 02:48:29 +0000 (+1200) Subject: samba-tool: make --color a general option X-Git-Tag: talloc-2.4.0~974 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=5dd4696fb792cff37534eccb943be66cdd9e544c;p=thirdparty%2Fsamba.git samba-tool: make --color a general option We don't put --color into options.SambaOptions because we can't handle the 'auto' case in the options module without knowing whether or not self.outf is a tty, and a) this might not be resolved and b) is fiddly to pass through. The .use_colour class flag allows samba-tool subcommands to avoid having --color, and is *also* useful in the short term for visualise and drs commands to avoid having this --color clobber their own bespoke versions (temporarily, during the transition). Signed-off-by: Douglas Bagnall Reviewed-by: Andrew Bartlett --- diff --git a/python/samba/netcmd/__init__.py b/python/samba/netcmd/__init__.py index 4cfd65bba36..94bb763e0d3 100644 --- a/python/samba/netcmd/__init__.py +++ b/python/samba/netcmd/__init__.py @@ -84,6 +84,7 @@ class Command(object): takes_optiongroups = {} hidden = False + use_colour = True raw_argv = None raw_args = None @@ -158,6 +159,12 @@ class Command(object): 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)", + metavar="always|never|auto", + default="auto") + return parser, optiongroups def message(self, text): @@ -180,6 +187,9 @@ class Command(object): del kwargs[option.dest] kwargs.update(optiongroups) + if self.use_colour: + self.apply_colour_choice(kwargs.pop('color', 'auto')) + # Check for a min a max number of allowed arguments, whenever possible # The suffix "?" means zero or one occurence # The suffix "+" means at least one occurence diff --git a/python/samba/netcmd/drs.py b/python/samba/netcmd/drs.py index 52620090a64..2262eaf62d9 100644 --- a/python/samba/netcmd/drs.py +++ b/python/samba/netcmd/drs.py @@ -92,6 +92,7 @@ class cmd_drs_showrepl(Command): """Show replication status.""" synopsis = "%prog [] [options]" + use_colour = False takes_optiongroups = { "sambaopts": options.SambaOptions, diff --git a/python/samba/netcmd/visualize.py b/python/samba/netcmd/visualize.py index ea8e696ee61..325cb14e46c 100644 --- a/python/samba/netcmd/visualize.py +++ b/python/samba/netcmd/visualize.py @@ -92,6 +92,7 @@ class GraphCommand(Command): } takes_options = COMMON_OPTIONS + DOT_OPTIONS takes_args = () + use_colour = False def get_db(self, H, sambaopts, credopts): lp = sambaopts.get_loadparm()