How to use the typer.run function in typer

To help you get started, we’ve selected a few typer examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github tiangolo / typer / docs_src / options / version / tutorial001.py View on Github external
def version_callback(value: bool):
    if value:
        typer.echo(f"Awesome CLI Version: {__version__}")
        raise typer.Exit()


def main(
    name: str = typer.Option("World"),
    version: bool = typer.Option(None, "--version", callback=version_callback),
):
    typer.echo(f"Hello {name}")


if __name__ == "__main__":
    typer.run(main)
github tiangolo / typer / docs_src / terminating / tutorial003.py View on Github external
import typer


def main(username: str):
    if username == "root":
        typer.echo("The root user is reserved")
        raise typer.Abort()
    typer.echo(f"New user created: {username}")


if __name__ == "__main__":
    typer.run(main)
github tiangolo / typer / docs_src / app_dir / tutorial001.py View on Github external
from pathlib import Path

import typer

APP_NAME = "my-super-cli-app"


def main():
    app_dir = typer.get_app_dir(APP_NAME)
    config_path: Path = Path(app_dir) / "config.json"
    if not config_path.is_file():
        typer.echo("Config file doesn't exist yet")


if __name__ == "__main__":
    typer.run(main)
github tiangolo / typer / docs_src / first_steps / tutorial001.py View on Github external
import typer


def main():
    typer.echo("Hello World")


if __name__ == "__main__":
    typer.run(main)
github tiangolo / typer / docs_src / parameter_types / enum / tutorial001.py View on Github external
import typer


class NeuralNetwork(str, Enum):
    simple = "simple"
    conv = "conv"
    lstm = "lstm"


def main(network: NeuralNetwork = NeuralNetwork.simple):
    typer.echo(f"Training neural network of type: {network.value}")


if __name__ == "__main__":
    typer.run(main)
github tiangolo / typer / docs_src / multiple_values / arguments_with_multiple_values / tutorial002.py View on Github external
from typing import Tuple

import typer


def main(
    names: Tuple[str, str, str] = typer.Argument(
        ("Harry", "Hermione", "Ron"), help="Select 3 characters to play with"
    )
):
    for name in names:
        typer.echo(f"Hello {name}")


if __name__ == "__main__":
    typer.run(main)
github tiangolo / typer / docs_src / launch / tutorial002.py View on Github external
def main():
    app_dir = typer.get_app_dir(APP_NAME)
    app_dir_path = Path(app_dir)
    app_dir_path.mkdir(parents=True, exist_ok=True)
    config_path: Path = Path(app_dir) / "config.json"
    if not config_path.is_file():
        config_path.write_text('{"version": "1.0.0"}')
    config_file_str = str(config_path)
    typer.echo("Opening config directory")
    typer.launch(config_file_str, locate=True)


if __name__ == "__main__":
    typer.run(main)
github niosh-mining / obsplus / scripts / make_docs.py View on Github external
if "ipynb_checkpoints" in str(note_book_path):
            continue
        result = run(cmd + f" {note_book_path}", shell=True)
        if result.returncode != 0:
            raise RuntimeError(f"failed to run {note_book_path}")
    # run auto api-doc
    run("sphinx-apidoc ../obsplus -e -M -o api", cwd=doc_path, shell=True)
    run("make html", cwd=doc_path, shell=True, check=True)
    # ensure html directory was created, return path to it.
    expected_path: Path = doc_path / "_build" / "html"
    assert expected_path.is_dir(), f"{expected_path} does not exist!"
    return str(expected_path)


if __name__ == "__main__":
    typer.run(make_docs)