What is promptly?
The 'promptly' npm package is a simple utility for prompting user input in Node.js applications. It provides a straightforward way to ask for user input from the command line, with support for validation, default values, and hidden input for sensitive information.
What are promptly's main functionalities?
Basic Prompt
This feature allows you to prompt the user for input and then use that input in your application. The example code asks the user for their name and then greets them.
const promptly = require('promptly');
(async () => {
const name = await promptly.prompt('Enter your name: ');
console.log(`Hello, ${name}!`);
})();
Prompt with Default Value
This feature allows you to provide a default value for the prompt. If the user does not enter anything, the default value will be used. The example code sets 'Anonymous' as the default name.
const promptly = require('promptly');
(async () => {
const name = await promptly.prompt('Enter your name: ', { default: 'Anonymous' });
console.log(`Hello, ${name}!`);
})();
Hidden Input
This feature allows you to prompt the user for input without displaying what they type on the screen. This is useful for sensitive information like passwords. The example code asks for a password and hides the input.
const promptly = require('promptly');
(async () => {
const password = await promptly.prompt('Enter your password: ', { silent: true });
console.log('Password received.');
})();
Input Validation
This feature allows you to validate the user's input before accepting it. The example code validates that the input is a positive integer representing the user's age.
const promptly = require('promptly');
(async () => {
const age = await promptly.prompt('Enter your age: ', {
validator: (value) => {
const age = parseInt(value, 10);
if (isNaN(age) || age <= 0) {
throw new Error('Invalid age');
}
return age;
}
});
console.log(`You are ${age} years old.`);
})();
Other packages similar to promptly
inquirer
Inquirer.js is a powerful and flexible library for creating interactive command-line interfaces. It supports a wide range of prompt types, including input, confirm, list, rawlist, expand, checkbox, password, and editor. Compared to promptly, Inquirer.js offers more advanced features and customization options.
readline-sync
Readline-sync is a synchronous library for reading user input from the command line. It provides basic functionality for prompting user input, similar to promptly, but operates synchronously, making it simpler to use in some cases. However, it lacks some of the advanced features and flexibility of promptly.
prompt
The 'prompt' package is another library for prompting user input in Node.js. It supports various input types, validation, and default values. While it offers similar functionality to promptly, it has a more complex API and additional features like schema-based prompts.
promptly
Simple command line prompting utilify.
API
In all the commands, the options argument is not mandatory.
.prompt(message, opts, fn)
Prompts for a value, printing the message
and waiting for the input.
When done, calls fn
with an error
and value
.
Default options:
{
'default': 'default value',
'trim': true,
'validator': null,
'retry': false
}
The validators have two purposes:
function (value) {
if (value.length !== 2) {
throw new Error('Length must be 2');
}
return value.replace('aa', 'bb');
}
Example usages:
Ask for a name.
promptly.prompt('name: ', function (err, value) {
if (err) {
console.log('invalid name');
return err.retry();
}
console.log(value);
});
Ask for a name until it validates (non-empty value).
promptly.prompt('name: ', { retry: true }, function (err, value) {
console.log(value);
});
Ask for a name until it validates (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: ', { retry: true, validator: validator }, function (err, value) {
console.log(value);
});
.confirm(message, opts, fn)
Ask the user to confirm something.
Calls fn
with an 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:
promply.confirm('Are you sure? ', function (err, value) {
console.log('Answer: ', value);
});
.choose(message, choices, opts, fn)
Ask the user to choose between multiple choices
(array of choices).
Calls fn
with an error
and value
(true or false).
The available options are the same, except that retry
defauls to true
.
promply.choose('Do you want an apple or an orange? ', ['apple', 'orange'], function (err, value) {
console.log('Answer: ', value);
});
.password(message, opts, fn)
Prompts for a password, printing the message
and waiting for the input.
When available, calls fn
with an error
and value
.
The available options are the same, except that trim
defauls to false
.
promply.password('password: ', function (err, value) {
console.log('password is ' + value);
});
License
Released under the MIT License.