What is prompt-sync?
The prompt-sync npm package is a simple, synchronous way to get user input from the command line in Node.js applications. It is particularly useful for small scripts and command-line tools where asynchronous input handling is not necessary.
What are prompt-sync's main functionalities?
Basic Input
This feature allows you to prompt the user for input and capture their response synchronously. The example code asks the user for their name and then greets them.
const prompt = require('prompt-sync')();
const name = prompt('What is your name? ');
console.log(`Hello, ${name}!`);
Default Values
You can provide a default value that will be used if the user simply presses Enter without typing anything. The example code asks for the user's favorite color and defaults to 'blue' if no input is provided.
const prompt = require('prompt-sync')();
const color = prompt('What is your favorite color? ', 'blue');
console.log(`Your favorite color is ${color}.`);
Hidden Input
This feature allows you to hide the user's input, which is useful for sensitive information like passwords. The example code prompts the user for a password and masks the input with asterisks.
const prompt = require('prompt-sync')();
const password = prompt('Enter your password: ', {echo: '*'});
console.log('Password received.');
Other packages similar to prompt-sync
readline-sync
The readline-sync package provides similar synchronous input capabilities but with more advanced features like input validation, history, and autocompletion. It is more feature-rich compared to prompt-sync but also slightly more complex to use.
inquirer
Inquirer is a more advanced package for handling user input in command-line applications. It supports asynchronous prompts, multiple types of questions (e.g., lists, checkboxes), and more complex workflows. It is more suitable for larger applications that require more sophisticated user interactions.
SYNOPSIS
A sync prompt for node. very simple. no C++ bindings and no bash scripts.
BASIC MODE
var prompt = require('prompt-sync').prompt;
process.stdout.write('How many more times? ')
var n = prompt()
WITH HISTORY
prompt = require('prompt-sync')
returns an object with three methods:
prompt.init(history-filename, max-history) // open history file.
prompt.save() // save history back to file
prompt.prompt(option) // do sync io from stdin
ADVANCED FUNCTIONS
prompt.prompt()
takes an optional argument object with the following properties
hidden
: Default is false
. This prevents echo-back during text entry
echo
: Default is '*'
. Echo character to be used. For no echo set this to ''
tabComplete
: A completer function that will be called when user enters TAB to allow for autocomplete. It takes
a string as an argument an returns an array of strings that are possible matches for completion. An empty array
is returned if there are no matches.
value
: The initial value for the prompt.
sigint
: Default is false
. A ^C may be pressed during the input process to abort the text entry. If sigint it false
, prompt returns null
. If sigint is true
the ^C will be handled in the traditional way: as a SIGINT signal causing process to exit with code 130.
ask
: The question being asked of the user, will be output to terminal
LINE EDITING
Line editing is enabled in the non-hidden mode. (use up/down arrows for history and backspace and left/right arrows for editing)
History is not set when using hidden mode.
EXAMPLES
var prompt = require('./index')
var commands = ['hello1234', 'he', 'hello', 'hello12', 'hello123456'];
function tabComplete(str) {
var i;
var ret = [];
for (i=0; i< commands.length; i++) {
if (commands[i].indexOf(str) == 0)
ret.push(commands[i]);
}
return ret;
};
prompt.init();
console.log('enter name');
var name = prompt.prompt({tabComplete: tabComplete});
console.log('enter echo * password');
var pw = prompt.prompt({hidden:true});
console.log('enter no echo password');
var pwb = prompt.prompt({hidden:true, echo: ''});
console.log('Name: %s, Password *: %s, Password no echo: ', name, pw, pwb);
prompt.save();