Socket
Socket
Sign inDemoInstall

prompt

Package Overview
Dependencies
0
Maintainers
0
Versions
38
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

prompt


Version published
Maintainers
0
Install size
11.2 kB
Created

Package description

What is prompt?

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.

What are prompt's main functionalities?

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();

Other packages similar to prompt

Readme

Source

PROMPT

What:

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.

Install:

The easiest way is to use npm:

npm install prompt

To those of you that don't, this module is simple enough that hopefully the process is pretty clear.

Usage & Examples:

While this module was originally inspired by BASIC-style prompts, the current api is quite a bit different, but much more useful in an asynchronous context.

Prompt() is an object with the methods .ask, .tap and .end. Chain these methods together to ask questions and do things to them, then call .end() to cap the chain.

It's probably easiest to follow from 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, off you go!');
    })
    .end();

Simple examples may be found in the test/ folder (which are also tests for node-prompt). More in-depth examples may be found in the examples/ folder, as they are written (there's only one right now).

Asking for passwords and stuff:

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. ;)

Bone-Picking:

Make a github issue and/or pull, write something (even a test that breaks Prompt) and request that I pull.

To-Do:

Prompt could use some TLC:

  1. There's no protection (yet) for two instances of Prompt trying to use openStdin at the same time. Similar problems occur between Prompt and the REPL.

  2. I would like to rewrite prompt to use chainsaw, in order to clean up some of the boilerplate. this might also make deprecating .end() easy.

Authors and Contributors:

  • Joshua Holbrook ( http://jesusabdullah.github.com/ )
  • Peteris Krumins ( http://catonmat.net )
  • Lele "El Dios" ( http://lele.amicofigo.com/ )

Licensing:

The MIT License

Copyright (c) 2011 Joshua Holbrook

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

FAQs

Last updated on 12 May 2011

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc