python - APT command line interface-like yes/no input?

ID : 20329

viewed : 15

Tags : pythonpython

Top 5 Answer for python - APT command line interface-like yes/no input?

vote vote

100

As you mentioned, the easiest way is to use raw_input() (or simply input() for Python 3). There is no built-in way to do this. From Recipe 577058:

import sys   def query_yes_no(question, default="yes"):     """Ask a yes/no question via raw_input() and return their answer.      "question" is a string that is presented to the user.     "default" is the presumed answer if the user just hits <Enter>.             It must be "yes" (the default), "no" or None (meaning             an answer is required of the user).      The "answer" return value is True for "yes" or False for "no".     """     valid = {"yes": True, "y": True, "ye": True, "no": False, "n": False}     if default is None:         prompt = " [y/n] "     elif default == "yes":         prompt = " [Y/n] "     elif default == "no":         prompt = " [y/N] "     else:         raise ValueError("invalid default answer: '%s'" % default)      while True:         sys.stdout.write(question + prompt)         choice = input().lower()         if default is not None and choice == "":             return valid[default]         elif choice in valid:             return valid[choice]         else:             sys.stdout.write("Please respond with 'yes' or 'no' " "(or 'y' or 'n').\n") 

(For Python 2, use raw_input instead of input.) Usage example:

>>> query_yes_no("Is cabbage yummier than cauliflower?") Is cabbage yummier than cauliflower? [Y/n] oops Please respond with 'yes' or 'no' (or 'y' or 'n'). Is cabbage yummier than cauliflower? [Y/n] [ENTER] >>> True  >>> query_yes_no("Is cabbage yummier than cauliflower?", None) Is cabbage yummier than cauliflower? [y/n] [ENTER] Please respond with 'yes' or 'no' (or 'y' or 'n'). Is cabbage yummier than cauliflower? [y/n] y >>> True 
vote vote

84

I'd do it this way:

# raw_input returns the empty string for "enter" yes = {'yes','y', 'ye', ''} no = {'no','n'}  choice = raw_input().lower() if choice in yes:    return True elif choice in no:    return False else:    sys.stdout.write("Please respond with 'yes' or 'no'") 
vote vote

80

You can use click's confirm method.

import click  if click.confirm('Do you want to continue?', default=True):     print('Do something') 

This will print:

$ Do you want to continue? [Y/n]: 

Should work for Python 2/3 on Linux, Mac or Windows.

Docs: http://click.pocoo.org/5/prompts/#confirmation-prompts

vote vote

69

There is a function strtobool in Python's standard library: http://docs.python.org/2/distutils/apiref.html?highlight=distutils.util#distutils.util.strtobool

You can use it to check user's input and transform it to True or False value.

vote vote

52

A very simple (but not very sophisticated) way of doing this for a single choice would be:

msg = 'Shall I?' shall = input("%s (y/N) " % msg).lower() == 'y' 

You could also write a simple (slightly improved) function around this:

def yn_choice(message, default='y'):     choices = 'Y/n' if default.lower() in ('y', 'yes') else 'y/N'     choice = input("%s (%s) " % (message, choices))     values = ('y', 'yes', '') if choices == 'Y/n' else ('y', 'yes')     return choice.strip().lower() in values 

Note: On Python 2, use raw_input instead of input.

Top 3 video Explaining python - APT command line interface-like yes/no input?

Related QUESTION?