1

cyclopts:基于Python类型提示的直观CLI

 8 months ago
source link: https://www.jdon.com/71812.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

cyclopts:基于Python类型提示的直观CLI - 极道

Cyclopts 是一个现代、易于使用的命令行界面 (CLI) 框架。它提供了一种构建 CLI 应用程序的简化方法,强调简单性、可扩展性和稳健性。Cyclopts 旨在提供直观、高效的开发人员体验,使 python CLI 开发变得更加容易和愉快。

  • 直观的 API:Cyclopts 具有简单直观的 API,使开发人员可以轻松地用最少的代码创建复杂的 CLI 应用程序。
  • 高级类型提示:Cyclopts 提供高级类型提示功能,允许更准确和信息丰富的命令行界面。
  • 丰富的帮助生成:自动生成美观、用户友好的帮助消息,确保用户可以轻松理解和使用您的 CLI 应用程序。
  • 可扩展和可定制:Cyclopts 在设计时考虑了可扩展性,允许开发人员轻松添加自定义行为并与其他系统集成。

Cyclopts 需要 Python >=3.8;要安装 Cyclopts,请运行:
pip install cyclopts

快速开始

  1. 使用创建应用程序cyclopts.App。
  2. 向command装饰器注册命令。
  3. 向装饰器注册默认函数default。
from cyclopts import App

app = App()


@app.command
def foo(loops: int):
    for i in range(loops):
        print(f"Looping! {i}")


@app.default
def default_action():
    print("Hello world! This runs when no command is specified.")


app()

从命令行执行脚本:

$ python demo.py
Hello world! This runs when no command is specified.

$ python demo.py foo 3
Looping! 0
Looping! 1
Looping! 2

只需添加几行代码,我们就拥有了一个功能齐全的 CLI 应用程序。

与Typer比较
Cyclopts 就是你所认为的 Typer。Cyclopts 包括来自文档字符串的信息,支持更复杂的类型(甚至联合和文字!),并包括适当的验证支持。请参阅文档以获取完整的 Typer 比较

同样功能,Typer 实现有 43 行长,而 Cyclopts 实现只有 30 行,Cyclopts 更加简洁,更具可读性,并且使用起来更加直观。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK