Skip to content

Tweak callback evaluation order of eagerness options #137

@kdeldycke

Description

@kdeldycke

Eager parameters are evaluated in the order as they were provided on the command line by the user as explained in: https://click.palletsprojects.com/en/8.0.x/advanced/#callback-evaluation-order

This means a call to:

  • --no-color --version will output a plain, uncoloured version string, while
  • --version --no-color will output a coloured string.

This is highlighted by a test at:

@skip_windows_colors
def test_color_option_precedence(invoke):
"""--no-color has an effect on --version, if placed in the right order.
Eager parameters are evaluated in the order as they were provided on the command
line by the user as expleined in:
https://click.palletsprojects.com/en/8.0.x/advanced/#callback-evaluation-order
..todo:
Maybe have the possibility to tweak CLI callback evaluation order so we can
let the user to have the NO_COLOR env set to allow for color-less --version output.
"""
@click.command()
@color_option()
@version_option(version="2.1.9")
def dummy_cli():
click.echo(Style(fg="yellow")("It works!"))
result = invoke(dummy_cli, "--no-color", "--version", "command1", color=True)
assert result.exit_code == 0
assert result.output == "dummy-cli, version 2.1.9\n"
assert not result.stderr
result = invoke(dummy_cli, "--version", "--no-color", "command1", color=True)
assert result.exit_code == 0
assert result.output == "\x1b[97mdummy-cli\x1b[0m, version \x1b[32m2.1.9\x1b[0m\n"
assert not result.stderr

It could be great to have --no-color (and its NO_COLOR env var) be respected whatever its order.

Metadata

Metadata

Assignees

No one assigned

    Labels

    🐛 bugSomething isn't working, or a fix is proposed🙏 help wantedI can't do this alone and need contributors

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions