def test_make_pass_decorator_args(runner): """ Test to check that make_pass_decorator doesn't consume arguments based on invocation order. """ class Foo(object): title = 'foocmd' pass_foo = click.make_pass_decorator(Foo) @click.group() @click.pass_context def cli(ctx): ctx.obj = Foo() @cli.command() @click.pass_context @pass_foo def test1(foo, ctx): click.echo(foo.title) @cli.command() @pass_foo @click.pass_context def test2(ctx, foo): click.echo(foo.title) result = runner.invoke(cli, ['test1']) assert not result.exception assert result.output == 'foocmd\n' result = runner.invoke(cli, ['test2']) assert not result.exception assert result.output == 'foocmd\n'
def test_ensure_context_objects(runner): class Foo(object): def __init__(self): self.title = 'default' pass_foo = click.make_pass_decorator(Foo, ensure=True) @click.group() @pass_foo def cli(foo): pass @cli.command() @pass_foo def test(foo): click.echo(foo.title) result = runner.invoke(cli, ['test']) assert not result.exception assert result.output == 'default\n'
def test_get_context_objects_missing(runner): class Foo(object): pass pass_foo = click.make_pass_decorator(Foo) @click.group() @click.pass_context def cli(ctx): pass @cli.command() @pass_foo def test(foo): click.echo(foo.title) result = runner.invoke(cli, ['test']) assert result.exception is not None assert isinstance(result.exception, RuntimeError) assert "Managed to invoke callback without a context object " \ "of type 'Foo' existing" in str(result.exception)
def test_get_context_objects_no_ensuring(runner): class Foo(object): def __init__(self): self.title = 'default' pass_foo = click.make_pass_decorator(Foo) @click.group() @click.pass_context def cli(ctx): ctx.obj = Foo() ctx.obj.title = 'test' @cli.command() @pass_foo def test(foo): click.echo(foo.title) result = runner.invoke(cli, ['test']) assert not result.exception assert result.output == 'test\n'
def test_get_context_objects(runner): class Foo(object): def __init__(self): self.title = "default" pass_foo = click.make_pass_decorator(Foo, ensure=True) @click.group() @click.pass_context def cli(ctx): ctx.obj = Foo() ctx.obj.title = "test" @cli.command() @pass_foo def test(foo): click.echo(foo.title) result = runner.invoke(cli, ["test"]) assert not result.exception assert result.output == "test\n"
from typing import cast import asyncclick as click from kasa import ( Discover, SmartBulb, SmartDevice, SmartLightStrip, SmartPlug, SmartStrip, ) click.anyio_backend = "asyncio" pass_dev = click.make_pass_decorator(SmartDevice) @click.group(invoke_without_command=True) @click.option( "--host", envvar="KASA_HOST", required=False, help="The host name or IP address of the device to connect to.", ) @click.option( "--alias", envvar="KASA_NAME", required=False, help="The device name, or alias, of the device to connect to.", )
self.verbose = False self.home = os.getcwd() def log(self, msg, *args): """Logs a message to stderr.""" if args: msg %= args click.echo(msg, file=sys.stderr) def vlog(self, msg, *args): """Logs a message to stderr only if verbose is enabled.""" if self.verbose: self.log(msg, *args) pass_environment = click.make_pass_decorator(Environment, ensure=True) cmd_folder = os.path.abspath( os.path.join(os.path.dirname(__file__), "commands")) class ComplexCLI(click.MultiCommand): def list_commands(self, ctx): rv = [] for filename in os.listdir(cmd_folder): if filename.endswith(".py") and filename.startswith("cmd_"): rv.append(filename[4:-3]) rv.sort() return rv def get_command(self, ctx, name): try:
parser.read([filename]) try: self.aliases.update(parser.items("aliases")) except configparser.NoSectionError: pass def write_config(self, filename): parser = configparser.RawConfigParser() parser.add_section("aliases") for key, value in self.aliases.items(): parser.set("aliases", key, value) with open(filename, "wb") as file: parser.write(file) pass_config = click.make_pass_decorator(Config, ensure=True) class AliasedGroup(click.Group): """This subclass of a group supports looking up aliases in a config file and with a bit of magic. """ def get_command(self, ctx, cmd_name): # Step one: bulitin commands as normal rv = click.Group.get_command(self, ctx, cmd_name) if rv is not None: return rv # Step two: find the config object and ensure it's there. This # will create the config object is missing.
class Repo(object): def __init__(self, home): self.home = home self.config = {} self.verbose = False def set_config(self, key, value): self.config[key] = value if self.verbose: click.echo(" config[{}] = {}".format(key, value), file=sys.stderr) def __repr__(self): return "<Repo {}>".format(self.home) pass_repo = click.make_pass_decorator(Repo) @click.group() @click.option( "--repo-home", envvar="REPO_HOME", default=".repo", metavar="PATH", help="Changes the repository folder location.", ) @click.option( "--config", nargs=2, multiple=True, metavar="KEY VALUE",