Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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))
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():
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
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,
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
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 (
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
import typer
app = typer.Typer()
def old_callback():
"""
Old callback help.
"""
users_app = typer.Typer(callback=old_callback, name="exp-users", help="Explicit help.")
def new_users():
"""
I have the highland! Create some users.
"""
app.add_typer(users_app, callback=new_users)
@users_app.callback()
def users():
"""
Manage users in the app.
"""
import typer
app = typer.Typer()
@app.command(
context_settings={"allow_extra_args": True, "ignore_unknown_options": True}
)
def main(ctx: typer.Context):
for extra_arg in ctx.args:
typer.echo(f"Got extra arg: {extra_arg}")
if __name__ == "__main__":
app()
import typer
app = typer.Typer()
@app.command()
def create(username: str):
typer.echo(f"Creating user: {username}")
@app.command()
def delete(username: str):
typer.echo(f"Deleting user: {username}")
@app.callback(invoke_without_command=True)
def main():
"""
Manage users in the awesome CLI app.