Examples

You can find all these examples at examples directory.

text.py

import os
import sys
import re
from pprint import pprint

import inquirer
from inquirer import errors

sys.path.append(os.path.realpath('.'))


def phone_validation(answers, current):
    if not re.match('\+?\d[\d ]+\d', current):
        raise errors.ValidationError('', reason='I don\'t like your phone number!')

    return True


questions = [
    inquirer.Text('name',
                  message="What's your name?"),
    inquirer.Text('surname',
                  message="What's your surname, {name}?"),
    inquirer.Text('phone',
                  message="What's your phone number",
                  validate=phone_validation,
                  )
]

answers = inquirer.prompt(questions)

pprint(answers)

Result on something like:

Example of Text Question

confirm.py

import os
import sys
import re
sys.path.append(os.path.realpath('.'))
from pprint import pprint

import inquirer

questions = [
    inquirer.Confirm('continue',
                  message="Should I continue"),
    inquirer.Confirm('stop',
                  message="Should I stop", default=True),
]

answers = inquirer.prompt(questions)

pprint(answers)

Result on something like:

Example of Confirm Question

list.py

import os
import sys
import re
sys.path.append(os.path.realpath('.'))
from pprint import pprint

import inquirer

questions = [
    inquirer.List('size',
                  message="What size do you need?",
                  choices=['Jumbo', 'Large', 'Standard', 'Medium', 'Small', 'Micro'],
              ),
]

answers = inquirer.prompt(questions)

pprint(answers)

Result on something like:

Example of List Question

checkbox.py

import os
import sys
import re
sys.path.append(os.path.realpath('.'))
from pprint import pprint

import inquirer

questions = [
    inquirer.Checkbox('interests',
                      message="What are you interested in?",
                      choices=['Computers', 'Books', 'Science', 'Nature', 'Fantasy', 'History'],
                      default=['Computers', 'Books']),
]

answers = inquirer.prompt(questions)

pprint(answers)

Result on something like:

Example of Checkbox Question

The choices list can also be a list of tuples. The first value in each tuple should be the label displayed to the user. The second value in each tuple should be the actual value for that option. This allows you to have the user choose options that are not plain strings in the code.

import os
import sys
import re
sys.path.append(os.path.realpath('.'))
from pprint import pprint

import inquirer

questions = [
    inquirer.Checkbox('interests',
                      message="What are you interested in?",
                      choices=[
                          ('Computers', 'c'),
                          ('Books', 'b'),
                          ('Science', 's'),
                          ('Nature', 'n'),
                          ('Fantasy', 'f'),
                          ('History', 'h'),
                      ],
                      default=['c', 'b']),
]

answers = inquirer.prompt(questions)

pprint(answers)

theme.py

import inquirer
from inquirer.themes import GreenPassion

q = [
    inquirer.Text('name',
                  message='Whats your name?',
                  default='No one'),
    inquirer.List('jon',
                  message='Does Jon Snow know?',
                  choices=['yes', 'no'],
                  default='no'),
    inquirer.Checkbox('kill_list',
                      message='Who you want to kill?',
                      choices=['Cersei', 'Littlefinger', 'The Mountain']
                      )
]

inquirer.prompt(q, theme=GreenPassion())

Result on something like:

Example of theme (GreenPassion)