[英]Take multiple inputs from user
I want to get user input from user. 我想从用户那里获得用户输入。 I have created a function to do that by taking the code from node.js documentation. 我已经创建了一个函数,通过从node.js文档中获取代码来做到这一点。 But I cannot receive multiple user inputs. 但是我无法收到多个用户输入。 How can I do that? 我怎样才能做到这一点? This is the code so far. 到目前为止,这是代码。
function getUserInput() {
rl.question('Please input a letter: ', (answer) => {
console.log('Letter entered: ${answer}');
rl.close();
});
}
//getUserInput();
var k=0;
while ( k < 3 ){
getUserInput();
k++;
}
I expect to take for example 3 user inputs. 我希望以3个用户输入为例。 I want to take user input. 我想接受用户输入。 With the code above I can only take only ONE user Input. 使用上面的代码,我只能接受一个用户输入。 I thought adding the function into a loop so it could work. 我以为将函数添加到循环中可以正常工作。 I am looking for any modification into my code so It can work and so I can take more than one user input. 我正在寻找对我的代码进行的任何修改,以便它可以正常工作,因此我可以接受多个用户输入。
Inquirer is probably what you're looking for, assuming that you're trying to get user input on the command line. 假设您试图在命令行上获取用户输入,那么Inquirer可能就是您要查找的内容。
Description taken from the repo itself: 描述来自仓库本身:
Inquirer.js strives to be an easily embeddable and beautiful command line interface for Node.js (and perhaps the "CLI Xanadu"). Inquirer.js致力于成为Node.js(也许还有“ CLI Xanadu”)的易于嵌入且美观的命令行界面。
Inquirer.js should ease the process of Inquirer.js应该简化
- providing error feedback 提供错误反馈
- asking questions 问问题
- parsing input 解析输入
- validating answers 验证答案
- managing hierarchical prompts 管理分层提示
Also, here's an example on how to use it: 另外,这是一个有关如何使用它的示例:
const inquirer = require('inquirer');
const questions = [
{
type: 'input',
name: 'first_name',
message: "What's your first name"
}, {
type: 'input',
name: 'last_name',
message: "What's your last name",
default: function() {
return 'Doe';
}
}
];
inquirer.prompt(questions).then(answers => {
console.log(JSON.stringify(answers, null, ' '));
});
Cheers. 干杯。
This appears to be using something like the promises API. 这似乎正在使用诸如promises API之类的东西。
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
function getUserInput(n) {
rl.question('Please input a letter: ', (answer) => {
console.log(`Letter entered: ${answer}`);
if (n < 3) {
getUserInput(n+1);
} else {
rl.close();
}
});
}
getUserInput(1);
When I run it, this happens: 当我运行它时,会发生以下情况:
$ node getinput.js
Please input a letter: a
Letter entered: a
Please input a letter: b
Letter entered: b
Please input a letter: c
Letter entered: c
I think the problem is that rl.question waits for an input before proceeding but the while loop doesn't. 我认为问题在于rl.question在继续进行之前等待输入,而while循环则没有。 Here's a simple node cli that does what you're after, I think. 我认为这是一个简单的节点cli,它可以满足您的需求。 Just save this code in a file named index.js and go to the directory and type: node index.js
只需将此代码保存在名为index.js的文件中,然后转到目录并键入: node index.js
// Dependencies
var readline = require('readline');
var cli = {};
// Init function
cli.init = function(){
// Send the start message to the console in magenta
console.log('\x1b[35m%s\x1b[0m',"The CLI is running");
// Start the interface
var _interface = readline.createInterface({
input: process.stdin,
output : process.stdout,
prompt : '>'
});
var arrayOfInputs = [];
var k = 0;
var max = 3;
cli.getUserInput = function(){
_interface.question("Ask Something? ", function(str){
k++;
arrayOfInputs.push(str);
if(k < max){
cli.getUserInput();
} else {
console.log(...arrayOfInputs);
}
});
};
cli.getUserInput();
};
cli.init();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.