Language: Python
CLI/Utils
PyInquirer was created as a Python adaptation of the popular Inquirer.js library for Node.js. It allows developers to easily build interactive CLIs, making terminal-based tools more user-friendly and visually appealing.
PyInquirer is a Python library that enables the creation of beautiful, interactive command-line interfaces with prompts, lists, checkboxes, and other input types inspired by Inquirer.js.
pip install PyInquirerconda install -c conda-forge pyinquirerPyInquirer provides different types of prompts such as input, confirm, list, checkbox, password, and rawlist. It supports nested questions, validation, and default values, enabling interactive CLI experiences.
from PyInquirer import prompt
questions = [{
'type': 'input',
'name': 'username',
'message': 'Enter your username:'
}]
answers = prompt(questions)
print(answers['username'])Prompts the user to enter a username and prints the input.
from PyInquirer import prompt
questions = [{
'type': 'confirm',
'name': 'continue',
'message': 'Do you want to continue?',
'default': True
}]
answers = prompt(questions)
print(answers['continue'])Asks a yes/no question and returns True or False.
from PyInquirer import prompt
questions = [{
'type': 'list',
'name': 'color',
'message': 'Pick a color:',
'choices': ['Red', 'Green', 'Blue']
}]
answers = prompt(questions)
print(answers['color'])Displays a list of options and lets the user select one.
from PyInquirer import prompt
questions = [{
'type': 'checkbox',
'name': 'features',
'message': 'Select features:',
'choices': [
{'name': 'Feature A'},
{'name': 'Feature B'},
{'name': 'Feature C'}
]
}]
answers = prompt(questions)
print(answers['features'])Allows multiple selections from a list using checkboxes.
from PyInquirer import prompt
questions = [{
'type': 'input',
'name': 'age',
'message': 'Enter your age:',
'validate': lambda val: val.isdigit() or 'Please enter a number'
}]
answers = prompt(questions)
print(answers['age'])Validates user input; here it ensures the input is numeric.
from PyInquirer import prompt
questions = [{
'type': 'input',
'name': 'name',
'message': 'What is your name?'
}, {
'type': 'confirm',
'name': 'confirm',
'message': 'Is that correct?'
}]
answers = prompt(questions)
print(answers)Shows multiple questions sequentially and collects responses in a dictionary.
Use descriptive messages for prompts to improve user experience.
Validate inputs to avoid invalid data.
Group related questions for clarity.
Use defaults when appropriate to simplify the workflow.
Keep the interface simple and intuitive for command-line users.