如何在 python `click` 中指定 3 个必需选项
我正在尝试使用 https://click.palletsprojects.com/en/8.0。 x/options/
我有一个用例,其中这 3 个参数之一必须是必填字段。
我就是这样做的。
10:45 $ python 测试.py 用法:test.py [OPTIONS]
错误:必须指定 --foo 或 bar 或 car
import click
@click.command()
@click.option('--foo', help='foo is bar.')
@click.option('--count', help='Number of greetings.')
@click.option('--name',
help='The person to greet.')
def hello(foo, count, name):
if not (count or name or foo):
raise click.UsageError( 'Must specify --foo or bar or car')
click.echo(f"Hello {name}!")
if __name__ == '__main__':
hello()
有没有办法将这些参数之一显示为必填字段。像这样的:
python test.py --help
Usage: test.py [OPTIONS]
Options:
--foo TEXT foo is bar
or --count TEXT Number of greetings
or --name TEXT The person to greet [required]
--help Show this message and exit.
尝试 1:
https://click.palletsprojects。 com/en/8.0.x/options/#feature-switches
import click
@click.command()
@click.option('--foo', flag_value='foo', required=True, help='foo is bar.')
@click.option('--count', flag_value='count', required=True, help='Number of greetings.')
@click.option('--name', flag_value='name', required=True, help='The person to greet.')
def hello(foo, count, name):
if not (count or name or foo):
raise click.UsageError( 'Must specify --foo or bar or car')
if foo:
click.echo(f"Hello {foo}!")
if count:
click.echo(f"Hello {count}!")
if name:
click.echo(f"Hello {name}!")
if __name__ == '__main__':
hello()
I am trying to use https://click.palletsprojects.com/en/8.0.x/options/
I have a use case where one out of these 3 parameters has to be a required field.
This is how I am doing it.
10:45 $ python test.py
Usage: test.py [OPTIONS]
Error: Must specify --foo or bar or car
import click
@click.command()
@click.option('--foo', help='foo is bar.')
@click.option('--count', help='Number of greetings.')
@click.option('--name',
help='The person to greet.')
def hello(foo, count, name):
if not (count or name or foo):
raise click.UsageError( 'Must specify --foo or bar or car')
click.echo(f"Hello {name}!")
if __name__ == '__main__':
hello()
Is there a way to show one of these params as required field. something like this:
python test.py --help
Usage: test.py [OPTIONS]
Options:
--foo TEXT foo is bar
or --count TEXT Number of greetings
or --name TEXT The person to greet [required]
--help Show this message and exit.
Try 1:
https://click.palletsprojects.com/en/8.0.x/options/#feature-switches
import click
@click.command()
@click.option('--foo', flag_value='foo', required=True, help='foo is bar.')
@click.option('--count', flag_value='count', required=True, help='Number of greetings.')
@click.option('--name', flag_value='name', required=True, help='The person to greet.')
def hello(foo, count, name):
if not (count or name or foo):
raise click.UsageError( 'Must specify --foo or bar or car')
if foo:
click.echo(f"Hello {foo}!")
if count:
click.echo(f"Hello {count}!")
if name:
click.echo(f"Hello {name}!")
if __name__ == '__main__':
hello()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的 try1 代码可以工作,但用户必须输入
--foo
开关。尝试1
但你想要
使用 此答案,作者: @Stephen Rauch 提供互斥体来实现依赖选项,我认为这是你的答案。
这是代码
运行
相关链接
python Click 中互斥的选项组
Your try1 code works but user must enter the
--foo
switch.Try1
But you want
Using this answer by @Stephen Rauch that provide mutex to implement dependence option I think its your answer.
Here is the Code
Run
Related link
Mutually exclusive option groups in python Click