Simple helper tools for interacting synchronously with user at command line. The interface is used with process.stdin
and process.stdout
in order to accept user input.
var query = require('cli-interact').getYesNo;
var answer = query('Is it true');
console.log('you answered:', answer);
$ node examples/getYesNoNone.js
Is it true (y/n)? j
Is it true (y/n)? y
you answered: true
var choices = ['All','None','First','Last'];
var query = require('cli-interact').getChar;
var answer;
answer = query('Tell me one of '+choices.toString(), choices.toString());
console.log('you answered:', answer);
npm install cli-interact
Presently, there are three queries:
getChar
gets a single character from some set of allowed characters. (now it takes more than one character)
getIPversion
gets a single character, either '4' or '6'.
getYesNo
prompts for 'y' or 'n', returns true/false. Can be configured to allow no response (returns undefined
).
And a passthorugh question
to access the underlying readlineSync.question
method.
Uses the readline-sync module. See it for more information.
The your Node and OS may not support interactively reading from stdin. The stdin interfaces are different by platforms. If in those platforms, an error is thrown by readlineSync.
try {
answer = readlineSync.question('What is your favorite food? :');
} catch (e) {
console.error(e);
process.exit(1);
}
- 2014-11-14 v0.1.0 Initial release.