simple "complex type" $click_ interface

This commit is contained in:
Jörn-Michael Miehe 2021-10-20 08:31:36 +02:00
parent fdedf20ba3
commit 217a5fa75b
7 changed files with 77 additions and 12 deletions

View file

@ -7,12 +7,12 @@
<env name="PYTHONUNBUFFERED" value="1" /> <env name="PYTHONUNBUFFERED" value="1" />
</envs> </envs>
<option name="SDK_HOME" value="" /> <option name="SDK_HOME" value="" />
<option name="WORKING_DIRECTORY" value="$PROJECT_DIR$" /> <option name="WORKING_DIRECTORY" value="$PROJECT_DIR$/example" />
<option name="IS_MODULE_SDK" value="true" /> <option name="IS_MODULE_SDK" value="true" />
<option name="ADD_CONTENT_ROOTS" value="true" /> <option name="ADD_CONTENT_ROOTS" value="true" />
<option name="ADD_SOURCE_ROOTS" value="true" /> <option name="ADD_SOURCE_ROOTS" value="true" />
<option name="SCRIPT_NAME" value="kiwi_scp.scripts.kiwi_next" /> <option name="SCRIPT_NAME" value="kiwi_scp.scripts.kiwi_next" />
<option name="PARAMETERS" value="" /> <option name="PARAMETERS" value="init" />
<option name="SHOW_COMMAND_LINE" value="false" /> <option name="SHOW_COMMAND_LINE" value="false" />
<option name="EMULATE_TERMINAL" value="false" /> <option name="EMULATE_TERMINAL" value="false" />
<option name="MODULE_MODE" value="true" /> <option name="MODULE_MODE" value="true" />

View file

20
kiwi_scp/commands/cli.py Normal file
View file

@ -0,0 +1,20 @@
import os
import click
class KiwiCLI(click.MultiCommand):
def list_commands(self, ctx):
result = []
for filename in os.listdir(os.path.abspath(os.path.dirname(__file__))):
if filename.startswith("cmd_") and filename.endswith(".py"):
result.append(filename[4:-3])
result.sort()
return result
def get_command(self, ctx, name):
try:
mod = __import__(f"kiwi_scp.commands.cmd_{name}", None, None, ["cmd"])
except ImportError:
return
return mod.cmd

View file

@ -0,0 +1,21 @@
import click
from ..config import Config
@click.command(
"init",
short_help="Initializes a repo."
)
@click.argument(
"path",
required=False,
type=click.Path(resolve_path=True)
)
@click.pass_context
def cmd(ctx, path):
"""Initializes a repository."""
kiwi: Config = ctx.obj["cfg"]
click.echo("Hello init")
click.echo(kiwi.kiwi_yml)
pass

View file

@ -1,14 +1,21 @@
from kiwi_scp.config import Config import click
import yaml import yaml
from kiwi_scp.commands.cli import KiwiCLI
from kiwi_scp.config import Config
def main():
with open("./example/kiwi.yml") as kc: @click.command(cls=KiwiCLI)
@click.pass_context
def main(ctx):
"""A complex command line interface."""
with open("./kiwi.yml") as kc:
yml = yaml.safe_load(kc) yml = yaml.safe_load(kc)
kiwi = Config(**yml) ctx.ensure_object(dict)
ctx.obj["cfg"] = Config(**yml)
print(repr(kiwi)) click.echo("Hello main")
print(kiwi.kiwi_yml)
if __name__ == "__main__": if __name__ == "__main__":

24
poetry.lock generated
View file

