import sys from functools import wraps import docopt from dcos import emitting emitter = emitting.FlatEmitter() def decorate_docopt_usage(func): """Handle DocoptExit exception :param func: function :type func: function :return: wrapped function :rtype: function """ @wraps(func) def wrapper(*args, **kwargs): try: result = func(*args, **kwargs) except docopt.DocoptExit as e: emitter.publish("Command not recognized\n") emitter.publish(e) return 1 return result return wrapper def confirm(prompt, yes): """ :param prompt: message to display to the terminal :type prompt: str :param yes: whether to assume that the user responded with yes :type yes: bool :returns: True if the user responded with yes; False otherwise :rtype: bool """ if yes: return True else: while True: sys.stdout.write('{} [yes/no] '.format(prompt)) sys.stdout.flush() response = sys.stdin.readline().strip().lower() if response == 'yes' or response == 'y': return True elif response == 'no' or response == 'n': return False else: msg = "'{}' is not a valid response.".format(response) emitter.publish(msg)