Last active
August 21, 2022 22:56
-
-
Save HarmonicHemispheres/15f5585b3d0be63443294593ba20fe84 to your computer and use it in GitHub Desktop.
Template Typer CLI Script - Python 3.7+
This file contains hidden or 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
""" | |
Copyright <<year>> <<insert publisher name>> | |
DESCRIPTION: | |
this is a sample Typer CLI layout | |
USAGE EXAMPLE: | |
> python template_simple_cli.py example_cmd | |
""" | |
# ::IMPORTS ------------------------------------------------------------------------ # | |
# cli framework - https://pypi.org/project/typer/ | |
import typer | |
# data types for validation - https://docs.python.org/3/library/typing.html | |
from typing import Optional | |
# cross platform path handling - https://docs.python.org/3/library/pathlib.html | |
from pathlib import Path | |
# package for reading details about this package | |
import pkg_resources | |
# ::SETUP -------------------------------------------------------------------------- # | |
app = typer.Typer(add_completion=False) | |
# ::SETUP SUBPARSERS --------------------------------------------------------------- # | |
# app.add_typer(<<module.app>>, name="subparser") | |
# ::GLOBALS --------------------------------------------------------------------- # | |
PKG_NAME = "<python package name>" | |
# ::CORE LOGIC --------------------------------------------------------------------- # | |
# place core script logic here and call functions | |
# from the cli command functions to separate CLI from business logic | |
# ::CLI ---------------------------------------------------------------------------- # | |
@app.command() | |
def example_cmd(): | |
print("hello typer!") | |
@app.command() | |
def example_cmd_args( | |
name: str, | |
last_name: str = typer.Option(default="<unknown>"), | |
age: Optional[str] = typer.Argument(None), | |
): | |
print(f"name:{name} last-name:{last_name} age:{age}") | |
@app.command() | |
def version(): | |
""" get the version of the package """ | |
version = pkg_resources.get_distribution(PKG_NAME).version | |
typer.echo(version) | |
# ::EXECUTE ------------------------------------------------------------------------ # | |
def main(): | |
app() | |
if __name__ == "__main__": # ensure importing the script will not execute | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment