Enquirer is a great foundation to build Interactive CLIs that don't leak tokens and passwords.
These are the built in prompts you get:
- AutoComplete Prompt
- BasicAuth Prompt
- Confirm Prompt
- Form Prompt
- Input Prompt
- Invisible Prompt
- List Prompt
- MultiSelect Prompt
- Numeral Prompt
- Password Prompt
- Quiz Prompt
- Survey Prompt
- Scale Prompt
- Select Prompt
- Sort Prompt
- Snippet Prompt
- Toggle Prompt
Installing:
yarn add enquirer
A basic enquirer prompt looks like this:
const { prompt } = require("enquirer");
// assuming you are in an async block
const response = await prompt({
type: "input",
name: "username",
message: "What is your username?"
});
console.log(response); // { username: 'jonschlinkert' }
Once you're familiar with the prompt options API you can use them pretty much everywhere:
{
// required
type: string | function,
name: string | function,
message: string | function | async function,
// optional
initial: string | function | async function, // The default value to return if the user does not supply a value.
format: function | async function, // Function to format user input in the terminal.
result: function | async function, // Function to format the final submitted value before it's returned.
validate: function | async function, // Function to validate the submitted value before it's returned. This function may return a boolean or a string. If a string is returned it will be used as the validation error message.
// each command will also recognize additional options
// e.g. `limit`, `choices`, `hint`, `fields`
}
Adding AutoComplete prompt:
const { prompt } = require("enquirer");
// inside async block
const response = await prompt({
type: "autocomplete",
name: "flavor",
message: "Pick your favorite flavor",
limit: 10,
choices: [
"Almond",
"Apple",
"Banana",
"Blackberry",
"Blueberry",
"Cherry",
"Chocolate",
"Cinnamon",
"Coconut",
"Cranberry",
"Grape",
"Nougat",
"Orange",
"Pear",
"Pineapple",
"Raspberry",
"Strawberry",
"Vanilla",
"Watermelon",
"Wintergreen"
]
});
Principles to help your CLI be CI friendly:
- Every prompt should have a corresponding flag that skips the prompt
- Every prompt should inform the user about what flag they can use to skip the prompt (nice to have)