Last active
February 14, 2024 14:57
-
-
Save joshuadavidthomas/f4bf90790d5d8ca11ad378f4b042d73a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from __future__ import annotations | |
import click | |
@click.command() | |
@click.argument("name", type=str, default="World") | |
def main(name: str): | |
print(f"Hello, {name.title()}!") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()}!") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[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"] |
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
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 asrc/test
directory within with an__init__.py
file and the four scripts provided.