英文:
How to Add Another Name Option for Python click.version_option?
问题
我的问题与https://stackoverflow.com/questions/68691821/how-to-add-option-name-to-the-version-option 中的问题完全相同
我有代码,并且可以使用命令"py main.py --version"打印出我的库的版本。
请参考:https://click.palletsprojects.com/en/8.1.x/api/#click.version_option
@click.version_option(version=version)
def main():
pass
但是,我想要添加另一个名为"-V"的选项,我该如何做?文档和代码库似乎没有提供添加另一个名称选项的选项,就像"--help"参数一样。(参见:https://click.palletsprojects.com/en/8.1.x/api/#click.Context.help_option_names)
我尝试在param_decls中添加"-v"名称选项,但是我得到以下错误:
这两种方式都不起作用,因为位置参数不能出现在关键字参数之后(第一行),而第二行不起作用,我不确定为什么:
@click.version_option(version=version, "--version", "-V")
@click.version_option("--version", "-V", version=version)
请参考@aaossa提供的答案!:D
英文:
My question is exactly the same as in https://stackoverflow.com/questions/68691821/how-to-add-option-name-to-the-version-option-in-click
I have the code, and I am able to print out the version of my library using the command "py main.py --version"
See: https://click.palletsprojects.com/en/8.1.x/api/#click.version_option
@click.version_option(version=version)
def main():
pass
However, I would like to add another name option "-V", how can I do this? The documentation and codebase doesn't seem to have this option to add another name option like for the --help argument. (See: https://click.palletsprojects.com/en/8.1.x/api/#click.Context.help_option_names)
I have tried to add the "-v" name option in the param_decls, however I get the following error:
Neither of these work either, since positional arguments can't come after keyword arguments (first line), and I'm not sure why the second line doesn't work:
@click.version_option(version=version, "--version", "-V")
@click.version_option("--version", "-V", version=version)
Please refer to the answer provided by @aaossa below!
答案1
得分: 0
你可以使用位置参数 param_decls
来定义额外的选项名称(根据文档中的描述)。
以下是一个示例:
import click
version = "0.0.1"
@click.version_option(version, "--version", "-V")
@click.command()
def main():
pass
if __name__ == '__main__':
main()
英文:
You should be able to define additional option names using the positional argument param_decls
("One or more option names." according to the docs)
Here's an example:
import click
version = "0.0.1"
@click.version_option(version, "--version", "-V")
@click.command()
def main():
pass
if __name__ == '__main__':
main()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论