def show(*args): if not args: _exit(colored('What to show?', ERROR_COLOR)) if len(args) > 1: _exit(colored('Too many arguments', ERROR_COLOR)) name = args[0] if name == 'all': if len(Store.data): print colored("Showing all items", SUCCESS_COLOR) for name, value in Store.data.iteritems(): print (colored(name, KEY_COLOR) + colored(' : ', SEPARATOR_COLOR) + colored(value, VALUE_COLOR)) return else: print colored("Nothing to show", INFO_COLOR) return if name in Store.data: print colored(Store.data[name], VALUE_COLOR) else: print (colored("`", ERROR_COLOR) + colored(args[0], KEY_COLOR) + colored('` not present', ERROR_COLOR)) from utils import did_you_mean guessed_key = did_you_mean(name, Store.data.keys()) print (colored("Did you mean", INFO_COLOR) + colored(" : ", SEPARATOR_COLOR) + colored(guessed_key, KEY_COLOR)) print (colored(guessed_key, KEY_COLOR) + colored(" : ", SEPARATOR_COLOR) + colored(Store.data[guessed_key], VALUE_COLOR))
def update(*args): if not args: print colored('update what?', ERROR_COLOR) return if len(args) < 2: print colored("require at least 2 arguments", ERROR_COLOR) return name = args[0] value = ' '.join(args[1:]) if name not in Store.data: print (colored(name, KEY_COLOR) + colored(" not in Store", ERROR_COLOR)) from utils import did_you_mean guessed_name = did_you_mean(name, Store.data.keys()) _exit(colored("Did you mean : ", INFO_COLOR) + colored(guessed_name, KEY_COLOR)) return else: Store.data[name] = value
def run(): parser = argparse.ArgumentParser(description='Command line key-value store.', add_help=False, ) parser.add_argument('action', metavar='action', nargs='*', help='action') args = parser.parse_args() # command = args or 'help' if not args.action: command = 'help' else: command = args.action[0] application = App() if not hasattr(application, command): print (colored('Command `', ERROR_COLOR) + colored(command, INFO_COLOR) + colored('`not found, Did you mean `', ERROR_COLOR) + colored(did_you_mean(command, App.SUPPORTED_COMMANDS), KEY_COLOR) + colored('`', ERROR_COLOR)) else: sub_commands = args.action[1:] if args.action else None if sub_commands: application.call(command, *sub_commands) else: application.call(command)