Simple command line prompting utilify.
In all the commands, the options argument is not mandatory.
Prompts for a value, printing the message
and waiting for the input.
When done, calls fn
with error
and value
.
Default options:
{
// The default value to assume. If not supplied, the input is mandatory
'default': 'default value',
// Automatically trim the input
'trim': true,
// A validator or an array of validators.
'validator': null,
// Automatically retry on error
'retry': false
}
The validators have two purposes:
function (value) {
// Validation example, throwing an error when invalid
if (value.length !== 2) {
throw new Error('Length must be 2');
}
// Parse the value, modifying it
return value.replace('aa', 'bb');
}
Example usages:
Ask for a name.
promptly.prompt('Name: ', function (err, value) {
// err is always null in this case, because no validators are set
console.log(value);
});
Ask for a name with a constraint (non-empty value and length > 2)
var validator = function (value) {
if (value.length < 2) {
throw new Error('Min length of 2');
}
return value;
}
promptly.prompt('Name: ', { validator: validator }, function (err, value) {
if (err) {
console.error('Invalid name');
// Manually call retry
// The passed error have a retry method to easily prompt again.
err.retry();
}
console.log('Name is: ', value);
});
Same as above but retry automatically
var validator = function (value) {
if (value.length < 2) {
throw new Error('Min length of 2');
}
return value;
}
promptly.prompt('Name: ', { validator: validator , retry: true}, function (err, value) {
// err is always null because promptly will prompting for a name until it validates
console.log('Name is: ', value);
});
Ask the user to confirm something.
Calls fn
with error
and value
(true or false).
The available options are the same, except that retry
defauls to true
.
Truthy values are: y
, yes
, 1
and true
.
Falsy values are n
, no
, 0
and false
.
Comparison is made in case insensitive way.
Example usage:
promptly.confirm('Are you sure? ', function (err, value) {
console.log('Answer: ', value);
});
Ask the user to choose between multiple choices
(array of choices).
Calls fn
with error
and value
(true or false).
The available options are the same, except that retry
defauls to true
.
promptly.choose('Do you want an apple or an orange? ', ['apple', 'orange'], function (err, value) {
console.log('Answer: ', value);
});
Prompts for a password, printing the message
and waiting for the input.
When available, calls fn
with error
and value
.
The available options are the same, except that trim
defauls to false
.
promptly.password('password: ', function (err, value) {
console.log('password is ' + value);
});
Released under the MIT License.