Skip to content

Commit

Permalink
Make parsed args available to help formatter.
Browse files Browse the repository at this point in the history
  • Loading branch information
hjoliver committed Jan 18, 2022
1 parent a3eb15f commit 7ce3e31
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 33 deletions.
22 changes: 5 additions & 17 deletions cylc/flow/option_parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,32 +133,20 @@ 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
Else:
- 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:
Expand Down
2 changes: 1 addition & 1 deletion cylc/flow/scripts/cylc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
27 changes: 12 additions & 15 deletions tests/unit/test_option_parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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)

0 comments on commit 7ce3e31

Please sign in to comment.