web/negromate/web/commands/__init__.py

44 lines
1.3 KiB
Python

import argparse
import sys
from . import run, build, thumbnail, rsync
def main():
"""
Build parser for all the commands and launch appropiate command.
Each command must be a module with at least the following members:
* name: String with the command name. Will be used for
argparse subcommand.
* help_text: String with the help text.
* options: Function to build the parser of the command. Takes
one parametter, the argparser parser instance for this
subcommand.
* run: Function that runs the actual command. Takes one
parametter, the Namespace with the arguments.
"""
commands = [
build,
run,
thumbnail,
rsync,
]
parser = argparse.ArgumentParser()
parser.set_defaults(command=None)
subparsers = parser.add_subparsers()
for command in commands:
command_parser = subparsers.add_parser(command.name, help=command.help_text)
command_parser.set_defaults(command=command.name)
command.options(command_parser)
args = parser.parse_args()
if args.command is None:
parser.print_usage()
else:
for command in commands:
if args.command == command.name:
command.run(args)