From 5cbe34ee336ee51c5666b774654afcd706f61579 Mon Sep 17 00:00:00 2001 From: Nico Schottelius Date: Tue, 13 Sep 2011 17:40:28 +0200 Subject: [PATCH] begin to implement subcommand handling Signed-off-by: Nico Schottelius --- bin/cdist | 98 ++++++++++++++++++++++++++----------------- doc/dev/todo/niconext | 3 +- 2 files changed, 61 insertions(+), 40 deletions(-) diff --git a/bin/cdist b/bin/cdist index 873fbb1a..99bc775f 100755 --- a/bin/cdist +++ b/bin/cdist @@ -60,10 +60,6 @@ VERSION = "2.0.0" logging.basicConfig(level=logging.INFO, format='%(levelname)s: %(message)s') log = logging.getLogger() -def banner(): - """Guess what :-)""" - print(BANNER) - class TypeEmulator: def __init__(self, name): @@ -602,42 +598,13 @@ class Cdist: self.deploy_to() self.cleanup() +def banner(*args): + """Guess what :-)""" + print(BANNER) + sys.exit(0) -def foo(): - print("test") - -if __name__ == "__main__": - parser = argparse.ArgumentParser(description='cdist ' + VERSION) - parser.add_argument('host', nargs='*', help='one or more hosts to operate on') - parser.add_argument('-b', '--banner', - help='Show cdist banner', - action='store_true', dest='banner') - parser.add_argument('-c', '--cdist-home', - help='Change cdist home (default: .. from bin directory)', - action='store') - parser.add_argument('-d', '--debug', help='Set log level to debug', - action='store_true') - parser.add_argument('-i', '--initial-manifest', - help='Path to a cdist manifest or - to read from stdin', - dest='manifest', required=False) - parser.add_argument('-p', '--parallel', - help='Operate on multiple hosts in parallel', - action='store_true', dest='parallel') - parser.add_argument('-s', '--sequential', - help='Operate on multiple hosts sequentially (default)', - action='store_false', dest='parallel') - parser.add_argument('-V', '--version', help='Show version', - action='version', version='%(prog)s ' + VERSION) - - args = parser.parse_args(sys.argv[1:]) - log.debug(args) - if args.debug: - logging.root.setLevel(logging.DEBUG) - - if args.banner: - banner() - sys.exit(0) - +def config(*args, **kargs): + """Configure remote system""" process = {} try: if len(args.host) == 0: @@ -667,3 +634,56 @@ if __name__ == "__main__": except KeyboardInterrupt: sys.exit(0) + +if __name__ == "__main__": + # Construct parser others can reuse + parser = {} + # Options _all_ parsers have in common + parser['most'] = argparse.ArgumentParser(add_help=False) + parser['most'].add_argument('-d', '--debug', + help='Set log level to debug', + action='store_true') + parser['most'].add_argument('-V', '--version', + help='Show version', + action='version', + version='%(prog)s ' + VERSION) + + # Main subcommand parser + parser['main'] = argparse.ArgumentParser(description='cdist ' + VERSION) + parser['sub'] = parser['main'].add_subparsers() + + + parser['banner'] = parser['sub'].add_parser('banner') +# parser['banner'].add_argument('all', nargs='*', +# help='Show cdist banner') + parser['banner'].set_defaults(func=banner) + + parser['config'] = parser['sub'].add_parser('config', + aliases=['deploy'], + parents=[parser['most']]) + parser['config'].add_argument('host', nargs='*', + help='one or more hosts to operate on') + parser['config'].add_argument('-c', '--cdist-home', + help='Change cdist home (default: .. from bin directory)', + action='store') + parser['config'].add_argument('-i', '--initial-manifest', + help='Path to a cdist manifest or - to read from stdin', + dest='manifest', required=False) + parser['config'].add_argument('-p', '--parallel', + help='Operate on multiple hosts in parallel', + action='store_true', dest='parallel') + parser['config'].add_argument('-s', '--sequential', + help='Operate on multiple hosts sequentially (default)', + action='store_false', dest='parallel') + parser['config'].set_defaults(func=config) + + + args = parser['main'].parse_args(sys.argv[1:]) + + # Most subcommands to have --debug, so handle it here + if 'debug' in args: + if args.debug: + logging.root.setLevel(logging.DEBUG) + log.debug(args) + + args.func(args) diff --git a/doc/dev/todo/niconext b/doc/dev/todo/niconext index b1b77a27..6b87b8fe 100644 --- a/doc/dev/todo/niconext +++ b/doc/dev/todo/niconext @@ -1,7 +1,8 @@ For 2.0.0: - support subcommands! - deploy? config? + config (deploy) + install (provision) --------------------------------------------------------------------------------