Ask for a name with a constraint (non-empty value and length > 2):
const promptly = require('promptly');const validator = function (value) { if (value.length < 2) { throw new Error(
Validators
The validators have two purposes: to check and transform input. They can be asynchronous or synchronous
const validator = (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');}const asyncValidator = async (value) => { await myfunc(); return value;}
.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 options are the same as prompt, except that trim defaults to false.
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 options are the same as prompt, except that trim defaults to false.
Examples
Ask to choose between:
const promptly = require('promptly');(async () => { 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 options are the same as prompt, except that trim and silent default to false and default is an empty string (to allow empty passwords).