Add a basic reflection-powered cli framework
This commit is contained in:
parent
47ca307f8e
commit
59fdc5b355
|
@ -0,0 +1,56 @@
|
||||||
|
from argparse import ArgumentParser, RawDescriptionHelpFormatter, REMAINDER
|
||||||
|
import argparse
|
||||||
|
from signal import signal, SIGPIPE, SIG_DFL
|
||||||
|
import sys
|
||||||
|
|
||||||
|
|
||||||
|
def command_help(args):
|
||||||
|
"""Print this help message and exit."""
|
||||||
|
print_usage()
|
||||||
|
return 0
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
"""CLI entry point"""
|
||||||
|
# Enable piping
|
||||||
|
signal(SIGPIPE, SIG_DFL)
|
||||||
|
|
||||||
|
# Get the available commands by reflection
|
||||||
|
cli = sys.modules[__name__]
|
||||||
|
commands = {
|
||||||
|
name[8:]: func
|
||||||
|
for name, func in vars(cli).items()
|
||||||
|
if name.startswith("command_")
|
||||||
|
}
|
||||||
|
descriptions = "\n".join([
|
||||||
|
f" {name} - {func.__doc__}"
|
||||||
|
for name, func in commands.items()])
|
||||||
|
|
||||||
|
# Set up the top-level parser
|
||||||
|
parser = ArgumentParser(
|
||||||
|
description=f"Available commands:\n{descriptions}\n",
|
||||||
|
formatter_class=RawDescriptionHelpFormatter,
|
||||||
|
add_help=False)
|
||||||
|
parser.add_argument("command",
|
||||||
|
nargs="?",
|
||||||
|
default="help",
|
||||||
|
help="The command to execute",
|
||||||
|
choices=commands,
|
||||||
|
metavar="command")
|
||||||
|
parser.add_argument("args",
|
||||||
|
nargs=argparse.REMAINDER,
|
||||||
|
help="Command arguments",
|
||||||
|
metavar="args")
|
||||||
|
|
||||||
|
# Pass the parser's help printer to command_help via global
|
||||||
|
global print_usage
|
||||||
|
print_usage = parser.print_help
|
||||||
|
|
||||||
|
args = parser.parse_args()
|
||||||
|
|
||||||
|
# Execute command
|
||||||
|
if args.command:
|
||||||
|
sys.exit(commands[args.command](args.args))
|
||||||
|
else:
|
||||||
|
parser.print_usage()
|
||||||
|
sys.exit(0)
|
|
@ -12,6 +12,9 @@ feedparser = "^6.0.10"
|
||||||
|
|
||||||
[tool.poetry.dev-dependencies]
|
[tool.poetry.dev-dependencies]
|
||||||
|
|
||||||
|
[tool.poetry.scripts]
|
||||||
|
intake = "intake.cli:main"
|
||||||
|
|
||||||
[build-system]
|
[build-system]
|
||||||
requires = ["poetry-core>=1.0.0"]
|
requires = ["poetry-core>=1.0.0"]
|
||||||
build-backend = "poetry.core.masonry.api"
|
build-backend = "poetry.core.masonry.api"
|
||||||
|
|
Loading…
Reference in New Issue