Simple CLI prompt for Node

A simple commandline prompt for Node.js

While working on a Node.js automation software I needed to generate some files and code after collecting some information from the user. Most of the modules that I found were a bit of an over-kill, all I wanted was a good old prompt (the browser type) so I decided to write this module.

Installation

  1. npm install simple-prompt

How to use

Simple prompt accepts an array of question objects

  1. var prompt = require('simple-prompt');
  2. var questions = [
  3. {
  4. question: 'Name',
  5. required: true // required
  6. },
  7. {
  8. question: 'Surname' //optional
  9. },
  10. {
  11. question: 'Age',
  12. required: true, // required
  13. validate: function (answer) {
  14. return parseInt(answer, 10) >= 18; // only 18 or higher
  15. },
  16. filter: function (answer) {
  17. return parseInt(answer, 10); // bring back my answer as a number
  18. }
  19. }
  20. ];
  21. prompt(questions, function (answers) {
  22. console.log(answers);
  23. });

Question object

The question object has 4 properties that you can specify:

  • question (String)- label for your question and key for your answer.
  • required (Boolean) - flag to indicated if input is required.
  • validate (Function) - a function that accepts a string and returns a boolean value after testing it.
  • filter (Function) - a function that accepts a string and returns it after doing operations on it.

Testing

  1. npm test

FORK ON GITHUB

If you like my content, please consider buying me a coffee.

Buy Me A Coffee