安装
pip install typer typer-cli
函数参数等于命令行参数
import typerdef test(name, age): print(f"{name}, {age}")if __name__ == '__main__': typer.run(test)
python test.py zhangsan 100
使用装饰器
import typerapp = typer.Typer(help="help提示字符串")@app.command()def test1(name, age): print(f"test1 -> {name}, {age}")@app.command()def test2(name, age): print(f"test2 -> {name}, {age}")if __name__ == '__main__': app()
python test.py test1 a 1python test.py test2 b 2
补全
typer --install-completion# run 后面 tab 键typer example_2.py run 子命令 参数1 参数2 --可选参数1 可选参数1的值
自动生成文档
typer test.py utils docs --name "python test.py" --output readme.md