Reading value from console, interactively

how to get input value in node js
how to take input from console in typescript
node prompt user for input
javascript readline console
readline-sync
how to read from command line in node
nodejs command line
npm readline

I thought to make an simple server http server with some console extension. I found the snippet to read from command line data.

  var i = rl.createInterface(process.stdin, process.stdout, null);
  i.question('Write your name: ', function(answer) {
    console.log('Nice to meet you> ' + answer);
    i.close();
    process.stdin.destroy();

  });

well to ask the questions repeatedly, i cant simply use the while(done) { } loop? Also well if the server receives output at the question time, it ruins the line.


you can't do a "while(done)" loop because that would require blocking on input, something node.js doesn't like to do.

Instead set up a callback to be called each time something is entered:

var stdin = process.openStdin();

stdin.addListener("data", function(d) {
    // note:  d is an object, and when converted to a string it will
    // end with a linefeed.  so we (rather crudely) account for that  
    // with toString() and then trim() 
    console.log("you entered: [" + 
        d.toString().trim() + "]");
  });

Reading value from console, interactively - node.js - html, I found the snippet to read from command line data. var i = rl.createInterface(​process.stdin, process.stdout, null); i.question('Write your name: ', function(​answer)  Reading value from console, interactively I thought to make an simple server http server with some console extension. I found the snippet to read from command line data.


I've used another API for this purpose..

var readline = require('readline');
var rl = readline.createInterface(process.stdin, process.stdout);
rl.setPrompt('guess> ');
rl.prompt();
rl.on('line', function(line) {
    if (line === "right") rl.close();
    rl.prompt();
}).on('close',function(){
    process.exit(0);
});

This allows to prompt in loop until the answer is right. Also it gives nice little console.You can find the details @ http://nodejs.org/api/readline.html#readline_example_tiny_cli

Reading value from console, interactively, I thought to make an simple server http server with some console extension. I found the snippet to read from command line data. var i = rl. Use the ReadLine() method to read input from the console in C#. This method receives the input as string, therefore you need to convert it. For example − Let us see how to get user input from user and convert it to integer. Firstly, read user input − string val; Console.Write("Enter integer: "); val = Console.ReadLine();


The Readline API has changed quite a bit since 12'. The doc's show a useful example to capture user input from a standard stream :

const readline = require('readline');

const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout
});

rl.question('What do you think of Node.js? ', (answer) => {
  console.log('Thank you for your valuable feedback:', answer);
  rl.close();
});

More information here.

Accept input from the command line in Node, js CLI program interactive? Node since version 7 provides the readline module to perform exactly this: get input from a readable stream such as  This article explains how to get an input from the console. If you have used C++ then cin and cout are the functions for read and write to the console. Type this code in a text editor and save file as read.cs. using System; class ReadStr {public static void Main() {Console.Write("Enter your name:"); string szName = Console.ReadLine();


Please use readline-sync, this lets you working with synchronous console withouts callbacks hells. Even works with passwords:

var favFood = read.question('What is your favorite food? ', {
  hideEchoBack: true // The typed text on screen is hidden by `*` (default). 
});

Ways to read input from console in Java, In Java, there are three different ways for reading input from the user in the command line Does not work in non-interactive environment (such as in an IDE​). Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information. Reading value from console, interactively. 89. Chrome


I believe this deserves a modern async-await answer, assuming node >= 7.x is used.

The answer still uses ReadLine::question but wraps it so that the while (done) {} is possible, which is something the OP asks about explicitely.

var cl = readln.createInterface( process.stdin, process.stdout );
var question = function(q) {
    return new Promise( (res, rej) => {
        cl.question( q, answer => {
            res(answer);
        })
    });
};

and then an example usage

(async function main() {
    var answer;
    while ( answer != 'yes' ) {
        answer = await question('Are you sure? ');
    }
    console.log( 'finally you are sure!');
})();

leads to following conversation

Are you sure? no
Are you sure? no
Are you sure? yes
finally you are sure!

3 ways for reading user's input from console in Java, Java code Examples for reading input from the user in the command line (​console) System.out.println( "No console: non-interactive mode!" );. Console.Read() reads a string from console A SINGLE CHARACTER AT A TIME (but waits for an enter before going on. You normally use it in a while cycle). So if you write 25 + Enter, it will return the unicode value of 2 that is 50. If you redo a second Console.Read() it will return immediately with 53 (the unicode value of 5).


Catching user input, This option has no effect if read is not reading input from the terminal or from a pipe. -u FD, Read input from file descriptor FD. This is a straightforward example,​  Using Console Class It has been becoming a preferred way for reading user’s input from the command line. In addition, it can be used for reading password-like input without echoing the characters entered by the user; the format string syntax can also be used (like System.out.printf()).


How to read interactive command-line input with Java , Java command-line FAQ: How do I read command line input from a Java application (interactively)?. Solution: As of Java 5 (and newer Java  Stack Overflow Public questions and answers; you can use console-read-write, Reading value from console, interactively. 18.


Getting User Input in Node.js, Learn how to handle user input synchronously in Node.js. which means that reading user input from a terminal isn't as simple as calling a getInput() function. out with JavaScript development or just want to get an interactive script running​  Reading Java command-line input in the good old days. As a quick note, while this is now basically legacy information, if you want to see how we read command line input in Java in the days before Java 5, see my article titled, Java code to read command-line input.