promptly
Simple command line prompting utility.
Installation
$ npm install promptly
API
.prompt(message, [options])
Prompts for a value, printing the message
and waiting for the input.
Returns a promise that resolves with the input.
Available options:
Name | Description | Type | Default |
---|
default | The default value to use if the user provided an empty input | string | undefined |
trim | Trims the user input | boolean | true |
validator | A validator or an array of validators | Function/Array | undefined |
retry | Retry if any of the validators fail | boolean | true |
silent | Do not print what the user types | boolean | false |
replace | Replace each character with the specified string when silent is true | string | '' |
input | Input stream to read from | Stream | process.stdin |
output | Output stream to write to | Stream | process.stdout |
The same options are available to all functions but with different default values.
Examples
-
Ask for a name:
const name = await promptly.prompt('Name: ');
console.log(name);
-
Ask for a name with a constraint (non-empty value and length > 2):
const validator = function (value) {
if (value.length < 2) {
throw new Error('Min length of 2');
}
return value;
};
const name = await promptly.prompt('Name: ', { validator });
console.log('Name is:', value);
-
Same as above but do not retry automatically:
var validator = function (value) {
if (value.length < 2) {
throw new Error('Min length of 2');
}
return value;
};
try {
const name = await promptly.prompt('Name: ', { validator, retry: false });
console.log('Name is:', value);
} catch (err) {
console.error('Invalid name:')
console.error(`- ${err.message}`);
}
Validators
The validators have two purposes: to validate and to transform input.
(value) => {
if (value.length !== 2) {
throw new Error('Length must be 2');
}
return value.replace('aa', 'bb');
}
.confirm(message, [options])
Ask the user for confirmation, printing the message
and waiting for the input.
Returns a promise that resolves with the answer.
Truthy values are: y
, yes
and 1
. Falsy values are n
, no
, and 0
.
Comparison is made in a case insensitive way.
The same options from prompt are available.
Examples
-
Ask to confirm something important:
const answer = await promptly.confirm('Are you really sure? ');
console.log('Answer:', answer);
.choose(message, choices, [options])
Ask the user to choose between multiple choices
(array of choices), printing the message
and waiting for the input.
Returns a promise that resolves with the choice.
The same options from prompt are available.
Examples
-
Ask to choose between:
const choice = await promptly.choose('Do you want an apple or an orange? ', ['apple', 'orange']);
console.log('Choice:', choice);
.password(message, [options])
Prompts for a password, printing the message
and waiting for the input.
Returns a promise that resolves with the password.
The same options from prompt are available, except that trim
and silent
default to false
and default
is an empty string to allow empty passwords.
Examples
-
Ask for a password:
const password = await promptly.password('Type a password: ');
console.log('Password:', password);
-
Ask for a password but mask the input with *
:
const password = await promptly.password('Type a password: ', { replace: '*' });
console.log('Password:', password);
License
Released under the MIT License.