cyclopts


توضیحات

Intuitive, easy CLIs based on type hints.
ویژگی مقدار
سیستم عامل OS Independent
نام فایل cyclopts-5.3.2
نام cyclopts
نسخه کتابخانه 5.3.2
نگهدارنده []
ایمیل نگهدارنده []
نویسنده Brian Pugh
ایمیل نویسنده -
آدرس صفحه اصلی -
آدرس اینترنتی https://pypi.org/project/cyclopts/
مجوز -
<div align="center"> <img src="https://raw.githubusercontent.com/BrianPugh/Cyclopts/main/assets/logo_512w.png"> </div> <div align="center"> ![Python compat](https://img.shields.io/badge/>=python-3.11-blue.svg) [![PyPI](https://img.shields.io/pypi/v/cyclopts.svg)](https://pypi.org/project/cyclopts/) [![ReadTheDocs](https://readthedocs.org/projects/cyclopts/badge/?version=latest)](https://cyclopts.readthedocs.io) [![codecov](https://codecov.io/gh/BrianPugh/cyclopts/graph/badge.svg?token=HA393WIYUK)](https://codecov.io/gh/BrianPugh/cyclopts) </div> --- **Documentation:** https://cyclopts.readthedocs.io **Source Code:** https://github.com/BrianPugh/cyclopts --- Cyclopts is a modern, easy-to-use command-line interface (CLI) framework that aims to provide an intuitive & efficient developer experience. # Why Cyclopts? - **Intuitive API**: Quickly write CLI applications using a terse, intuitive syntax. - **Advanced Type Hinting**: Full support of all builtin types and even user-specified (yes, including [Pydantic](https://docs.pydantic.dev/latest/), [Dataclasses](https://docs.python.org/3/library/dataclasses.html), and [Attrs](https://www.attrs.org/en/stable/api.html)). - **Rich Help Generation**: Automatically generates beautiful help pages from **docstrings** and other contextual data. - **Extendable**: Easily customize converters, validators, token parsing, and application launching. # Installation Cyclopts requires Python >=3.11; to install Cyclopts, run: ```console pip install cyclopts ``` # Quick Start - Import `cyclopts.run()` and give it a function to run. ```python from cyclopts import run def foo(loops: int): for i in range(loops): print(f"Looping! {i}") run(foo) ``` Execute the script from the command line: ```console $ python start.py 3 Looping! 0 Looping! 1 Looping! 2 ``` When you need more control: - Create an application using `cyclopts.App`. - Register commands with the `command` decorator. - Register a default function with the `default` decorator. ```python 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() ``` Execute the script from the command line: ```console $ python demo.py Hello world! This runs when no command is specified. $ python demo.py foo 3 Looping! 0 Looping! 1 Looping! 2 ``` With just a few additional lines of code, we have a full-featured CLI app. See [the docs](https://cyclopts.readthedocs.io) for more advanced usage. # Compared to Typer Cyclopts is what you thought Typer was. Cyclopts includes information from docstrings, supports more complex types (even Unions!), and includes proper validation support. See [the documentation for a complete Typer comparison](https://cyclopts.readthedocs.io/en/latest/vs_typer/README.html). Consider the following short 29-line Cyclopts application: ```python import cyclopts from typing import Literal app = cyclopts.App() @app.command def deploy( env: Literal["dev", "staging", "prod"], replicas: int | Literal["default", "performance"] = "default", ): """Deploy code to an environment. Parameters ---------- env Environment to deploy to. replicas Number of workers to spin up. """ if replicas == "default": replicas = 10 elif replicas == "performance": replicas = 20 print(f"Deploying to {env} with {replicas} replicas.") if __name__ == "__main__": app() ``` ```console $ my-script deploy --help Usage: my-script.py deploy [ARGS] [OPTIONS] Deploy code to an environment. ╭─ Parameters ────────────────────────────────────────────────────────────────────────────────────╮ │ * ENV --env Environment to deploy to. [choices: dev, staging, prod] [required] │ │ REPLICAS --replicas Number of workers to spin up. [choices: default, performance] [default: │ │ default] │ ╰─────────────────────────────────────────────────────────────────────────────────────────────────╯ $ my-script deploy staging Deploying to staging with 10 replicas. $ my-script deploy staging 7 Deploying to staging with 7 replicas. $ my-script deploy staging performance Deploying to staging with 20 replicas. $ my-script deploy nonexistent-env ╭─ Error ────────────────────────────────────────────────────────────────────────────────────────────╮ │ Error converting value "nonexistent-env" to typing.Literal['dev', 'staging', 'prod'] for "--env". │ ╰────────────────────────────────────────────────────────────────────────────────────────────────────╯ $ my-script --version 0.0.0 ``` In its current state, this application would be impossible to implement in Typer; Typer does not support the union type-hint of `replicas`. However, let's see how close we can get with Typer (41-lines): ```python import typer from typing import Annotated, Literal app = typer.Typer() def replica_parser(value: str): if value == "default": return 10 elif value == "performance": return 20 else: return int(value) def _version_callback(value: bool): if value: print("0.0.0") raise typer.Exit() @app.callback() def callback( version: Annotated[ bool | None, typer.Option("--version", callback=_version_callback) ] = None, ): pass @app.command(help="Deploy code to an environment.") def deploy( env: Annotated[Literal["dev", "staging", "prod"], typer.Argument(help="Environment to deploy to.")], replicas: Annotated[ int, typer.Argument( parser=replica_parser, help="Number of workers to spin up.", ), ] = replica_parser("default"), ): print(f"Deploying to {env} with {replicas} replicas.") if __name__ == "__main__": app() ``` ```console $ my-script deploy --help Usage: my-script deploy [OPTIONS] {env}:<dev|staging|prod> [replicas] Deploy code to an environment. ╭─ Arguments ─────────────────────────────────────────────────────────────────────────────────────╮ │ * env <dev|staging|prod> Environment to deploy to. [required] │ │ replicas <replica_parser> Number of workers to spin up. [default: 10] │ ╰─────────────────────────────────────────────────────────────────────────────────────────────────╯ ╭─ Options ───────────────────────────────────────────────────────────────────────────────────────╮ │ --help Show this message and exit. │ ╰─────────────────────────────────────────────────────────────────────────────────────────────────╯ $ my-script deploy staging Deploying to staging with 10 replicas. $ my-script deploy staging 7 Deploying to staging with 7 replicas. $ my-script deploy staging performance Deploying to staging with 20 replicas. $ my-script deploy nonexistent-env Usage: my-script deploy [OPTIONS] {env}:<dev|staging|prod> [replicas] Try 'my-script deploy --help' for help. ╭─ Error ─────────────────────────────────────────────────────────────────────────────────────────╮ │ Invalid value for 'env': 'nonexistent-env' is not one of 'dev', 'staging', 'prod'. │ ╰─────────────────────────────────────────────────────────────────────────────────────────────────╯ $ my-script --version 0.0.0 ``` The Typer implementation is 41 lines long, while the Cyclopts implementation is just 29 (29% shorter!). Not only is the Cyclopts implementation significantly shorter, but the code is easier to read. Since Typer does not support Unions, the choices for ``replicas`` could not be displayed on the help page. Cyclopts is much more terse, much more readable, and much more intuitive to use. # Contributing Contributions are welcome! See [CONTRIBUTING.md](https://github.com/BrianPugh/cyclopts/blob/main/CONTRIBUTING.md) for development setup, coding standards, and how to submit a pull request.


نیازمندی

مقدار نام
- attrs>=23.1.0
- docstring-parser<4.0,>=0.15
- rich-rst<3,>=2.0.1
- rich>=13.6.0
xtr ipdb>=0.13.9;
xtr line-profiler>=3.5.1;
xtr coverage[toml]>=5.1;
xtr mkdocs>=1.4.0;
ys_platfor pexpect>=4.9.0;
xtr pre-commit>=2.16.0;
xtr pydantic<3.0.0,>=2.11.2;
xtr pytest-cov>=3.0.0;
xtr pytest-mock>=3.7.0;
xtr pytest-timeout>=2.3.0;
xtr pytest>=8.2.0;
xtr pyyaml>=6.0.1;
xtr syrupy>=4.0.0;
xtr toml<1.0.0,>=0.10.2;
xtr trio>=0.10.0;
xtr gitpython>=3.1.31;
xtr myst-parser[linkify]<6.0.0,>=3.0.1;
xtr sphinx-autodoc-typehints<4.0.0,>=1.25.2;
xtr sphinx-copybutton<1.0,>=0.5;
xtr sphinx-rtd-dark-mode<2.0.0,>=1.3.0;
xtr sphinx-rtd-theme<4.0.0,>=3.0.0;
xtr sphinx<10.0.0,>=7.4.7;
xtr markdown>=3.3;
xtr mkdocs>=1.4.0;
xtr pymdown-extensions>=10.0;
xtr trio>=0.10.0;
xtr pyyaml>=6.0.1;


زبان مورد نیاز

مقدار نام
>=3.11 Python


نحوه نصب


نصب پکیج whl cyclopts-5.3.2:

    pip install cyclopts.whl


نصب پکیج tar.gz cyclopts-5.3.2:

    pip install cyclopts.tar.gz