Skip to content

Instantly share code, notes, and snippets.

@joshuadavidthomas
Last active February 14, 2024 14:57
Show Gist options
  • Save joshuadavidthomas/f4bf90790d5d8ca11ad378f4b042d73a to your computer and use it in GitHub Desktop.
Save joshuadavidthomas/f4bf90790d5d8ca11ad378f4b042d73a to your computer and use it in GitHub Desktop.
from __future__ import annotations
import click
@click.command()
@click.argument("name", type=str, default="World")
def main(name: str):
print(f"Hello, {name.title()}!")
from __future__ import annotations
import typer
from typing_extensions import Annotated
cli = typer.Typer()
@cli.command()
def command(name: Annotated[str, typer.Argument()] = "World") -> None:
print(f"Hello {name.title()}!")
def main():
cli()
from __future__ import annotations
import typer
from typing_extensions import Annotated
cli = typer.Typer()
@cli.command()
def command(name: Annotated[str, typer.Argument()] = "World") -> None:
print(f"Hello {name.title()}!")
from __future__ import annotations
import typer
from typing_extensions import Annotated
def command(name: Annotated[str, typer.Argument()] = "World") -> None:
print(f"Hello {name.title()}!")
def cli():
typer.run(command)
[project]
name = "test"
version = "0.1.0"
description = "Test of Click vs. Typer"
authors = [
{ name = "Josh", email = "josh@joshthomas.dev" }
]
dependencies = [
"typer[all]>=0.9.0",
"click>=8.1.7",
]
readme = "README.md"
requires-python = ">= 3.11"
[project.scripts]
hello-click = "test.hello_click:main"
hello-typer = "test.hello_typer:main"
hello-typer-alt1 = "test.hello_typer_alt1:cli"
hello-typer-alt2 = "test.hello_typer_alt2:cli"
[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"
[tool.hatch.build.targets.wheel]
packages = ["src/test"]
@joshuadavidthomas
Copy link
Author

joshuadavidthomas commented Feb 14, 2024

I've been using Rye, but this should be reproducible under any Python build tool. Make a new directory with the pyproject.toml file at the base, make a src/test directory within with an __init__.py file and the four scripts provided.

typer-test on  main [!+?]  v0.1.0  (typer-test) 
➜ tree
.
├── pyproject.toml
└── src
    └── test
        ├── __init__.py
        ├── hello_click.py
        ├── hello_typer.py
        ├── hello_typer_alt1.py
        └── hello_typer_alt2.py

@joshuadavidthomas
Copy link
Author

typer-test on  main [!+?]  v0.1.0  (typer-test) 
➜ rye run hello-click --help
Usage: hello-click [OPTIONS] [NAME]

Options:
  --help  Show this message and exit.

typer-test on  main [!+?]  v0.1.0  (typer-test) 
➜ rye run hello-click
Hello, World!

typer-test on  main [!+?]  v0.1.0  (typer-test) 
➜ rye run hello-click Josh
Hello, Josh!

typer-test on  main [!+?]  v0.1.0  (typer-test) 
➜ rye run hello-typer --help
                                                                                                                                                                          
 Usage: hello-typer [OPTIONS]                                                                                                                                             
                                                                                                                                                                          
╭─ Options ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
│ --name                      TEXT  [default: World]                                                                                                                     │
│ --install-completion              Install completion for the current shell.                                                                                            │
│ --show-completion                 Show completion for the current shell, to copy it or customize the installation.                                                     │
│ --help                            Show this message and exit.                                                                                                          │
╰────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯


typer-test on  main [!+?]  v0.1.0  (typer-test) 
➜ rye run hello-typer
Hello World!

typer-test on  main [!+?]  v0.1.0  (typer-test) 
➜ rye run hello-typer Josh
Usage: hello-typer [OPTIONS]
Try 'hello-typer --help' for help.
╭─ Error ────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
│ Got unexpected extra argument (Josh)                                                                                                                                   │
╰────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment