What is inquirer?
The inquirer npm package is a collection of common interactive command line user interfaces. It provides an easy way to capture user input in a variety of ways such as lists, confirmations, and text input, which are useful for building command-line tools and scripts.
What are inquirer's main functionalities?
Text Input
Captures freeform text input from the user.
const inquirer = require('inquirer');
inquirer.prompt([{ type: 'input', name: 'username', message: 'What is your username?' }]).then(answers => { console.log(`Hello, ${answers.username}!`); });
Confirmation
Asks the user a yes/no question.
const inquirer = require('inquirer');
inquirer.prompt([{ type: 'confirm', name: 'continue', message: 'Do you wish to continue?' }]).then(answers => { console.log(`You chose to ${answers.continue ? 'continue' : 'stop'}.`); });
List
Presents a list of options for the user to choose from.
const inquirer = require('inquirer');
inquirer.prompt([{ type: 'list', name: 'selection', message: 'Choose an option:', choices: ['Option 1', 'Option 2', 'Option 3'] }]).then(answers => { console.log(`You selected: ${answers.selection}`); });
Checkbox
Allows the user to select multiple options from a list.
const inquirer = require('inquirer');
inquirer.prompt([{ type: 'checkbox', name: 'features', message: 'What features do you want?', choices: ['Feature A', 'Feature B', 'Feature C'] }]).then(answers => { console.log(`You selected: ${answers.features.join(', ')}`); });
Password
Asks the user for a password, input is hidden on the terminal.
const inquirer = require('inquirer');
inquirer.prompt([{ type: 'password', name: 'password', message: 'Enter your password:' }]).then(answers => { console.log('Password captured'); });
Other packages similar to inquirer
prompt
Prompt is another command-line input collector with a slightly different API. It has a simpler interface but lacks some of the more advanced features and UI components that inquirer provides.
enquirer
Enquirer is a stylish, minimal alternative to inquirer with a more modern promise-based API. It offers similar functionality but with a focus on being lightweight and fast.
vorpal
Vorpal is a framework for building interactive CLI applications. It includes a command-line interface with a suite of interactive prompts, but it's more focused on building the entire CLI tool rather than just handling prompts.
Inquirer.js ![Build Status](https://travis-ci.org/SBoudrias/Inquirer.js.png?branch=master)
A collection of common interactive command line user interfaces.
Goal and philosophy
We strive at providing easily embeddable and beatiful command line interface for Node.js ;
some hope in becoming the CLI Xanadu.
Inquirer should ease the process of asking end user questions, parsing, validating answers, and providing error feedback.
Inquirer provide the user interface, and the inquiry session flow. If you're searching for a full blown command line program utility, then check out Commander.js (inspired by) or Charm (used internally).
Documentation
Installation
npm install inquirer
var inquirer = require("inquirer");
inquirer.prompt([], function( answers ) {
});
Examples (Run it and see it)
Checkout the examples/
folder for code and interface examples.
node examples/pizza.js
# etc
Methods
inquirer.prompt( questions, callback )
Launch the prompt interface (inquiry session)
Objects
Question
A question object is a hash
containing question related values:
- type: (String) Type of the prompt. Defaults:
input
- Possible values: input
, confirm
,
list
, rawlist
- name: (String) The name to use when storing the answer in the anwers hash.
- message: (String) The question to print.
- default: (String) Default value to use if nothing is entered
- choices: (Array) Choices array.
Values can be simple string
s, or object
s containing a name
(to display) and a value
properties (to save in the answers hash). - validate: (Function) Receive the user input and should return
true
if the value is valid, and an error message (String
) otherwise. If no error message is returned, a default one is provided. - filter: (Function) Receive the user input and return the filtered value to be used inside the program. The value returned will be added to the
Answers
hash.
validate
and filter
function can be asynchronous using this.async()
. You just have to pass the value you'd normally return to the callback option.
{
validate: function(input) {
var done = this.async();
setTimeout(function() {
if (typeof input !== "number") {
done("You need to provide a number");
return;
}
done(true);
}, 3000);
}
}
Answers
A key/value hash containing the client answers in each prompt.
- Key The
name
property of the question object - Value (Depends on the prompt)
confirm
: (Boolean)input
: User input (String)rawlist
, list
: Selected choice value (or name if no value specified) (String)
Prompts
List - { type: "list" }
Take type
, name
, message
, choices
[, filter
] properties.
What about the toping?
[X] Peperonni and chesse
[ ] All dressed
[ ] Hawaïan
(Use arrow key)
Raw List - { type: "rawlist" }
Take type
, name
, message
, choices
[, filter
] properties.
You also get a free 2L liquor! Which one?
1) Pepsi
2) 7up
3) Coke
Default (1)
Confirm - { type: "confirm" }
Take type
, name
, message
properties.
Hi, welcome to Node Pizza plaza. Is it for a delivery? (Y/n)
Input - { type: "input" }
Take type
, name
, message
[, default
, filter
, validate
] properties.
Any comments on your purchase experience? (default "Nope, all good!"):
News on the march (Release notes)
- 0.1.3 : Add async support for validation and filtering functions.
- 0.1.0 : First official release. There's 4 prompt types:
input
, confirm
, list
and
rawlist
. There's functionnality to allow the validation of input, and the filtering of values.
License
Copyright (c) 2012 Simon Boudrias
Licensed under the MIT license.