JAVA SCRIPT - Getting Input from the Terminal - Supercoders | Web Development and Design | Tutorial for Java, PHP, HTML, Javascript JAVA SCRIPT - Getting Input from the Terminal - Supercoders | Web Development and Design | Tutorial for Java, PHP, HTML, Javascript

Breaking

Post Top Ad

Post Top Ad

Saturday, March 9, 2019

JAVA SCRIPT - Getting Input from the Terminal

Getting Input from the Terminal


Problem

You want to get input from the application user via the terminal.

Solution 

Use Node’s Readline module. To get data from the standard input, use code such as the following:

var readline = require('readline');
var rl = readline.createInterface({
 input: process.stdin,
 output: process.stdout
});
rl.question(">>What's your name? ", function(answer) {
 console.log("Hello " + answer);
 rl.close();
});


EXPLAIN

The Readline module provides the ability to get lines of text from a readable stream. You start by creating an instance of the Readline interface with createInterface() passing in, at minimum, the readable and writable streams. 

You need both, because you’re writing prompts, as well as reading in text. In the solution, the input stream is process.stdin, the standard input stream, and the output stream is process.stdout. In other words, input and output are from, and to, the command line. 

The solution used the question() function to post a question, and provided a callback function to process the response. Within the function, close() was called, which closes the interface, releasing control of the input and output streams. 

You can also create an application that continues to listen to the input, taking some action on the incoming data, until something signals the application to end. 

Typically that something is a letter sequence signaling the person is done, such as the word exit. This type of application makes use of other Readline functions, such as setPrompt() to change the prompt given the individual for each line of text, prompt(), which prepares the input area, including changing the prompt to the one set by setPrompt(), and write(), to write out a prompt. In addition, you’ll also need to use event handlers to process events, such as line, which listens for each new line of text.

contains a complete Node application that continues to process input from the user until the person types in exit. Note that the application makes use of process.exit(). This function cleanly terminates the Node application.

Access numbers from stdin until the user types in exit


var readline = require('readline');
var sum = 0;
var rl = readline.createInterface({
 input: process.stdin,
 output: process.stdout
});
console.log("Enter numbers, one to a line. Enter 'exit' to quit.");
rl.setPrompt('>> ');
rl.prompt();
rl.on('line', function(input) {
 input = input.trim();
 if (input == 'exit') {
 rl.close();
 return;
 } else {
 sum+= Number(input);
 }
 rl.prompt();
});
// user typed in 'exit'
rl.on('close', function() {
 console.log("Total is " + sum);
 process.exit(0);
});


Running the application with several numbers results in the following output:

Enter numbers, one to a line. Enter 'exit' to quite.
>> 55
>> 209
>> 23.44
>> 0
>> 1
>> 6
>> exit
Total is 294.44

No comments:

Post a Comment

Post Top Ad