如何为Python的click.version_option添加另一个名称选项?

huangapple go评论67阅读模式
英文:

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"名称选项,但是我得到以下错误:

如何为Python的click.version_option添加另一个名称选项?

这两种方式都不起作用,因为位置参数不能出现在关键字参数之后(第一行),而第二行不起作用,我不确定为什么:

@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:

如何为Python的click.version_option添加另一个名称选项?

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! 如何为Python的click.version_option添加另一个名称选项?

答案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()

huangapple
  • 本文由 发表于 2023年2月14日 20:02:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/75447557.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定