What is read?
The 'read' npm package is a simple utility for reading user input from the command line. It is often used to prompt users for information in a synchronous manner, making it useful for command-line applications and scripts.
What are read's main functionalities?
Basic User Input
This feature allows you to prompt the user for input and handle the response. The example code asks the user for their name and then prints a greeting.
const read = require('read');
const options = {
prompt: 'What is your name? '
};
read(options, function(err, result) {
if (err) {
console.error('Error:', err);
} else {
console.log('Hello, ' + result + '!');
}
});
Password Input
This feature allows you to prompt the user for sensitive information like passwords without displaying the input on the screen. The example code asks the user for a password and confirms receipt without showing the password.
const read = require('read');
const options = {
prompt: 'Enter your password: ',
silent: true
};
read(options, function(err, result) {
if (err) {
console.error('Error:', err);
} else {
console.log('Password received.');
}
});
Default Values
This feature allows you to provide a default value for the user input. The example code asks the user for their favorite color and defaults to 'blue' if no input is provided.
const read = require('read');
const options = {
prompt: 'What is your favorite color? ',
default: 'blue'
};
read(options, function(err, result) {
if (err) {
console.error('Error:', err);
} else {
console.log('Your favorite color is ' + result + '.');
}
});
Other packages similar to read
prompt
The 'prompt' package is a more feature-rich alternative to 'read'. It supports validation, default values, and complex prompts with multiple fields. It is more suitable for applications that require more sophisticated user input handling.
inquirer
The 'inquirer' package is another alternative that provides a more interactive experience. It supports various types of prompts like lists, checkboxes, and password fields. It is ideal for building interactive command-line interfaces.
readline-sync
The 'readline-sync' package allows for synchronous reading of user input from the command line. It is similar to 'read' but provides more control over the input process, including the ability to handle multiple lines of input.
read
For reading user input from stdin.
Similar to the readline
builtin's question()
method, but with a
few more features.
Usage
var read = require("read")
try {
const result = await read(options, callback)
} catch (er) {
console.error(er)
}
Options
Every option is optional.
prompt
What to write to stdout before reading input.silent
Don't echo the output as the user types it.replace
Replace silenced characters with the supplied character value.timeout
Number of ms to wait for user input before giving up.default
The default value if the user enters nothing.edit
Allow the user to edit the default value.terminal
Treat the output as a TTY, whether it is or not.input
Readable stream to get input data from. (default process.stdin
)output
Writable stream to write prompts to. (default: process.stdout
)
If silent is true, and the input is a TTY, then read will set raw
mode, and read character by character.
Contributing
Patches welcome.