]> git.ipfire.org Git - thirdparty/samba.git/commitdiff
samba-tool: make --color a general option
authorDouglas Bagnall <douglas.bagnall@catalyst.net.nz>
Fri, 9 Sep 2022 02:48:29 +0000 (14:48 +1200)
committerAndrew Bartlett <abartlet@samba.org>
Fri, 16 Sep 2022 05:46:36 +0000 (05:46 +0000)
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 <douglas.bagnall@catalyst.net.nz>
Reviewed-by: Andrew Bartlett <abartlet@samba.org>
python/samba/netcmd/__init__.py
python/samba/netcmd/drs.py
python/samba/netcmd/visualize.py

index 4cfd65bba36be48d8ed15f231fddf211603c2e3e..94bb763e0d39fbeb3179415cb3fb3ce51f0518d3 100644 (file)
@@ -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
index 52620090a640de20733668a90b43d629e3f7370b..2262eaf62d97cb9f1c1247ddfaaa7d1b3d5ce212 100644 (file)
@@ -92,6 +92,7 @@ class cmd_drs_showrepl(Command):
     """Show replication status."""
 
     synopsis = "%prog [<DC>] [options]"
+    use_colour = False
 
     takes_optiongroups = {
         "sambaopts": options.SambaOptions,
index ea8e696ee614e8106d672440897cbe1cc42f3ed2..325cb14e46c13fd7d088cfa26d140409bf5cd2e4 100644 (file)
@@ -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()