简体   繁体   English

如何在 Node js 中通过控制台日志显示选项列表并要求用户通过控制台日志从显示的列表中输入一个选项

[英]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如何在 Node js 中通过控制台日志显示选项列表并要求用户通过控制台日志从显示的列表中输入一个选项

Expected behavior user@desktop:~$ node test.js预期行为 user@desktop:~$ node test.js

  1. Option 1选项1
  2. Option 2选项 2
  3. Option 3选项 3

Select option to continue: 3 You have selected option 3 Select 选项继续:3 您已选择选项 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();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 console.log() 对象不会记录通过节点 js 控制台中的原型添加的方法。 或者如何打印原型? - console.log() an object does not log the method added via prototype in node js console. Or how to print the prototypes also? 如何通过 console.log 将 css 文件中的值显示到控制台 - How to display value from the css file to console via console.log 如何控制台记录JS对象的选项? - How to console log an option of a JS object? 如何使用javascript node.js中的console.log打印函数内的变量列表 - how can I print a list of variables within a function using console.log in javascript node.js 如何在 javascript (node.js) 中通过控制台输入? - How to take input via console in javascript (node.js)? 如何使用 node.js 的控制台日志打开命令提示符 - How to open a command prompt with the console log from node.js mongoDB connect(通过Node.js驱动程序)不会给我控制台日志消息 - mongoDB connect (via Node.js driver) doesn't give me console log msg 通过 `chrome.storage.sync.get` 访问 'content.js' 中的 localstorage 并在 console.log 中显示令牌 - Access localstorage in 'content.js' via `chrome.storage.sync.get` and display the token in console.log 如何console.log列表的一部分? - How to console.log a part of a list? JS-如何通过控制台从 html 中删除 display:none - JS- how to remove display:none from html via console
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM