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.