Skip to content

Commit

Permalink
✨ Add CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
Kludex committed Jul 5, 2023
1 parent bd57e9e commit 841db82
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 1 deletion.
7 changes: 6 additions & 1 deletion copier.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@ _subdirectory: template
project_name:
type: str
help: What's the name of the project?
default: package-name
default: Package Name

cli:
type: bool
help: Do you want to add a CLI?
default: true

description:
type: str
Expand Down
10 changes: 10 additions & 0 deletions template/{{ project_slug }}/pyproject.toml.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,14 @@ classifiers = [
]
license = "MIT"
requires-python = ">=3.8"
{% if cli -%}
dependencies = [
"typer>=0.9.0,<0.10.0",
"rich>=13.0.0,<14.0.0",
]
{% else -%}
dependencies = []
{% endif -%}
optional-dependencies = {}
dynamic = ["version"]
Expand All @@ -34,6 +41,9 @@ Source = "https://github.com/{{ github }}/{{ project_slug }}"
Twitter = "https://twitter.com/{{ twitter }}"
Funding = "https://github.com/sponsors/{{ github }}"
[project.scripts]
{{ project_slug }} = "{{ package }}.__main__:app"
[tool.hatch.version]
path = "{{ package }}/__init__.py"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import platform
import typer

from {{ package }} import __version__

app = typer.Typer()


def version_callback(value: bool) -> None:
if value:
typer.echo(
"Running {{ project_name }} {} with {} {} on {}.".format(
__version__,
platform.python_implementation(),
platform.python_version(),
platform.system(),
)
)
raise typer.Exit(0)


@app.callback()
def callback(
version: bool = typer.Option(
None,
"--version",
callback=version_callback,
is_eager=True,
help="Show version and exit.",
)
):
...


@app.command()
def hello(name: str):
print(f"Hello {name}")

if __name__ == "__main__":
app()

0 comments on commit 841db82

Please sign in to comment.