python 中 2 个不同功能的完全相同的选项单击
exactly same options for 2 different functions in python click
我正在使用 Python 2 and click 编写一个工具,该工具 reads/writes 在硬件中注册。我有两个函数接受完全相同的选项。区别在于它们处理输入并将输出直接输出到不同的设备。
这是我目前的情况:
@cli.command()
@click.option('--r0', type=click.IntRange(-2, 0xffffffff, clamp=False), default=-2)
@click.option('--r1', type=click.IntRange(-2, 0xffffffff, clamp=False), default=-2)
@click.option('--r2', type=click.IntRange(-2, 0xffffffff, clamp=False), default=-2)
def mydevice1(r0, r1, r2):
# Handle inputs for device 1
click.echo('myfunc1')
@cli.command()
@click.option('--r0', type=click.IntRange(-2, 0xffffffff, clamp=False), default=-2)
@click.option('--r1', type=click.IntRange(-2, 0xffffffff, clamp=False), default=-2)
@click.option('--r2', type=click.IntRange(-2, 0xffffffff, clamp=False), default=-2)
def mydevice2(r0, r1, r2):
# Handle inputs for device 2
click.echo('myfunc2')
这两个函数将以相同的方式处理输入,唯一的区别是它们将处理的信息传递给不同的设备。换句话说,我想要的是
@click.option('--r0', type=click.IntRange(-2, 0xffffffff, clamp=False), default=-2)
@click.option('--r1', type=click.IntRange(-2, 0xffffffff, clamp=False), default=-2)
@click.option('--r2', type=click.IntRange(-2, 0xffffffff, clamp=False), default=-2)
def handle_common_options(r0, r1, r2):
# Handle common options
pass
@cli.command()
def mydevice1():
handle_common_options()
# pass processed options to device 1
@cli.command()
def mydevice2():
handle_common_options()
# pass processed options to device 2
这可能吗?
当然。
@decorator
def f():
pass
表示
def f():
pass
f = decorator(f)
所以:
decorator0 = cli.command()
decorator1 = click.option('--r0', type=click.IntRange(-2, 0xffffffff, clamp=False), default=-2)
decorator2 = click.option('--r1', type=click.IntRange(-2, 0xffffffff, clamp=False), default=-2)
decorator3 = click.option('--r2', type=click.IntRange(-2, 0xffffffff, clamp=False), default=-2)
common_decorator = lambda f: decorator0(decorator1(decorator2(decorator3(f))))
@common_decorator
def mydevice1(r0, r1, r2):
click.echo('myfunc1')
@common_decorator
def mydevice2(r0, r1, r2):
click.echo('myfunc2')
没有 lambda:
def common_decorator(f):
return decorator0(decorator1(decorator2(decorator3(f))))
我正在使用 Python 2 and click 编写一个工具,该工具 reads/writes 在硬件中注册。我有两个函数接受完全相同的选项。区别在于它们处理输入并将输出直接输出到不同的设备。
这是我目前的情况:
@cli.command()
@click.option('--r0', type=click.IntRange(-2, 0xffffffff, clamp=False), default=-2)
@click.option('--r1', type=click.IntRange(-2, 0xffffffff, clamp=False), default=-2)
@click.option('--r2', type=click.IntRange(-2, 0xffffffff, clamp=False), default=-2)
def mydevice1(r0, r1, r2):
# Handle inputs for device 1
click.echo('myfunc1')
@cli.command()
@click.option('--r0', type=click.IntRange(-2, 0xffffffff, clamp=False), default=-2)
@click.option('--r1', type=click.IntRange(-2, 0xffffffff, clamp=False), default=-2)
@click.option('--r2', type=click.IntRange(-2, 0xffffffff, clamp=False), default=-2)
def mydevice2(r0, r1, r2):
# Handle inputs for device 2
click.echo('myfunc2')
这两个函数将以相同的方式处理输入,唯一的区别是它们将处理的信息传递给不同的设备。换句话说,我想要的是
@click.option('--r0', type=click.IntRange(-2, 0xffffffff, clamp=False), default=-2)
@click.option('--r1', type=click.IntRange(-2, 0xffffffff, clamp=False), default=-2)
@click.option('--r2', type=click.IntRange(-2, 0xffffffff, clamp=False), default=-2)
def handle_common_options(r0, r1, r2):
# Handle common options
pass
@cli.command()
def mydevice1():
handle_common_options()
# pass processed options to device 1
@cli.command()
def mydevice2():
handle_common_options()
# pass processed options to device 2
这可能吗?
当然。
@decorator
def f():
pass
表示
def f():
pass
f = decorator(f)
所以:
decorator0 = cli.command()
decorator1 = click.option('--r0', type=click.IntRange(-2, 0xffffffff, clamp=False), default=-2)
decorator2 = click.option('--r1', type=click.IntRange(-2, 0xffffffff, clamp=False), default=-2)
decorator3 = click.option('--r2', type=click.IntRange(-2, 0xffffffff, clamp=False), default=-2)
common_decorator = lambda f: decorator0(decorator1(decorator2(decorator3(f))))
@common_decorator
def mydevice1(r0, r1, r2):
click.echo('myfunc1')
@common_decorator
def mydevice2(r0, r1, r2):
click.echo('myfunc2')
没有 lambda:
def common_decorator(f):
return decorator0(decorator1(decorator2(decorator3(f))))