How to use typer - 10 common examples

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 jefftriplett / personal-goals / tasks.py View on Github external
import maya
import typer
import yaml

from datetime import date, datetime
from math import ceil
from pathlib import Path


ACCOMPLISHMENT_TEMPLATE = Path("templates", "weekly.md")
OUTPUT_FOLDER = Path("accomplishments", "{year}")
README_TEMPLATE = Path("templates", "README.md")
TASKS_DATA = Path("data", "tasks.yml")


app = typer.Typer()


@app.command()
def accomplishment(date: str = "now", overwrite: bool = False):
    parsed_date = maya.when(date, timezone="US/Central").datetime(naive=True)

    day_of_month = parsed_date.day
    week_number = (day_of_month - 1) // 7 + 1

    output_filename = f"{parsed_date.year}-{parsed_date.month:02}-week{week_number}.md"
    output_filename = Path(
        str(OUTPUT_FOLDER.joinpath(output_filename)).format(year=parsed_date.year)
    )

    typer.echo(str(output_filename))
github tiangolo / typer / tests / test_tutorial / test_first_steps / test_tutorial003.py View on Github external
import subprocess

import typer
from typer.testing import CliRunner

from docs_src.first_steps import tutorial003 as mod

runner = CliRunner()

app = typer.Typer()
app.command()(mod.main)


def test_1():
    result = runner.invoke(app, ["Camila"])
    assert result.exit_code != 0
    assert "Error: Missing argument 'LASTNAME'" in result.output


def test_2():
    result = runner.invoke(app, ["Camila", "Gutiérrez"])
    assert result.exit_code == 0
    assert "Hello Camila Gutiérrez" in result.output


def test_script():
github tiangolo / typer / tests / test_tutorial / test_arguments / test_envvar / test_tutorial002.py View on Github external
import subprocess

import typer
from typer.testing import CliRunner

from docs_src.arguments.envvar import tutorial002 as mod

runner = CliRunner()

app = typer.Typer()
app.command()(mod.main)


def test_help():
    result = runner.invoke(app, ["--help"])
    assert result.exit_code == 0
    assert "[OPTIONS] [NAME]" in result.output
    assert "Arguments:" in result.output
    assert "[env var: AWESOME_NAME, GOD_NAME;default: World]" in result.output


def test_call_arg():
    result = runner.invoke(app, ["Wednesday"])
    assert result.exit_code == 0
    assert "Hello Mr. Wednesday" in result.output
github tiangolo / typer / tests / test_tutorial / test_parameter_types / test_file / test_tutorial003.py View on Github external
import subprocess
from pathlib import Path

import typer
from typer.testing import CliRunner

from docs_src.parameter_types.file import tutorial003 as mod

runner = CliRunner()

app = typer.Typer()
app.command()(mod.main)


def test_main(tmpdir):
    binary_file = Path(tmpdir) / "config.txt"
    binary_file.write_bytes(b"la cig\xc3\xbce\xc3\xb1a trae al ni\xc3\xb1o")
    result = runner.invoke(app, ["--file", f"{binary_file}"])
    binary_file.unlink()
    assert result.exit_code == 0
    assert "Processed bytes total:" in result.output


def test_script():
    result = subprocess.run(
        ["coverage", "run", mod.__file__, "--help"],
        stdout=subprocess.PIPE,
github tiangolo / typer / tests / test_tutorial / test_prompt / test_tutorial003.py View on Github external
import subprocess

import typer
from typer.testing import CliRunner

from docs_src.prompt import tutorial003 as mod

runner = CliRunner()

app = typer.Typer()
app.command()(mod.main)


def test_cli():
    result = runner.invoke(app, input="y\n")
    assert result.exit_code == 0
    assert "Are you sure you want to delete it? [y/N]:" in result.output
    assert "Deleting it!" in result.output


def test_no_confirm():
    result = runner.invoke(app, input="n\n")
    assert result.exit_code == 1
    assert "Are you sure you want to delete it? [y/N]:" in result.output
    assert "Aborted!" in result.output
github tiangolo / typer / tests / test_others.py View on Github external
def test_forward_references():
    app = typer.Typer()

    @app.command()
    def main(arg1, arg2: int, arg3: "int", arg4: bool = False, arg5: "bool" = False):
        typer.echo(f"arg1: {type(arg1)} {arg1}")
        typer.echo(f"arg2: {type(arg2)} {arg2}")
        typer.echo(f"arg3: {type(arg3)} {arg3}")
        typer.echo(f"arg4: {type(arg4)} {arg4}")
        typer.echo(f"arg5: {type(arg5)} {arg5}")

    result = runner.invoke(app, ["Hello", "2", "invalid"])
    assert (
        "Error: Invalid value for 'ARG3': invalid is not a valid integer"
        in result.stdout
    )
    result = runner.invoke(app, ["Hello", "2", "3", "--arg4", "--arg5"])
    assert (
github tiangolo / typer / tests / test_tutorial / test_options / test_help / test_tutorial002.py View on Github external
import subprocess

import typer
from typer.testing import CliRunner

from docs_src.options.help import tutorial002 as mod

runner = CliRunner()

app = typer.Typer()
app.command()(mod.main)


def test_call():
    result = runner.invoke(app)
    assert result.exit_code == 0
    assert "Hello Wade Wilson" in result.output


def test_help():
    result = runner.invoke(app, ["--help"])
    assert result.exit_code == 0
    assert "--fullname TEXT" in result.output
    assert "[default: Wade Wilson]" not in result.output
github tiangolo / typer / tests / test_others.py View on Github external
    def main(name: str = typer.Option(..., callback=name_callback)):
        typer.echo("Hello World")
github tiangolo / typer / tests / test_tutorial / test_commands / test_callback / test_tutorial003.py View on Github external
import subprocess

from typer.testing import CliRunner

from docs_src.commands.callback import tutorial003 as mod

app = mod.app

runner = CliRunner()


def test_app():
    result = runner.invoke(app, ["create", "Camila"])
    assert result.exit_code == 0
    assert "Override callback, running a command" in result.output
    assert "Running a command" not in result.output
    assert "Creating user: Camila" in result.output


def test_for_coverage():
    mod.callback()


def test_script():
    result = subprocess.run(
github tiangolo / typer / tests / test_tutorial / test_options / test_version / test_tutorial003.py View on Github external
import os
import subprocess

import typer
from typer.testing import CliRunner

from docs_src.options.version import tutorial003 as mod

runner = CliRunner()

app = typer.Typer()
app.command()(mod.main)


def test_1():
    result = runner.invoke(app, ["--name", "Rick", "--version"])
    assert result.exit_code == 0
    assert "Awesome CLI Version: 0.1.0" in result.output


def test_2():
    result = runner.invoke(app, ["--name", "rick"])
    assert result.exit_code != 0
    assert "Error: Invalid value for '--name': Only Camila is allowed" in result.output