1
0
Fork 0
mirror of https://github.com/yavook/kiwi-scp.git synced 2024-11-22 12:53:00 +00:00
kiwi-scp/kiwi_scp/commands/cli.py

42 lines
1.2 KiB
Python
Raw Normal View History

import importlib
import os
2021-12-02 16:19:14 +00:00
from typing import List, Optional
import click
class KiwiCLI(click.MultiCommand):
"""Command Line Interface spread over multiple files in this directory"""
def list_commands(self, ctx: click.Context) -> List[str]:
"""list all the commands defined by cmd_*.py files in this directory"""
return [
filename[4:-3]
for filename in os.listdir(os.path.abspath(os.path.dirname(__file__)))
if filename.startswith("cmd_") and filename.endswith(".py")
]
def get_command(self, ctx: click.Context, cmd_name: str) -> Optional[click.Command]:
"""import and return a specific command"""
try:
cmd_module = importlib.import_module(f"kiwi_scp.commands.cmd_{cmd_name}")
except ImportError:
return
2022-01-19 14:35:33 +00:00
member_name = f"{cmd_name.capitalize()}Command"
if member_name in dir(cmd_module):
member = getattr(cmd_module, member_name)
if isinstance(member, click.Command):
return member
2022-01-19 14:35:33 +00:00
else:
raise Exception("Fail class")
2022-01-19 14:35:33 +00:00
else:
raise Exception("Fail member name")