From 7ce3e312aa70e8fa2dac6e14155db666061820f1 Mon Sep 17 00:00:00 2001 From: Hilary James Oliver Date: Tue, 18 Jan 2022 09:19:21 +1300 Subject: [PATCH] Make parsed args available to help formatter. --- cylc/flow/option_parsers.py | 22 +++++----------------- cylc/flow/scripts/cylc.py | 2 +- tests/unit/test_option_parsers.py | 27 ++++++++++++--------------- 3 files changed, 18 insertions(+), 33 deletions(-) diff --git a/cylc/flow/option_parsers.py b/cylc/flow/option_parsers.py index 69854a2194a..180ef6d91f8 100644 --- a/cylc/flow/option_parsers.py +++ b/cylc/flow/option_parsers.py @@ -133,9 +133,8 @@ class CylcHelpFormatter(IndentedHelpFormatter): def _format(self, text): """Format help (usage) text on the fly to handle coloring. - This has to be done here because help is printed to the terminal before - command line parsing is completed, and color initialization for general - command output is done after that to make use of parsed options. + Help is printed to the terminal before color initialization for general + command output. If coloring is wanted: - Add color tags to shell examples @@ -143,22 +142,11 @@ def _format(self, text): - Strip any hardwired color tags """ - # Crudely parse sys.argv for --color options here (see docstring above; - # only default option values are available here in self.parser.values). - # Join the command line with '=' to unify "--opt=val" and "--opt val": - joined_args = '='.join(sys.argv[2:]) use_color = ( - '--color=always' in joined_args - or - '--colour=always' in joined_args + self.parser.values.color == "always" or ( - supports_color() - and not - ( - '--color=never' in joined_args - or - '--colour=never' in joined_args - ) + self.parser.values.color == "auto" + and supports_color() ) ) if use_color: diff --git a/cylc/flow/scripts/cylc.py b/cylc/flow/scripts/cylc.py index d4810258852..6000f8eeda3 100644 --- a/cylc/flow/scripts/cylc.py +++ b/cylc/flow/scripts/cylc.py @@ -630,7 +630,7 @@ def main(): # check if this is a command abbreviation or exit command = match_command(command) if opts.help_: - execute_cmd(command, "--help") + execute_cmd(command, *cmd_args, "--help") else: if opts.version: cmd_args.append("--version") diff --git a/tests/unit/test_option_parsers.py b/tests/unit/test_option_parsers.py index c8c794ff642..190548a186f 100644 --- a/tests/unit/test_option_parsers.py +++ b/tests/unit/test_option_parsers.py @@ -31,11 +31,6 @@ def parser(): return COP('usage') -@pytest.fixture(scope='module') -def parser_usage_with_comment(): - return COP(USAGE_WITH_COMMENT) - - @pytest.mark.parametrize( 'args,verbosity', [ ([], 0), @@ -59,25 +54,27 @@ def test_verbosity(args, verbosity, parser, monkeypatch): assert cylc.flow.flags.verbosity == verbosity -def test_help_color(parser_usage_with_comment): +def test_help_color(monkeypatch): """Test for colorized comments in 'cylc cmd --help --color=always'.""" # This colorization is done on the fly when help is printed. - argv = sys.argv - sys.argv = ['cmd', 'arg', '--help', '--color=always'] + monkeypatch.setattr("sys.argv", ['cmd', 'foo', '--color=always']) + parser = COP(USAGE_WITH_COMMENT) + parser.parse_args(['default-arg']) + assert parser.values.color == "always" f = io.StringIO() with redirect_stdout(f): - parser_usage_with_comment.print_help() - sys.argv = argv + parser.print_help() assert not (f.getvalue()).startswith("Usage: " + USAGE_WITH_COMMENT) -def test_help_nocolor(parser_usage_with_comment): +def test_help_nocolor(monkeypatch): """Test for no colorization in 'cylc cmd --help --color=never'.""" # This colorization is done on the fly when help is printed. - argv = sys.argv - sys.argv = ['cmd', 'arg', '--help', '--color=never'] + monkeypatch.setattr(sys, "argv", ['cmd', 'foo', '--color=never']) + parser = COP(USAGE_WITH_COMMENT) + parser.parse_args(['default-arg']) + assert parser.values.color == "never" f = io.StringIO() with redirect_stdout(f): - parser_usage_with_comment.print_help() - sys.argv = argv + parser.print_help() assert (f.getvalue()).startswith("Usage: " + USAGE_WITH_COMMENT)