编号
您可以使用max
和min
值来定义int
和float
CLI参数的数值验证。
import typer
from typing_extensions import Annotated
def main(
id: Annotated[int, typer.Argument(min=0, max=1000)],
age: Annotated[int, typer.Option(min=18)] = 20,
score: Annotated[float, typer.Option(max=100)] = 0,
):
print(f"ID is {id}")
print(f"--age is {age}")
print(f"--score is {score}")
if __name__ == "__main__":
typer.run(main)
提示
如果可能,建议使用Annotated
版本。
import typer
def main(
id: int = typer.Argument(..., min=0, max=1000),
age: int = typer.Option(20, min=18),
score: float = typer.Option(0, max=100),
):
print(f"ID is {id}")
print(f"--age is {age}")
print(f"--score is {score}")
if __name__ == "__main__":
typer.run(main)
CLI参数和CLI选项都可以使用这些验证。
您可以指定min
、max
或两者。
检查一下
$ python main.py --help
// Notice the extra RANGE in the help text for --age and --score
Usage: main.py [OPTIONS] ID
Arguments:
ID [required]
Options:
--age INTEGER RANGE [default: 20]
--score FLOAT RANGE [default: 0]
--help Show this message and exit.
// Pass all the CLI parameters
$ python main.py 5 --age 20 --score 90
ID is 5
--age is 20
--score is 90.0
// Pass an invalid ID
$ python main.py 1002
Usage: main.py [OPTIONS] ID
Try "main.py --help" for help.
Error: Invalid value for 'ID': 1002 is not in the range 0<=x<=1000.
// Pass an invalid age
$ python main.py 5 --age 15
Usage: main.py [OPTIONS] ID
Try "main.py --help" for help.
Error: Invalid value for '--age': 15 is not in the range x>=18.
// Pass an invalid score
$ python main.py 5 --age 20 --score 100.5
Usage: main.py [OPTIONS] ID
Try "main.py --help" for help.
Error: Invalid value for '--score': 100.5 is not in the range x<=100.
// But as we didn't specify a minimum score, this is accepted
$ python main.py 5 --age 20 --score -5
ID is 5
--age is 20
--score is -5.0
数字钳位¶
您可能希望使用最接近的最小或最大有效值,而不是显示错误。
您可以使用clamp
参数来实现。
import typer
from typing_extensions import Annotated
def main(
id: Annotated[int, typer.Argument(min=0, max=1000)],
rank: Annotated[int, typer.Option(max=10, clamp=True)] = 0,
score: Annotated[float, typer.Option(min=0, max=100, clamp=True)] = 0,
):
print(f"ID is {id}")
print(f"--rank is {rank}")
print(f"--score is {score}")
if __name__ == "__main__":
typer.run(main)
提示
如果可能,建议使用Annotated
版本。
import typer
def main(
id: int = typer.Argument(..., min=0, max=1000),
rank: int = typer.Option(0, max=10, clamp=True),
score: float = typer.Option(0, min=0, max=100, clamp=True),
):
print(f"ID is {id}")
print(f"--rank is {rank}")
print(f"--score is {score}")
if __name__ == "__main__":
typer.run(main)
然后,当您传递超出有效范围的数据时,它将被“钳位”,将使用最接近的有效值。
// ID doesn't have clamp, so it shows an error
$ python main.py 1002
Usage: main.py [OPTIONS] ID
Try "main.py --help" for help.
Error: Invalid value for 'ID': 1002 is not in the range 0<=x<=1000.
// But --rank and --score use clamp
$ python main.py 5 --rank 11 --score -5
ID is 5
--rank is 10
--score is 0
计数器CLI选项¶
您可以使用counter
参数使CLI选项作为计数器工作。
import typer
from typing_extensions import Annotated
def main(verbose: Annotated[int, typer.Option("--verbose", "-v", count=True)] = 0):
print(f"Verbose level is {verbose}")
if __name__ == "__main__":
typer.run(main)
提示
如果可能,建议使用Annotated
版本。
import typer
def main(verbose: int = typer.Option(0, "--verbose", "-v", count=True)):
print(f"Verbose level is {verbose}")
if __name__ == "__main__":
typer.run(main)
这意味着CLI选项将像一个布尔标志,例如--verbose
。
您在函数中收到的值将是--verbose
添加的次数。
// Check it
$ python main.py
Verbose level is 0
// Now use one --verbose
$ python main.py --verbose
Verbose level is 1
// Now 3 --verbose
$ python main.py --verbose --verbose --verbose
Verbose level is 3
// And with the short name
$ python main.py -v
Verbose level is 1
// And with the short name 3 times
$ python main.py -v -v -v
Verbose level is 3
// As short names can be put together, this also works
$ python main.py -vvv
Verbose level is 3