begin to implement subcommand handling

Signed-off-by: Nico Schottelius <nico@kr.ethz.ch>
This commit is contained in:
Nico Schottelius 2011-09-13 17:40:28 +02:00
parent f4faff6b33
commit 5cbe34ee33
2 changed files with 61 additions and 40 deletions

View file

@ -60,10 +60,6 @@ VERSION = "2.0.0"
logging.basicConfig(level=logging.INFO, format='%(levelname)s: %(message)s') logging.basicConfig(level=logging.INFO, format='%(levelname)s: %(message)s')
log = logging.getLogger() log = logging.getLogger()
def banner():
"""Guess what :-)"""
print(BANNER)
class TypeEmulator: class TypeEmulator:
def __init__(self, name): def __init__(self, name):
@ -602,42 +598,13 @@ class Cdist:
self.deploy_to() self.deploy_to()
self.cleanup() self.cleanup()
def banner(*args):
"""Guess what :-)"""
print(BANNER)
sys.exit(0)
def foo(): def config(*args, **kargs):
print("test") """Configure remote system"""
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)
process = {} process = {}
try: try:
if len(args.host) == 0: if len(args.host) == 0:
@ -667,3 +634,56 @@ if __name__ == "__main__":
except KeyboardInterrupt: except KeyboardInterrupt:
sys.exit(0) 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)

View file

@ -1,7 +1,8 @@
For 2.0.0: For 2.0.0:
- support subcommands! - support subcommands!
deploy? config? config (deploy)
install (provision)
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------