简体   繁体   中英

How is it possible in Node js to display list of options via console log and ask user to input a option from the displayed list via console log

How is it possible in Node js to display list of options via console log and ask user to input a option from the displayed list via console log

Expected behavior user@desktop:~$ node test.js

  1. Option 1
  2. Option 2
  3. Option 3

Select option to continue: 3 You have selected option 3

Figured out a method using inquirer module

const inquirer = require('inquirer');
function main() {
inquirer
  .prompt([
    {
      type: 'list',
      name: 'Options',
      message: 'Select an Item from below list',
      choices: ['alligator', 'crocodile', 'Exit'],
    },
  ])
  .then(answers => {
        if (answers.Options == "Exit"){
                process.exit()
        } else {
                console.info('Answer:', answers.Options);
        }
  });
}
if (require.main === module) {
  main();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM