Skip to main content

Run Typer scripts with completion, without having to create a package, using Typer CLI.

Project description

Typer CLI

Run Typer scripts with completion, without having to create a package, using Typer CLI.

Build Status Coverage Package version


⚠️ WARNING ⚠️ If you are building a CLI package you probably need Typer, the library itself. This, Typer CLI, is a CLI application that simplifies running simple Typer scripts, it is not the library itself.

Description

Typer is a library for building CLIs (Command Line Interface applications).

Typer CLI (this package) is a CLI application that simplifies running simple programs created with Typer.

Typer CLI's main feature is to provide completion in the Terminal for your own small programs built with Typer, without you having to create a complete installable Python package with them.

It's probably most useful if you have a small custom Python script using Typer (maybe as part of some project), for some small tasks, and it's not complex/important enough to create a whole installable Python package for it (something to be installed with pip).

In that case, you can install Typer CLI, and run your program with the typer command in your Terminal, and it will provide completion for your script.

Usage

Install

Install Typer CLI:

$ python -m pip install typer-cli

That creates a typer command.

You can then install completion for it:

$ typer --install-completion

Sample script

Let's say you have a script that uses Typer in my_custom_script.py:

import typer

app = typer.Typer()


@app.command()
def hello(name: str = None):
    if name:
        typer.echo(f"Hello {name}")
    else:
        typer.echo("Hello World!")


@app.command()
def bye(name: str = None):
    if name:
        typer.echo(f"Bye {name}")
    else:
        typer.echo("Goodbye!")


if __name__ == "__main__":
    app()

Run with Python

Then you could run your script with normal Python:

$ python my_custom_script.py hello

Hello World!

$ python my_custom_script.py hello --name Camila

Hello Camila!

$ python my_custom_script.py bye --name Camila

Bye Camila

There's nothing wrong with running with Python directly. And, in fact, if some other code or program uses your script, that would be the best way to do it.

⛔️ But in your terminal, you won't get completion when hitting TAB for any of the subcommands or options, like hello, bye, and --name.

Run with Typer CLI

Here's where Typer CLI is useful.

You can also run the same script with the typer command you get after installing typer-cli:

$ typer my_custom_script.py run hello

Hello World!

$ typer my_custom_script.py run hello --name Camila

Hello Camila!

$ typer my_custom_script.py run bye --name Camila

Bye Camila
  • Instead of using python directly you type typer.
  • After the name of the file you add the subcommand run.

✔️ If you installed completion as described above, when you hit TAB you will have autocompletion for everything, including the run and all the subcommands and options of your script, like hello, bye, and --name.

If main

Because Typer CLI won't use the block with:

if __name__ == "__main__":
    app()

You can also remove it if you are calling that script only with Typer CLI (using the typer command).

License

This project is licensed under the terms of the MIT license.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

typer-cli-0.0.3.tar.gz (5.2 kB view details)

Uploaded Source

Built Distribution

typer_cli-0.0.3-py3-none-any.whl (5.3 kB view details)

Uploaded Python 3

File details

Details for the file typer-cli-0.0.3.tar.gz.

File metadata

  • Download URL: typer-cli-0.0.3.tar.gz
  • Upload date:
  • Size: 5.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.0.5 CPython/3.6.7 Linux/4.15.0-1028-gcp

File hashes

Hashes for typer-cli-0.0.3.tar.gz
Algorithm Hash digest
SHA256 cf09d7c80c7da5718cfa285ceae9f397a943607e943343b70c9d4fb415fc2729
MD5 3487bd7752daf80efa64ae964d1595c7
BLAKE2b-256 49a07b5fc66671c749e57e0b450410acbd3b1779e5519fd0ea7638c68fad91b9

See more details on using hashes here.

Provenance

File details

Details for the file typer_cli-0.0.3-py3-none-any.whl.

File metadata

  • Download URL: typer_cli-0.0.3-py3-none-any.whl
  • Upload date:
  • Size: 5.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.0.5 CPython/3.6.7 Linux/4.15.0-1028-gcp

File hashes

Hashes for typer_cli-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 ba4a732c7c861867805e432dbcf9ee49de41310fdfad2519d496b295c49dc4cf
MD5 226ef148dfbc15645415512697897189
BLAKE2b-256 739e6e8bd3be1f5bbd33956fc411ed21eae3bdb43e40ce91f6e1676c3a5b4722

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page