From 2a39fcaf8cf6d20c2cfac46ddba661ddc9699f39 Mon Sep 17 00:00:00 2001 From: Kali Kaneko Date: Wed, 6 Jul 2016 14:24:30 +0200 Subject: [refactor] beautify cli output --- src/leap/bitmask/cli/command.py | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) (limited to 'src/leap/bitmask/cli/command.py') diff --git a/src/leap/bitmask/cli/command.py b/src/leap/bitmask/cli/command.py index ac427c76..36552c03 100644 --- a/src/leap/bitmask/cli/command.py +++ b/src/leap/bitmask/cli/command.py @@ -31,15 +31,28 @@ from txzmq import ZmqRequestTimeoutError from leap.bitmask.core import ENDPOINT +appname = 'bitmaskctl' + + def _print_result(result): print Fore.GREEN + '%s' % result + Fore.RESET +def default_dict_printer(result): + for key, value in result.items(): + if value is None: + value = str(value) + print(Fore.RESET + key.ljust(10) + Fore.GREEN + value + Fore.RESET) + + class Command(object): + """A generic command dispatcher. + Any command in the class attribute `commands` will be dispached and + represented with a generic printer.""" service = '' - usage = '''%s %s ''' % tuple(sys.argv[:2]) - epilog = ("Use '%s %s --help' to learn more " - "about each command." % tuple(sys.argv[:2])) + usage = '''{name} '''.format(name=appname) + epilog = ("Use bitmaskctl --help' to learn more " + "about each command.") commands = [] def __init__(self): @@ -61,9 +74,11 @@ class Command(object): except SystemExit: return defer.succeed(None) + # if command is in the default list, send the bare command + # and use the default printer if args.command in self.commands: self.data += [args.command] - return self._send() + return self._send(printer=default_dict_printer) elif (args.command == 'execute' or args.command.startswith('_') or -- cgit v1.2.3