作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想编写一个以 FILENAME 作为参数的 CLI hello,除非给出了选项 -s STRING,在这种情况下直接处理 STRING。程序应该打印 hello {NAME}
其中 name 要么通过 STRING 给出,要么被视为文件 FILENAME 的内容。
例子:
$ cat myname.txt
john
$ hello myname.txt
hello john
$ hello -s paul
hello paul
一种可能的解决方法是使用两个选项:
@click.command()
@click.option("-f", "--filename", type=str)
@click.option("-s", "--string", type=str)
def main(filename: str, string: str):
if filename:
...
if string:
....
但这意味着我必须调用
hello
有一面旗帜。
最佳答案
您可以使用点击 argument并使其不需要,例如:
import click
@click.command()
@click.argument('filename', required=False)
@click.option("-s", "--string")
def main(filename, string):
if None not in (filename, string):
raise click.ClickException('filename argument and option string are mutually exclusive!')
click.echo(f'Filename: {filename}')
click.echo(f'String: {string}')
关于python - 单击带有可选参数的工具,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65719432/
我正在尝试用 Swift 编写这段 JavaScript 代码:k_combinations 到目前为止,我在 Swift 中有这个: import Foundation import Cocoa e
我是一名优秀的程序员,十分优秀!