[英]Can I give a click option another name?
I use click
like this: 我使用
click
这样:
import click
@click.command(name='foo')
@click.option('--bar', required=True)
def do_something(bar):
print(bar)
So the name of the option and the name of the function parameter is the same. 因此选项的名称和函数参数的名称是相同的。 When it is not the same (eg when you want
--id
instead of --bar
), I get: 当它是不一样的(例如,当你想
--id
代替--bar
),我得到:
TypeError: do_something() got an unexpected keyword argument 'id'
I don't want the parameter to be called id
because that is a Python function. 我不希望该参数被称为
id
因为这是一个Python函数。 I don't want the CLI parameter to be called different, because it would be more cumbersome / less intuitive. 我不希望将CLI参数调用为不同,因为它会更麻烦/更不直观。 How can I fix it?
我该如何解决?
You just need to add a non-option (doesn't start with -
) argument in the click.option
decorator. 您只需要在
click.option
装饰器中添加一个非选项(不以-
开头)参数。 Click will use this as the name of the parameter to the function. 单击将使用此作为函数参数的名称。 This allows you to use Python keywords as option names.
这允许您使用Python关键字作为选项名称。
Here is an example which uses id_
inside the function: 这是一个在函数内使用
id_
的例子:
import click
@click.command(name='foo')
@click.option('--id', 'id_', required=True)
def do_something(id_):
print(id_)
There is an official example here in the --from option. 有一个官方的例子这里在--from选项。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.