简体   繁体   English

我可以给点击选项另一个名字吗?

[英]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.

相关问题 如何为用户提供选择播放哪种模式的选项? - How can I give the user the option of picking which mode to play in? 如何让用户选择输入另一个名字或输入数字以发布或退出程序 - How to give the user the option of entering another name or entering a number to post or quit the program 如何在python中的函数内给变量另一个值? - how can I give a variable another value inside a function in python? 如何使用 Python+selenium 单击此选项? - How can I click this option with Python+selenium? 我可以使用上下文值作为 click.option() 默认值吗? - Can I use a context value as a click.option() default? 如何从 python selenium 的下拉菜单中单击一个选项? - How can I click on an option from a dropdown menu in python selenium? 我可以根据用户使用python在列表中的位置向用户提供从列表中选择元素的选项吗? - Can I give the user an option to chose an element from the list based on its position in the list using python? 如何在Django选择框中更改选项名称 - How can I change name of option in django select box 如何在单击选项中使用 python-click 参数(检查文件中是否存在变量)? - How can I use an python-click argument from within a click option (check if variable exists in file)? Tkinter-通过类给比例的变量选项名称 - Tkinter - Give name of variable option of a Scale through a class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM