Examples

You can find all these examples at examples directory.

text.py

import os
import re
import sys
from pprint import pprint


sys.path.append(os.path.realpath("."))
import inquirer3  # noqa


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

    return True


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

answers = inquirer3.prompt(questions)

pprint(answers)

Result on something like:

{{ inquirer text }}

confirm.py

import os
import sys
from pprint import pprint


sys.path.append(os.path.realpath("."))
import inquirer3  # noqa


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

answers = inquirer3.prompt(questions)

pprint(answers)

Result on something like:

{{ inquirer confirm }}

list.py

import os
import sys
from pprint import pprint


sys.path.append(os.path.realpath("."))
import inquirer3  # noqa


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

answers = inquirer3.prompt(questions)

pprint(answers)

Result on something like:

{{ inquirer list }}

checkbox.py

import os
import sys
from pprint import pprint


sys.path.append(os.path.realpath("."))
import inquirer3  # noqa


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

answers = inquirer3.prompt(questions)

pprint(answers)

Result on something like:

{{ inquirer checkbox }}

Apart from all the controlls supported by other questions, checkbox supports some more as shown above:

  • Ctrl + A to select all.

  • Ctrl + R to un-select all the choices, even the defaults.

  • Ctrl + I to invert/toggle all the choices.

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
from pprint import pprint


sys.path.append(os.path.realpath("."))
import inquirer3  # noqa


questions = [
    inquirer3.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 = inquirer3.prompt(questions)

pprint(answers)

theme.py

import inquirer3
from inquirer3.themes import GreenPassion


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

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

Result on something like:

{{ inquirer theme }}