def ask(prompt='', default=None): if prompt: if default: prompt = prompt + ' [' + default + ']' prompt = prompt + ' ' answer = None while not answer: answer = input(prompt) if default and not answer: return default return answer
def __call__(self): releaser = self.releaser path = releaser.config['version_file'] keyword = releaser.config['version_keyword'] releaser.old_version = version_in_python_source_file( path, keyword=keyword) print('Current version: {0}'.format(releaser.old_version)) releaser.the_version = input('What is the new version number? ') # Write the new version onto the source code version_in_python_source_file( path, keyword=keyword, replace=releaser.the_version) self._succeed()
def ask(prompt='', default=None): """Print the ``prompt``, get user's answer, return it -- or a default.""" if prompt: if default: prompt = prompt + ' [' + default + ']' prompt = prompt + ' ' answer = None while not answer: answer = input(prompt) if default and not answer: return default return answer
def pick_one_of(options, prompt='Pick one: '): '''Lets the user pick an item by number.''' alist = options if isinstance(options, list) else list(options) c = 0 for o in alist: c += 1 print(str(c).rjust(2) + ". " + str(o)) while True: try: opt = int(input(prompt)) except ValueError: continue return alist[opt - 1]
def bool_input(prompt, default=None): '''Returns True or False based on the user's choice.''' if default is None: choices = ' (y/n) ' elif default: choices = ' (Y/n) ' else: choices = ' (y/N) ' opt = input(prompt + choices).lower() if opt == 'y': return True elif opt == "n": return False elif not opt and default is not None: return default else: # Invalid answer, let's ask again return bool_input(prompt)
def pick_one_of(options, prompt='Pick one: ', to_str=None): """Let the user choose an item (from a sequence of options) by number. ``to_str()`` is a callback that must take an item as argument and must return a corresponding string to be displayed. """ alist = options if isinstance(options, list) else list(options) c = 0 for o in alist: c += 1 print(str(c).rjust(2) + ". " + to_str(o) if to_str else str(o)) while True: try: opt = int(input(prompt)) except ValueError: continue return alist[opt - 1]