tiagoantao / ty_cli

Easy command line interfaces in Python using explict typing

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

ty_cli

Build Status Code style: black

Intro

ty_cli is an extremely opinionated command line parsing library. Because it decides everything for you, its interface is dead simple. Its particularly good if you need a quick CLI interface with almost zero effort.

It is based on and requires type annotations (à la mypy). Uses Python 3.8 or above.

Examples

Hello world

from ty_cli import cli

@cli
def hello(name: str) -> None:
    """Hello world: the typical example.
    
    Here is some documentation"""
    print(f"Hello {name}")
    
hello()

python hello.py --help

python hello.py Randi

Named parameters

If you want, you can force the naming on parameters on the CLI (exactly as you would force it in Python, of course):

from ty_cli import cli

@cli
def hello(*, name: str) -> None:
    print(f"Hello {name}")

python hello.py --name Randi

Multiple commands

ty_cli supports multiple commands:

from typing import Optional

from ty_cli import cli


@cli
def greet(*, first_name: str, last_name: Optional[str]) -> None:
    if last_name is None:
        print(f"Howdy {first_name}!")
    else:
        print(f"Greetings {first_name} {last_name}")


@cli
def bye(*, name: str) -> None:
    print(f"Bye {name}!")


if __name__ == "__main__":
    cli()

This can be used as follows:

python multi_hello.py greet --first-name Randi
Howdy Randi!

python multi_hello.py greet --first-name Randi --last-name Taira
Greetings Randi Taira

python multi_hello.py bye --name Randi
Bye Randi!

Documentation

Documentation is available with more examples

Legalese

Copyright 2019- Tiago Rodrigues Antao.

Licensed under the GNU Affero General Public License version 3.

About

Easy command line interfaces in Python using explict typing

License:GNU Affero General Public License v3.0


Languages

Language:Python 94.6%Language:Makefile 5.4%