I have looked at similar questions on SO and I have an ongoing question about if it is possible to get synchronous input from the command line.
I know about readline and process.stdin.on('readable', ...) but both of those seem to be asynchronous.
I am looking to for a way to prompt the user for input where code later on in my script does not run before there is user input.
May be you can try this in case if you know fix number of inputs you want in sync manner.
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
let i = 0;
rl.question('Number of inputs : ', (answer1) => {
rl.on('line', (answer2) => {
console.log(`input: ${answer2}`);
i++;
if (i >= answer1) {
rl.close();
}
});
});
Related
I have a string with the alphabet and certain letters are in parenthesis. It means these letters should be uppercase. But the user has to be asked first if he wants it.
The problem is, I have to call readline inside a loop and this doesn't work.
const readline = require('readline');
const text = 'a(b)cdefg(h)ijklmnopqrst(u)vwxyz';
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
text.replace(/\((\w)\)/g, function (match, p1) {
let answer;
rl.question(`Do you want to change ${p1}? [yes/no] `, input => {
answer = input;
});
return answer === "yes" ? p1.toUpperCase() : p1;
});
console.log("Result:", text);
It just prints out Do you want to change b? [yes/no] Result: a(b)cdefg(h)ijklmnopqrst(u)vwxyz.
How can this be solved?
As Nick has explained in the comments the main issue of your code it that it does not handle the async readline operation correctly - here's how I would do this:
use a promisifed version of readline to prevent nested callbacks
use a while loop to step through all regex matches and await the user input in order to convert the chars to upper case or not
Something like (still needs error handling, but should give you an idea):
const readline = require('readline').createInterface({
input: process.stdin,
output: process.stdout
});
function question(query) {
return new Promise(resolve => {
readline.question(query, resolve);
})
}
const specialCharsRegex = /\((\w)\)/g;
const text = 'a(b)cdefg(h)ijklmnopqrst(u)vwxyz';
(async () => {
let result = text;
let currMatch;
while ((currMatch = specialCharsRegex.exec(text)) !== null) {
const convertInput = await question(`Do you want to change ${currMatch[1]}? [yes/no] `);
result = result.replace(currMatch[0], v => {
return convertInput === "yes" ? currMatch[1].toUpperCase() : currMatch[1];
});
}
console.log("Result: ", result);
readline.close();
})();
The main issue with your code is that you're trying to do something asynchronous in a callback that is expected to be synchronous - that is, you're trying to read user input in the .replace() callback, but reading user input doesn't happen immediately, so the callback to .question() occurs sometime after your question has been asked and your replace callback has terminated/finished. Making rl.question() synchronous would fix this (Node.js synchronous prompt) and would allow you to prompt against the exact words you're replacing.
If you want to keep things asynchronous, you could first match the letters you want to replace using .matchAll(), that way you'll have the text/groups, and the indexes of the characters you want to change. Then, you can use a loop to iterate the matches, and prompt the user whether they want to change the group. For each group, you can update the text accordingly by updating the text at the specified index. You could use replace, but if your text has duplicates eg: "a(b)c(b)e", and you said "no" for the first (b) but yes for the second (b), then you'll either need to replace both occurrences, or the first match. By using the index, you can specify exactly which group to change:
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin, //or fileStream
output: process.stdout
});
const question = q => new Promise(res => rl.question(q, res));
let text = 'a(b)cdefg(h)ijklmnopqrst(u)vwxyz';
(async () => {
const matches = text.matchAll(/\((\w)\)/g);
for(const {0: match, 1: p1, index} of matches) {
const ans = await question(`Do you want to change ${p1}? [yes/no] `);
if(ans === "yes")
text = text.slice(0, index) + match.toUpperCase() + text.slice(index + match.length);
}
console.log(text);
})();
I'm using Node's readline but it works only for the first time.
At the first try everything works well (answer is logged and readline is closed) but another time it looks like the readline is not being closed (answer is never logged and console is still waiting for my input even if I've already sent it).
I've class in Node.js (using ES6 with Babel and Nodemon) which has readline in constructor.
class SomeClass
{
constructor(readline) {
this.readline = readline;
}
askPath() {
this.readline.question('Question: ', (answer) => {
console.log(answer);
this.readline.close();
this.askPath();
});
}
}
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
let someClass = new SomeClass(rl);
someClass.askPath();
Console looks like:
> Question: answer
> answer
> Question: second answer
> I can still write
> nothing happens...
Just don't call the .close() function, inside of your callback as it closes the entire readline interface
askPath() {
this.readline.question('Question: ', (answer) => {
console.log(answer);
this.askPath();
});
}
As the documentation says:
The rl.close() method closes the readline.Interface instance and relinquishes control over the input and output streams. [source:nodejs.org]
This question already has answers here:
Get user input through Node.js console
(6 answers)
Closed last month.
I'm new to NodeJs and i'm used to C# were we can use
Console.ReadLine();
I looked into 'readline' and the node prompt package, but it either outputs all the user input twice while entering or, with the 'terminal: false' option, does not allow us to use the backspace.
var stdin = process.openStdin();
stdin.addListener("data", function(d) {
console.log("your input: " + d.toString());
});
This is like you take input value
There is a readline, you can use it like this:
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.question('How are you today? ', (answer) => {
// TODO: Log the answer in a database
console.log(`Thank you for your valuable feedback: ${answer}`);
rl.close();
});
Documentation is available here.
I have the following Node.js code that behaves strangely:
#!/usr/bin/env node
"use strict";
var readline = require('readline');
var rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
function input(prompt) {
rl.question(prompt, function (x) {
rl.close();
console.log("debug: " + x);
return x;
});
}
function main() {
var n = input("Number: ");
// console.log("value: " + n); // problematic line
}
main();
I want to mimic Python's raw_input, i.e. reading a line from the user. After showing the prompt, the program should be blocked until the user presses Enter.
If the "problematic line" is in comments, it works, the program is waiting for input. However, if this line is not in comments, then the program doesn't wait for input and n becomes undefined. Why? How to write a function that returns the user input?
That's because you are expecting the execution of input wait until return is called, which is not how this will work. The problematic line is indeed the previous one. First, input does not return anything, the return statement is the return of the question callback function, but then, you seem to misunderstand the execution flow, as we all have at some point (you'll get it pretty quick after some dead-ends like this one)
Your script is loaded
You declare and define rl, input and main
Main executes
You define n as the result of input
And here is where things start getting asynchronously funny
since question is asynchronous, its execution start but does not block the process
input returns undefined (while you're still waiting for the input)
you print that undefined
You write something on the input
question() finishes its execution and calls the callback (the function you gave as second parameter)
rl is closed
the callback function returns the line, and it is swallowed by the void (this is not technical terminology, just a metaphor)
You may want to do it like this:
#!/usr/bin/env node
"use strict";
var readline = require('readline');
var rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
function input(prompt, callback) {
rl.question(prompt, function (x) {
rl.close();
callback(x);
});
}
function main() {
var n = input("Number: ", console.log);
}
main();
If you're new to javascript and node, you may find very useful to use learnyounode and the node code school path or even, if you have the time, the money and the opportunity, read Node.js, MongoDB, and AngularJS Web Development, by Brad Dayley.
Does NodeJS has any functionality for accepting input via standard input from a user. In Browser based JS we used to use 'prompt' functionality for same but the this would not work on NodeJS standalone app.
node one.js
Enter any number:
<program accepts the number and does the processing>
As vinayr said, you should use readline. An example of what you desire:
var readline = require('readline');
var rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.question("Input number:", function(numAnswer) {
// TODO: Log the answer in a database
var num = parseInt(numAnswer);
processingFunctionYouUse(num);
rl.close();
});
Use readline prompt http://nodejs.org/api/readline.html
I would like to suggest you to use stdio. It is a module that aims to simplify your life with standard input/output. One of its main features is the standard input reading, line by line, and it can be done as follows:
var stdio = require('stdio');
stdio.readByLines(function (line) {
// This function is called for every line while they are being read
}, function (err) {
// This function is called when the whole input has been processed
});
PD: I'm the stdio creator. :-)
(source: nodei.co)