typer ,建造了很棒的CLIS。易于编码。基于Python型提示。
源代码:https://github.com/fastapi/typer
typer是一个用于构建CLI应用程序的图书馆,用户会喜欢使用,开发人员会喜欢创建。基于Python型提示。
它也是运行脚本的命令行工具,将它们自动转换为CLI应用程序。
关键功能是:
- 直观的写作:出色的编辑支持。到处完成。更少的时间调试。设计为易于使用和学习。更少的时间阅读文档。
- 易于使用:最终用户易于使用。自动帮助,并自动完成所有外壳。
- 简短:最小化代码重复。每个参数声明中的多个功能。更少的错误。
- 启动简单:最简单的示例仅在应用程序中添加2行代码: 1导入,1个函数调用。
- 大大生长:随心所欲地增长,以选项和参数创建任意复杂的命令和子命令群的树木。
- 运行脚本: typer包括一个typer命令/程序,您可以使用该脚本来运行脚本,即使它们在内部不使用typer ,也可以自动将其转换为CLIS。
克里斯的Fastapi
typer是Fastapi的小兄弟姐妹,它是CLI的Fastapi。
安装
创建并激活虚拟环境,然后安装typer :
typer
—> 100%
Successfully installed typer rich shellingham\”>
$ pip install typer ---> 100% Successfully installed typer rich shellingham
例子
绝对最小值
- 使用以下方式创建一个文件main.py
def main ( name : str ): print ( f\"Hello { name } \" )
该脚本甚至都不在内部使用typer 。但是您可以使用typer命令将其作为CLI应用程序运行。
运行它
使用typer命令运行您的应用程序:
typer main.py run
// You get a nice error, you are missing NAME
Usage: typer [PATH_OR_MODULE] run [OPTIONS] NAME
Try \’ typer [PATH_OR_MODULE] run –help\’ for help.
╭─ Error ───────────────────────────────────────────╮
│ Missing argument \’NAME\’. │
╰───────────────────────────────────────────────────╯
// You get a –help for free
$ typer main.py run –help
Usage: typer [PATH_OR_MODULE] run [OPTIONS] NAME
Run the provided typer app.
╭─ Arguments ───────────────────────────────────────╮
│ * name TEXT [default: None] [required] |
╰───────────────────────────────────────────────────╯
╭─ Options ─────────────────────────────────────────╮
│ –help Show this message and exit. │
╰───────────────────────────────────────────────────╯
// Now pass the NAME argument
$ typer main.py run Camila
Hello Camila
// It works! ?\”>
// Run your application $ typer main.py run // You get a nice error, you are missing NAME Usage: typer [PATH_OR_MODULE] run [OPTIONS] NAME Try \' typer [PATH_OR_MODULE] run --help\' for help. ╭─ Error ───────────────────────────────────────────╮ │ Missing argument \'NAME\'. │ ╰───────────────────────────────────────────────────╯ // You get a --help for free $ typer main.py run --help Usage: typer [PATH_OR_MODULE] run [OPTIONS] NAME Run the provided typer app. ╭─ Arguments ───────────────────────────────────────╮ │ * name TEXT [default: None] [required] | ╰───────────────────────────────────────────────────╯ ╭─ Options ─────────────────────────────────────────╮ │ --help Show this message and exit. │ ╰───────────────────────────────────────────────────╯ // Now pass the NAME argument $ typer main.py run Camila Hello Camila // It works! ?
这是最简单的用例,甚至没有内部使用typer ,但是对于简单的脚本来说,它已经非常有用。
注意:当您创建Python软件包并使用-Antall-Completion或使用typer命令时,自动完成工作就可以工作。
在您的代码中使用typer
现在,让我们开始在您自己的代码中使用typer ,请更新main.py:
typer
def main(name: str):
print(f\”Hello {name}\”)
if __name__ == \”__main__\”:
typer .run(main)\”>
import typer def main ( name : str ): print ( f\"Hello { name } \" ) if __name__ == \"__main__\" : typer . run ( main )
现在,您可以直接使用Python运行它:
// Run your application $ python main.py // You get a nice error, you are missing NAME Usage: main.py [OPTIONS] NAME Try \'main.py --help\' for help. ╭─ Error ───────────────────────────────────────────╮ │ Missing argument \'NAME\'. │ ╰───────────────────────────────────────────────────╯ // You get a --help for free $ python main.py --help Usage: main.py [OPTIONS] NAME ╭─ Arguments ───────────────────────────────────────╮ │ * name TEXT [default: None] [required] | ╰───────────────────────────────────────────────────╯ ╭─ Options ─────────────────────────────────────────╮ │ --help Show this message and exit. │ ╰───────────────────────────────────────────────────╯ // Now pass the NAME argument $ python main.py Camila Hello Camila // It works! ?
注意:您也可以使用typer命令调用此相同的脚本,但您不需要。
示例升级
这是最简单的例子。
现在,让我们看看一个更复杂的地方。
两个子命令的示例
修改文件main.py。
创建一个typer 。 typer ()应用程序,并创建两个带有参数的子命令。
typer
app = typer . typer ()
@app.command()
def hello(name: str):
print(f\”Hello {name}\”)
@app.command()
def goodbye(name: str, formal: bool = False):
if formal:
print(f\”Goodbye Ms. {name}. Have a good day.\”)
else:
print(f\”Bye {name}!\”)
if __name__ == \”__main__\”:
app()\”>
import typer app = typer . typer () @ app . command () def hello ( name : str ): print ( f\"Hello { name } \" ) @ app . command () def goodbye ( name : str , formal : bool = False ): if formal : print ( f\"Goodbye Ms. { name } . Have a good day.\" ) else : print ( f\"Bye { name } !\" ) if __name__ == \"__main__\" : app ()
那将是:
- 明确创建一个typer 。 typer应用程序。
- 以前的typer .run实际上是为您隐含的。
- 使用 @app.command()添加两个子命令。
- 执行App()本身,就好像它是一个函数(而不是typer .run)。
运行升级的示例
检查新帮助:
$ python main.py --help Usage: main.py [OPTIONS] COMMAND [ARGS]... ╭─ Options ─────────────────────────────────────────╮ │ --install-completion Install completion │ │ for the current │ │ shell. │ │ --show-completion Show completion for │ │ the current shell, │ │ to copy it or │ │ customize the │ │ installation. │ │ --help Show this message │ │ and exit. │ ╰───────────────────────────────────────────────────╯ ╭─ Commands ────────────────────────────────────────╮ │ goodbye │ │ hello │ ╰───────────────────────────────────────────────────╯ // When you create a package you get auto-completion for free, installed with --install-completion // You have 2 subcommands (the 2 functions): goodbye and hello
现在检查Hello命令的帮助:
$ python main.py hello --help Usage: main.py hello [OPTIONS] NAME ╭─ Arguments ───────────────────────────────────────╮ │ * name TEXT [default: None] [required] │ ╰───────────────────────────────────────────────────╯ ╭─ Options ─────────────────────────────────────────╮ │ --help Show this message and exit. │ ╰───────────────────────────────────────────────────╯
现在检查Goodbye命令的帮助:
$ python main.py goodbye --help Usage: main.py goodbye [OPTIONS] NAME ╭─ Arguments ───────────────────────────────────────╮ │ * name TEXT [default: None] [required] │ ╰───────────────────────────────────────────────────╯ ╭─ Options ─────────────────────────────────────────╮ │ --formal --no-formal [default: no-formal] │ │ --help Show this message │ │ and exit. │ ╰───────────────────────────────────────────────────╯ // Automatic --formal and --no-formal for the bool option ?
现在,您可以尝试新的命令行应用程序:
// Use it with the hello command $ python main.py hello Camila Hello Camila // And with the goodbye command $ python main.py goodbye Camila Bye Camila! // And with --formal $ python main.py goodbye --formal Camila Goodbye Ms. Camila. Have a good day.
回顾
总而言之,您将参数类型( CLI参数和CLI选项)声明为函数参数。
您可以使用标准的现代Python类型来做到这一点。
您不必学习新的语法,特定库的方法或类别等。
只是标准python 。
例如,对于INT:
total : int
或用于布尔国旗:
force : bool
同样,对于文件,路径,枚举(选择)等,也有一些工具来创建子命令组,添加元数据,额外验证等。
您会得到:出色的编辑支持,包括到处的完成和类型检查。
您的用户获得:自动– 螺旋,在安装您的包装或使用typer命令时,在其终端中的自动完成(bash,zsh,fish,powershell)。
有关包括更多功能的更完整的示例,请参见教程 – 用户指南。
依赖性
typer站在巨人的肩膀上。它唯一需要的内部依赖性是单击。
默认情况下,它还带有额外的标准依赖项:
- Rich:自动显示格式良好的错误。
- Shellingham:安装完成时自动检测当前外壳。
- 使用Shellingham,您可以使用安装 – 完整。
- 没有Shellingham,您必须传递外壳的名称,以安装完成,例如安装 – 完整bash。
typer slim
如果您不想要额外的标准可选依赖项,请安装typer -slim。
当您安装时:
pip install typer
…它包含与以下方式相同的代码和依赖项
pip install \" typer -slim[standard] \"
标准的额外依赖性是丰富和贝壳汉。
注意: typer命令仅包含在typer软件包中。
执照
该项目是根据MIT许可证的条款获得许可的。
