是否可以将python @ click.option装饰器重复用于多个命令? [英] Is it possible to reuse python @click.option decorators for multiple commands?

查看:50
本文介绍了是否可以将python @ click.option装饰器重复用于多个命令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个Python CLI工具,它们共享一组常用的click.options.目前,通用选项已重复:

I have two Python CLI tools which share a set of common click.options. At the moment, the common options are duplicated:

@click.command()
@click.option('--foo', is_flag=True)
@click.option('--bar', is_flag=True)
@click.option('--unique-flag-1', is_flag=True)
def command_one():
    pass

@click.command()
@click.option('--foo', is_flag=True)
@click.option('--bar', is_flag=True)
@click.option('--unique-flag-2', is_flag=True)
def command_two():
    pass

是否可以将通用选项提取到可以应用于每个功能的单个装饰器中?

Is it possible to extract the common options in to a single decorator that can be applied to each function?

推荐答案

您可以构建自己的装饰器来封装常用选项:

You can build your own decorator that encapsulates the common options:

def common_options(function):
    function = click.option('--unique-flag-1', is_flag=True)(function)
    function = click.option('--bar', is_flag=True)(function)
    function = click.option('--foo', is_flag=True)(function)
    return function

@click.command()
@common_options
def command():
    pass

这篇关于是否可以将python @ click.option装饰器重复用于多个命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