From 217a5fa75b67f8ea381dae1357a4898bdddbb6ad Mon Sep 17 00:00:00 2001
From: ldericher <40151420+ldericher@users.noreply.github.com>
Date: Wed, 20 Oct 2021 08:31:36 +0200
Subject: [PATCH] simple "complex type" $click_ interface
---
.idea/runConfigurations/kiwi_next.xml | 4 ++--
kiwi_scp/commands/__init__.py | 0
kiwi_scp/commands/cli.py | 20 ++++++++++++++++++++
kiwi_scp/commands/cmd_init.py | 21 +++++++++++++++++++++
kiwi_scp/scripts/kiwi_next.py | 19 +++++++++++++------
poetry.lock | 24 ++++++++++++++++++++----
pyproject.toml | 1 +
7 files changed, 77 insertions(+), 12 deletions(-)
create mode 100644 kiwi_scp/commands/__init__.py
create mode 100644 kiwi_scp/commands/cli.py
create mode 100644 kiwi_scp/commands/cmd_init.py
diff --git a/.idea/runConfigurations/kiwi_next.xml b/.idea/runConfigurations/kiwi_next.xml
index b4babb9..ad46889 100644
--- a/.idea/runConfigurations/kiwi_next.xml
+++ b/.idea/runConfigurations/kiwi_next.xml
@@ -7,12 +7,12 @@
-
+
-
+
diff --git a/kiwi_scp/commands/__init__.py b/kiwi_scp/commands/__init__.py
new file mode 100644
index 0000000..e69de29
diff --git a/kiwi_scp/commands/cli.py b/kiwi_scp/commands/cli.py
new file mode 100644
index 0000000..03e8e62
--- /dev/null
+++ b/kiwi_scp/commands/cli.py
@@ -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
diff --git a/kiwi_scp/commands/cmd_init.py b/kiwi_scp/commands/cmd_init.py
new file mode 100644
index 0000000..9f61706
--- /dev/null
+++ b/kiwi_scp/commands/cmd_init.py
@@ -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
diff --git a/kiwi_scp/scripts/kiwi_next.py b/kiwi_scp/scripts/kiwi_next.py
index 0885d92..de7c32c 100644
--- a/kiwi_scp/scripts/kiwi_next.py
+++ b/kiwi_scp/scripts/kiwi_next.py
@@ -1,14 +1,21 @@
-from kiwi_scp.config import Config
+import click
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)
- kiwi = Config(**yml)
+ ctx.ensure_object(dict)
+ ctx.obj["cfg"] = Config(**yml)
- print(repr(kiwi))
- print(kiwi.kiwi_yml)
+ click.echo("Hello main")
if __name__ == "__main__":
diff --git a/poetry.lock b/poetry.lock
index 1e00a2e..978f60e 100644
--- a/poetry.lock
+++ b/poetry.lock
@@ -35,11 +35,23 @@ importlib-metadata = {version = "*", markers = "python_version < \"3.8\""}
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)"]
+[[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]]
name = "colorama"
version = "0.4.4"
description = "Cross-platform colored terminal text."
-category = "dev"
+category = "main"
optional = false
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"
version = "4.8.1"
description = "Read metadata from Python packages"
-category = "dev"
+category = "main"
optional = false
python-versions = ">=3.6"
@@ -260,7 +272,7 @@ testing = ["coverage (>=4)", "coverage-enable-subprocess (>=1)", "flaky (>=3)",
name = "zipp"
version = "3.6.0"
description = "Backport of pathlib-compatible object wrapper for zip files"
-category = "dev"
+category = "main"
optional = false
python-versions = ">=3.6"
@@ -271,7 +283,7 @@ testing = ["pytest (>=4.6)", "pytest-checkdocs (>=2.4)", "pytest-flake8", "pytes
[metadata]
lock-version = "1.1"
python-versions = "^3.6.1"
-content-hash = "7343a635c1769b0dda5c30d310a00bf6c4dac0d64f26ae2076b9b3e6fde69700"
+content-hash = "eb1a3ab9af78ac7898062245858dbf9e9a27e82a6484f45f14b8db6c7fe812d6"
[metadata.files]
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.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 = [
{file = "colorama-0.4.4-py2.py3-none-any.whl", hash = "sha256:9f47eda37229f68eee03b24b9748937c7dc3868f906e8ba69fbcbdd3bc5dc3e2"},
{file = "colorama-0.4.4.tar.gz", hash = "sha256:5941b2b48a20143d2267e95b1c2a7603ce057ee39fd88e7329b0c292aa16869b"},
diff --git a/pyproject.toml b/pyproject.toml
index 85844ed..1b61ceb 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -8,6 +8,7 @@ authors = ["ldericher <40151420+ldericher@users.noreply.github.com>"]
python = "^3.6.1"
PyYAML = "^5.4.1"
pydantic = "^1.8.2"
+click = "^8.0.3"
[tool.poetry.dev-dependencies]
virtualenv = "^20.8.1"