clan-cli: manage sub-commands via python argparse
This commit is contained in:
parent
95df33e2bc
commit
1e5cce99a9
@ -1,24 +1,15 @@
|
||||
# !/usr/bin/env python3
|
||||
import subprocess
|
||||
import sys
|
||||
import argparse
|
||||
|
||||
import clan_admin
|
||||
|
||||
|
||||
def showhelp() -> None:
|
||||
print('''
|
||||
usage:
|
||||
clan admin ...
|
||||
clan join ...
|
||||
clan delete ...
|
||||
''')
|
||||
# this will be the entrypoint under /bin/clan (see pyproject.toml config)
|
||||
def clan() -> None:
|
||||
parser = argparse.ArgumentParser(description="cLAN tool")
|
||||
subparsers = parser.add_subparsers()
|
||||
|
||||
# init clan admin
|
||||
parser_admin = subparsers.add_parser("admin")
|
||||
clan_admin.make_parser(parser_admin)
|
||||
|
||||
try:
|
||||
cmd = f'clan-{sys.argv[1]}'
|
||||
except: # noqa
|
||||
showhelp()
|
||||
|
||||
try:
|
||||
subprocess.Popen([cmd] + sys.argv[2:])
|
||||
except FileNotFoundError:
|
||||
print(f'command {cmd} not found')
|
||||
exit(2)
|
||||
|
@ -69,8 +69,8 @@ def git(args: argparse.Namespace) -> None:
|
||||
] + args.git_args
|
||||
)
|
||||
|
||||
|
||||
parser = argparse.ArgumentParser(description="clan-admin")
|
||||
# takes a (sub)parser and configures it
|
||||
def make_parser(parser: argparse.ArgumentParser) -> None:
|
||||
parser.add_argument(
|
||||
"-f",
|
||||
"--folder",
|
||||
@ -118,5 +118,9 @@ parser_git = subparser.add_parser("git", help="control the clan repo via git")
|
||||
parser_git.add_argument("git_args", nargs="*")
|
||||
parser_git.set_defaults(func=git)
|
||||
|
||||
|
||||
# entry point if this file is executed directly
|
||||
if __name__ == "__main__":
|
||||
parser = argparse.ArgumentParser(description="clan-admin")
|
||||
args = parser.parse_args()
|
||||
args.func(args)
|
||||
|
@ -9,7 +9,7 @@ include = ["clan.py"]
|
||||
name = "clan"
|
||||
description = "cLAN CLI tool"
|
||||
dynamic = ["version"]
|
||||
scripts = {clan = "clan:my_cli"}
|
||||
scripts = {clan = "clan:clan"}
|
||||
|
||||
[tool.pytest.ini_options]
|
||||
addopts = "--cov . --cov-report term --cov-fail-under=100 --no-cov-on-fail"
|
||||
|
Loading…
Reference in New Issue
Block a user