def test_option_custom_class_reusable(runner): """Ensure we can reuse a custom class option. See Issue #926""" class CustomOption(click.Option): def get_help_record(self, ctx): """a dumb override of a help text for testing""" return ("--help", "I am a help text") # Assign to a variable to re-use the decorator. testoption = click.option("--testoption", cls=CustomOption, help="you wont see me") @click.command() @testoption def cmd1(testoption): click.echo(testoption) @click.command() @testoption def cmd2(testoption): click.echo(testoption) # Both of the commands should have the --help option now. for cmd in (cmd1, cmd2): result = runner.invoke(cmd, ["--help"]) assert "I am a help text" in result.output assert "you wont see me" not in result.output
def _proc(proc): args = ( "-p", "--path", ) if with_path else ("--hidden_path") proc = click.option( *args, "path_", nargs=2, type=(P, P), multiple=True, help="Parameter (name value), as path", hidden=not with_path, )(proc) args = ( "-e", "--eval", ) if with_path else ("--hidden_eval") proc = click.option( *args, "eval_", nargs=2, type=(P, str), multiple=True, help="Parameter (name value), evaluated", hidden=not with_eval, )(proc) proc = click.option( "-v", "--var", "vars_", nargs=2, type=(P, str), multiple=True, help="Parameter (name value)", )(proc) return proc
def test_nargs_envvar(runner, nargs, value, expect): if nargs == -1: param = click.argument("arg", envvar="X", nargs=nargs) else: param = click.option("--arg", envvar="X", nargs=nargs) @click.command() @param def cmd(arg): return arg result = runner.invoke(cmd, env={"X": value}, standalone_mode=False) if isinstance(expect, str): assert isinstance(result.exception, click.BadParameter) assert expect in result.exception.format_message() else: assert result.return_value == expect