Ways to get input from node js command line program.

Ways to get input from node js command line program.

Input from a command-line interface (CLI) application can be received in Node.js in a number of different ways. The following are a few of the most typical methods:

1. process.argv

The arguments given from the command line to the Node.js process when it was started are contained in this array. The user-provided parameters begin at index 2 because the first two members of the array are set aside for the running script and the Node.js executable.

Below is an example of how to use it:

file: index.js

const args = process.argv.slice(2);

console.log(`Command-line arguments: ${args}`);

command line: command

node script.js arg1 arg2 arg3

output:

Command-line arguments: arg1,arg2,arg3

2. process.stdin

The Node.js process can read data from the normal input stream using this stream. This may be utilized to allow user input without blocking.

Below is an example of how to use it:

file: index.js

process.stdin.setEncoding('utf8');

process.stdin.on('readable', () => {
  const chunk = process.stdin.read();
  if (chunk !== null) {
    console.log(`Received data: ${chunk}`);
  }
});

process.stdin.on('end', () => {
  console.log('End of data');
});
  • process.stdin stream's encoding to "utf8"

  • use the "readable" event to monitor the stream for new data.

  • When data is available, we use the read() method to read a single chunk of data.

  • utilize the "end" event to watch for the stream of data to come to a stop.

process.stdin stream to quickly read a single line of user input.

This method can be used to read data from a pipe or other source or to take user input in a command-line interface (CLI) application.

3. readline

A built-in Node.js module called readline offers a straightforward interface for reading input from the command line. It offers capabilities like line editing and history and enables the user to read one line of data at a time.

Below is an example of how to use it:

file: index.js

const readline = require('readline');

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

rl.question('Your Name: ', (answer) => {
  console.log(`My name is ${answer}`);
  rl.close();
});
  • we use the createInterface() method of the readline module to create a readline.Interface.

  • an object that reads from the standard input stream (process.stdin).

  • writes to the standard output stream (process.stdout).

  • the question() method of the readline.Interface object to prompt the user for input, it takes a prompt string and a callback function as arguments.

  • the user enters a response and hits the enter key, the callback function is executed with the user's response as an argument.

  • the callback function logs the user's response to the console and closes the readline.Interface object using the close() method.

A simple way to read a single line of input from the user using the readline module.

While there are many third-party libraries are also available:

1. minimist

A well-liked third-party module that may be used to parse command-line options and arguments is the minimist module. It offers an easy-to-use API for retrieving the generated results and lets the user define parameters and flags in a number of formats.

3. yargs

Another well-liked external module that may be used to parse command-line options and arguments is yargs. It offers a strong and adaptable API for specifying, checking, and producing use documentation for options.

TL;DR

  1. process.argv: The arguments given from the command line to the Node.js process when it was started are contained in this array. The user-provided parameters begin at index 2 because the first two members of the array are set aside for the running script and the Node.js executable.

  2. process.stdin: The Node.js process can read data from the normal input stream using this stream. This may be utilized to allow user input without blocking.

  3. readline: It offers capabilities like line editing and history and enables the user to read one line of data at a time.

For straightforward command-line programs that need to read and process one line of input at a time, a readline module is a good option.

For more complex applications that require non-blocking data reading or that require additional control over the input and output streams, stdin is a reasonable option.

Thanks for reading till last, I hope you have understood the basics.