From 997df5f54f5cc8c8a6f324a20de01b78579d0507 Mon Sep 17 00:00:00 2001 From: Tim Van Baak Date: Mon, 28 Dec 2020 15:12:29 -0800 Subject: [PATCH] Move console script entry to cli module --- inquisitor/__main__.py | 67 ------------------------------------------ inquisitor/cli.py | 63 ++++++++++++++++++++++++++++++++++++++- setup.py | 2 +- 3 files changed, 63 insertions(+), 69 deletions(-) delete mode 100644 inquisitor/__main__.py diff --git a/inquisitor/__main__.py b/inquisitor/__main__.py deleted file mode 100644 index e536f17..0000000 --- a/inquisitor/__main__.py +++ /dev/null @@ -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()) diff --git a/inquisitor/cli.py b/inquisitor/cli.py index f56d00e..9271b6c 100644 --- a/inquisitor/cli.py +++ b/inquisitor/cli.py @@ -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) diff --git a/setup.py b/setup.py index cc9e519..c552f51 100644 --- a/setup.py +++ b/setup.py @@ -13,7 +13,7 @@ setuptools.setup( install_requires=requires, entry_points={ "console_scripts": [ - "inquisitor = inquisitor.__main__:main" + "inquisitor = inquisitor.cli:main", ], }, )