diff options
author | Bob Whitelock <bob.whitelock1@gmail.com> | 2020-08-23 00:02:26 +0100 |
---|---|---|
committer | Bob Whitelock <bob.whitelock1@gmail.com> | 2020-08-23 00:02:26 +0100 |
commit | 2432a2ba87271a22c8e7b43092f6b874f403463c (patch) | |
tree | 15755bfe1edbe13948a2a92b9e334c17ad5847bf /dotbot/cli.py | |
parent | 1d4f4348bba8462c247eb63656907556b5da2dd3 (diff) |
Add `--force-color` option
This forces Dotbot to produce colored output, regardless of whether it
is outputting to a TTY.
This is useful to support use cases such as piping colored Dotbot output
into another program for formatting (e.g. I want to indent the output as
part of a larger installation script); this was not previously easy to
do as this would cause the output to lose its colored formatting.
This option cannot be provided at the same time as the existing
`--no-color` option, as there's no logical interpretation of what effect
providing both of these should have.
As part of this change I've refactored some existing code determining
whether output should be colored to where options are parsed, as this
made this change simpler and I think it makes sense for all this logic
to be performed in the same place.
Diffstat (limited to 'dotbot/cli.py')
-rw-r--r-- | dotbot/cli.py | 14 |
1 files changed, 13 insertions, 1 deletions
diff --git a/dotbot/cli.py b/dotbot/cli.py index b400dd6..32db016 100644 --- a/dotbot/cli.py +++ b/dotbot/cli.py @@ -1,4 +1,5 @@ import os, glob +import sys from argparse import ArgumentParser from .config import ConfigReader, ReadingError @@ -32,6 +33,8 @@ def add_options(parser): help='only run specified directives', metavar='DIRECTIVE') parser.add_argument('--except', nargs='+', dest='skip', help='skip specified directives', metavar='DIRECTIVE') + parser.add_argument('--force-color', dest='force_color', action='store_true', + help='force color output') parser.add_argument('--no-color', dest='no_color', action='store_true', help='disable color output') parser.add_argument('--version', action='store_true', @@ -56,8 +59,17 @@ def main(): log.set_level(Level.INFO) if options.verbose: log.set_level(Level.DEBUG) - if options.no_color: + + if options.force_color and options.no_color: + log.error("`--force-color` and `--no-color` cannot both be provided") + exit(1) + elif options.force_color: + log.use_color(True) + elif options.no_color: log.use_color(False) + else: + log.use_color(sys.stdout.isatty()) + plugin_directories = list(options.plugin_dirs) if not options.disable_built_in_plugins: from .plugins import Clean, Create, Link, Shell |