typer

2025-12-11 0 841

typer ,建造了很棒的CLIS。易于编码。基于Python型提示。


文档https://typer.tiangolo.com

源代码: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许可证的条款获得许可的。

下载源码

通过命令行克隆项目:

git clone https://github.com/fastapi/typer.git

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

申明:本文由第三方发布,内容仅代表作者观点,与本网站无关。对本文以及其中全部或者部分内容的真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。本网发布或转载文章出于传递更多信息之目的,并不意味着赞同其观点或证实其描述,也不代表本网对其真实性负责。

左子网 编程相关 typer https://www.zuozi.net/34456.html

RustPython
上一篇: RustPython
maigret
下一篇: maigret
常见问题
  • 1、自动:拍下后,点击(下载)链接即可下载;2、手动:拍下后,联系卖家发放即可或者联系官方找开发者发货。
查看详情
  • 1、源码默认交易周期:手动发货商品为1-3天,并且用户付款金额将会进入平台担保直到交易完成或者3-7天即可发放,如遇纠纷无限期延长收款金额直至纠纷解决或者退款!;
查看详情
  • 1、描述:源码描述(含标题)与实际源码不一致的(例:货不对板); 2、演示:有演示站时,与实际源码小于95%一致的(但描述中有”不保证完全一样、有变化的可能性”类似显著声明的除外); 3、发货:不发货可无理由退款; 4、安装:免费提供安装服务的源码但卖家不履行的; 5、收费:价格虚标,额外收取其他费用的(但描述中有显著声明或双方交易前有商定的除外); 6、其他:如质量方面的硬性常规问题BUG等。 注:经核实符合上述任一,均支持退款,但卖家予以积极解决问题则除外。
查看详情
  • 1、左子会对双方交易的过程及交易商品的快照进行永久存档,以确保交易的真实、有效、安全! 2、左子无法对如“永久包更新”、“永久技术支持”等类似交易之后的商家承诺做担保,请买家自行鉴别; 3、在源码同时有网站演示与图片演示,且站演与图演不一致时,默认按图演作为纠纷评判依据(特别声明或有商定除外); 4、在没有”无任何正当退款依据”的前提下,商品写有”一旦售出,概不支持退款”等类似的声明,视为无效声明; 5、在未拍下前,双方在QQ上所商定的交易内容,亦可成为纠纷评判依据(商定与描述冲突时,商定为准); 6、因聊天记录可作为纠纷评判依据,故双方联系时,只与对方在左子上所留的QQ、手机号沟通,以防对方不承认自我承诺。 7、虽然交易产生纠纷的几率很小,但一定要保留如聊天记录、手机短信等这样的重要信息,以防产生纠纷时便于左子介入快速处理。
查看详情

相关文章

猜你喜欢
发表评论
暂无评论
官方客服团队

为您解决烦忧 - 24小时在线 专业服务