Collection of common interactive command line user interfaces, based on Inquirer.js
Project description
VERSION |
DOWNLOADS |
TESTS |
COVERAGE |
---|---|---|---|
Collection of common interactive command line user interfaces, based on Inquirer.js.
Goal and Philosophy
Born as a Inquirer.js clone, it shares part of the goals and philosophy.
So, Inquirer should ease the process of asking end user questions, parsing, validating answers, managing hierarchical prompts and providing error feedback.
Documentation
Installation
pip install inquirer
Usage example:
import inquirer
questions = [
inquirer.Text ('name', message="What's your name"),
inquirer.Password('password', message="Add a password"),
inquirer.Confirm ('correct', message="Is correct"),
]
answers = inquirer.prompt(questions)
Changelog
It is maintained in the separated file changes.rst
Examples
The examples/ directory contains several examples. Feel free to run them:
python examples/text.py
Objects
The main object is Question, but it should not be instantiated. You must use any of the subclasses, listed below. All of them have the next attributes that can be set in the initialization:
Attribute |
Type |
Explanation |
name |
String |
The key in the hash of answers. |
message |
String|Function |
To be shown in the prompt to the user. Functions will receive the hash with previous values. |
default |
Any|Function |
Default value. Functions will receive the hash with previous values. |
choices |
List|Function |
List of available options. Functions will receive the hash with previous values. |
validate |
Bool|Function |
If the value set is valid. Functions will receive the hash with previous values and the value set in this question, and should return a boolean. |
ignore |
Bool|Function |
If the quiestion should be shown. Functions will receive the hash with previous values and should return a boolean. |
Prompt types
Text
choices argument is not used.
Example:
import inquirer
questions = [
inquirer.Text('name', message="What's your name"),
inquirer.Text('surname', message="What's your surname"),
inquirer.Text('phone', message="What's your phone number",
validate=lambda x, _: re.match('\d+', x),
)
]
answers = inquirer.prompt(questions)
Password
choices argument is not used.
Example:
import inquirer
questions = [
inquirer.Password('password', message="What's your password"),
]
answers = inquirer.prompt(questions)
Confirm
choices argument is not used.
Example:
import inquirer
questions = [
inquirer.Confirm('continue', message="Should I continue"),
inquirer.Confirm('stop', message="Should I stop", default=True),
]
answers = inquirer.prompt(questions)
List
Shows a list of choices, and allows the selection of one of them.
Example:
import inquirer
questions = [
inquirer.List('size',
message="What size do you need?",
choices=['Jumbo', 'Large', 'Standard', 'Medium', 'Small', 'Micro'],
),
]
answers = inquirer.prompt(questions)
Checkbox
Shows a list of choices, with multiple selection.
Example:
import inquirer
questions = [
inquirer.Checkbox('interests',
message="What are you interested in?",
choices=['Computers', 'Books', 'Science', 'Nature', 'Fantasy', 'History'],
),
]
answers = inquirer.prompt(questions)
Advanced usage
Some tips:
Instantiate from JSON
From version 2.0.2, you can instantiate from JSON strings:
import inquirer
json = """[
{"name": "name", "kind": "text", "message"="What's your name?"},
{"name": "surname", "kind": "text", "message"="{name}, what's your surname?"},
{"name": "alias", "kind": "text", "message"="What's your Alias", "default"="{name}"}
]"""
questions = [inquirer.load_from_json(q) for q in json]
answers = inquirer.prompt(questions)
As you can see, it is mandatory to add the name
and kind
fields. The rest of fields are the same that was documented.
The JSON version has a problem: As you can imagine, it is not possible to use functions anywhere.
There is a shortcut from version 2.0.3 to load lists of questions, because it is supported from load_from_json
directly:
import inquirer
json = """[
{"name": "name", "kind": "text", "message"="What's your name?"},
{"name": "surname", "kind": "text", "message"="{name}, what's your surname?"},
{"name": "alias", "kind": "text", "message"="What's your Alias", "default"="{name}"}
]"""
questions = inquirer.load_from_json(json)
answers = inquirer.prompt(questions)
Reusing previous answers
Every String argument but name can use any previous answer just putting it in roots:
import inquirer
questions = [
inquirer.Text('name', message="What's your name?"),
inquirer.Text('surname', message="{name}, what's your surname?"),
inquirer.Text('alias', message="What's your Alias"
default="{name}"),
]
answers = inquirer.prompt(questions)
License
Copyright (c) 2014 Miguel Ángel García (@magmax9), based on Inquirer.js, by Simon Boudrias (@vaxilart)
Licensed under the MIT license.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.