Move console script entry to cli module

This commit is contained in:
Tim Van Baak 2020-12-28 15:12:29 -08:00
parent 6c25692d3d
commit 997df5f54f
3 changed files with 63 additions and 69 deletions

View File

@ -1,67 +0,0 @@
# Standard library imports
import argparse
# Application imports
from inquisitor import cli
from inquisitor import configs
from signal import signal, SIGPIPE, SIG_DFL
signal(SIGPIPE, SIG_DFL)
def parse_args(valid_commands):
command_descs = "\n".join([
"- {0}: {1}".format(name, func.__doc__)
for name, func in valid_commands.items()])
parser = argparse.ArgumentParser(
description="Available commands:\n{}\n".format(command_descs),
formatter_class=argparse.RawDescriptionHelpFormatter,
add_help=False)
parser.add_argument("command",
nargs="?",
default="help",
help="The command to execute",
choices=valid_commands,
metavar="command")
parser.add_argument("args",
nargs=argparse.REMAINDER,
help="Command arguments",
metavar="args")
parser.add_argument("-v",
action="store_true",
dest="verbose",
help="Enable debug logging")
global print_usage
print_usage = parser.print_help
return parser.parse_args()
def command_help(args):
"""Print this help message and exit."""
print_usage()
return 0
def main():
# Enumerate valid commands.
commands = {
name[8:] : func
for name, func in vars(cli).items()
if name.startswith("command_")}
commands['help'] = command_help
args = parse_args(commands)
# Configure logging.
if args.verbose:
configs.log_verbose()
# Execute command.
if args.command:
return commands[args.command](args.args)
if __name__ == "__main__":
import sys
sys.exit(main())

View File

@ -4,9 +4,10 @@ import json
import logging
import os
import random
import sys
# Application imports
from inquisitor.configs import logger, DUNGEON_PATH, SOURCES_PATH
from inquisitor.configs import logger, DUNGEON_PATH, SOURCES_PATH, add_logging_handler
def command_test(args):
@ -216,3 +217,63 @@ def command_run(args):
except Exception as e:
logger.error(e)
return -1
def command_help(args):
"""Print this help message and exit."""
print_usage()
return 0
def main():
"""CLI entry point"""
# Enable piping
from signal import signal, SIGPIPE, SIG_DFL
signal(SIGPIPE, SIG_DFL)
# Collect the commands from this module
import inquisitor.cli
commands = {
name[8:] : func
for name, func in vars(inquisitor.cli).items()
if name.startswith('command_')
}
descriptions = "\n".join([
"- {0}: {1}".format(name, func.__doc__)
for name, func in commands.items()])
# Set up the parser
parser = argparse.ArgumentParser(
description="Available commands:\n{}\n".format(descriptions),
formatter_class=argparse.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")
parser.add_argument("-v",
action="store_true",
dest="verbose",
help="Enable debug logging")
# Extract the usage print for command_help
global print_usage
print_usage = parser.print_help
args = parser.parse_args()
# Initialize a console logger
add_logging_handler(verbose=args.verbose, log_filename=None)
# Execute command
if args.command:
sys.exit(commands[args.command](args.args))
else:
print("command required")
sys.exit(0)

View File

@ -13,7 +13,7 @@ setuptools.setup(
install_requires=requires,
entry_points={
"console_scripts": [
"inquisitor = inquisitor.__main__:main"
"inquisitor = inquisitor.cli:main",
],
},
)