@ -35,11 +35,23 @@ importlib-metadata = {version = "*", markers = "python_version < \"3.8\""}
docs = ["sphinx", "jaraco.packaging (>=8.2)", "rst.linker (>=1.9)"] docs = ["sphinx", "jaraco.packaging (>=8.2)", "rst.linker (>=1.9)"]
testing = ["pytest (>=4.6)", "pytest-flake8", "pytest-cov", "pytest-black (>=0.3.7)", "pytest-mypy", "pytest-checkdocs (>=2.4)", "pytest-enabler (>=1.0.1)"] testing = ["pytest (>=4.6)", "pytest-flake8", "pytest-cov", "pytest-black (>=0.3.7)", "pytest-mypy", "pytest-checkdocs (>=2.4)", "pytest-enabler (>=1.0.1)"]
[[package]]
name = "click"
version = "8.0.3"
description = "Composable command line interface toolkit"
category = "main"
optional = false
python-versions = ">=3.6"
[package.dependencies]
colorama = {version = "*", markers = "platform_system == \"Windows\""}
importlib-metadata = {version = "*", markers = "python_version < \"3.8\""}
[[package]] [[package]]
name = "colorama" name = "colorama"
version = "0.4.4" version = "0.4.4"
description = "Cross-platform colored terminal text." description = "Cross-platform colored terminal text."
category = "dev" category = "main"
optional = false optional = false
python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.*" python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.*"
@ -75,7 +87,7 @@ testing = ["covdefaults (>=1.2.0)", "coverage (>=4)", "pytest (>=4)", "pytest-co
name = "importlib-metadata" name = "importlib-metadata"
version = "4.8.1" version = "4.8.1"
description = "Read metadata from Python packages" description = "Read metadata from Python packages"
category = "dev" category = "main"
optional = false optional = false
python-versions = ">=3.6" python-versions = ">=3.6"
@ -260,7 +272,7 @@ testing = ["coverage (>=4)", "coverage-enable-subprocess (>=1)", "flaky (>=3)",
name = "zipp" name = "zipp"
version = "3.6.0" version = "3.6.0"
description = "Backport of pathlib-compatible object wrapper for zip files" description = "Backport of pathlib-compatible object wrapper for zip files"
category = "dev" category = "main"
optional = false optional = false
python-versions = ">=3.6" python-versions = ">=3.6"
@ -271,7 +283,7 @@ testing = ["pytest (>=4.6)", "pytest-checkdocs (>=2.4)", "pytest-flake8", "pytes
[metadata] [metadata]
lock-version = "1.1" lock-version = "1.1"
python-versions = "^3.6.1" python-versions = "^3.6.1"
content-hash = "7343a635c1769b0dda5c30d310a00bf6c4dac0d64f26ae2076b9b3e6fde69700" content-hash = "eb1a3ab9af78ac7898062245858dbf9e9a27e82a6484f45f14b8db6c7fe812d6"
[metadata.files] [metadata.files]
atomicwrites = [ atomicwrites = [
@ -286,6 +298,10 @@ attrs = [
{file = "backports.entry_points_selectable-1.1.0-py2.py3-none-any.whl", hash = "sha256:a6d9a871cde5e15b4c4a53e3d43ba890cc6861ec1332c9c2428c92f977192acc"}, {file = "backports.entry_points_selectable-1.1.0-py2.py3-none-any.whl", hash = "sha256:a6d9a871cde5e15b4c4a53e3d43ba890cc6861ec1332c9c2428c92f977192acc"},
{file = "backports.entry_points_selectable-1.1.0.tar.gz", hash = "sha256:988468260ec1c196dab6ae1149260e2f5472c9110334e5d51adcb77867361f6a"}, {file = "backports.entry_points_selectable-1.1.0.tar.gz", hash = "sha256:988468260ec1c196dab6ae1149260e2f5472c9110334e5d51adcb77867361f6a"},
] ]
click = [
{file = "click-8.0.3-py3-none-any.whl", hash = "sha256:353f466495adaeb40b6b5f592f9f91cb22372351c84caeb068132442a4518ef3"},
{file = "click-8.0.3.tar.gz", hash = "sha256:410e932b050f5eed773c4cda94de75971c89cdb3155a72a0831139a79e5ecb5b"},
]
colorama = [ colorama = [
{file = "colorama-0.4.4-py2.py3-none-any.whl", hash = "sha256:9f47eda37229f68eee03b24b9748937c7dc3868f906e8ba69fbcbdd3bc5dc3e2"}, {file = "colorama-0.4.4-py2.py3-none-any.whl", hash = "sha256:9f47eda37229f68eee03b24b9748937c7dc3868f906e8ba69fbcbdd3bc5dc3e2"},
{file = "colorama-0.4.4.tar.gz", hash = "sha256:5941b2b48a20143d2267e95b1c2a7603ce057ee39fd88e7329b0c292aa16869b"}, {file = "colorama-0.4.4.tar.gz", hash = "sha256:5941b2b48a20143d2267e95b1c2a7603ce057ee39fd88e7329b0c292aa16869b"},

View file

@ -8,6 +8,7 @@ authors = ["ldericher <40151420+ldericher@users.noreply.github.com>"]
python = "^3.6.1" python = "^3.6.1"
PyYAML = "^5.4.1" PyYAML = "^5.4.1"
pydantic = "^1.8.2" pydantic = "^1.8.2"
click = "^8.0.3"
[tool.poetry.dev-dependencies] [tool.poetry.dev-dependencies]
virtualenv = "^20.8.1" virtualenv = "^20.8.1"