Skip to content

Chordium - Syntax

How to Prompt a User to Select from a List in CLI

Used to interactively get user input by displaying a selectable list of options in the terminal. This is commonly used in CLI-based tools, scripts, or automation flows to collect user choices easily.


Prerequisite

  • Ensure Node.js is installed.
  • Install the Inquirer package:
Terminal window
npm install inquirer

Basic Syntax

import inquirer from "inquirer";
const ldAnswers = await inquirer.prompt([
{
type: "list",
name: "lSelectedValue",
message: lMessage,
choices: laChoices
}
]);
console.log(ldAnswers.lSelectedValue);

Parameters

ParameterTypeDescription
typestringType of prompt. Use "list" for selectable list.
namestringKey name to access the selected value (e.g., "lSelectedValue").
laChoicesstring[]Array of options to display in the prompt list.
lMessagestringMessage/question displayed to the user before the list.

Example

const laFruits = ["Apple", "Banana", "Cherry", "Mango"];
const ldSelectedFruit = await inquirer.prompt([
{
type: "list",
name: "lSelectedValue",
message: "Choose your favorite fruit:",
choices: laFruits
}
]);
console.log("Selected Fruit is:", ldSelectedFruit.lSelectedValue);

Sample CLI Output

? Choose your favorite fruit:
❯ Apple
Banana
Cherry
Mango

If the user selects Apple, the console displays:

Selected Fruit is: Apple