1. yesno
A simple library for asking boolean questions in cli programs
yesno
Package: yesno
Created by: tcql
Last modified: Sat, 27 May 2023 02:27:09 GMT
Version: 0.4.0
License: BSD
Downloads: 558,806
Repository: https://github.com/tcql/node-yesno

Install

npm install yesno
yarn add yesno

Build Status

A nodejs library for issuing and handling responses to yes/no questions

Supports Node 8+.

Installation

 npm install yesno

Usage

 import yesno from 'yesno';        // modern es modules approach

// *OR*

const yesno = require('yesno');   // commonjs approach

Examples

basic
 const ok = await yesno({
    question: 'Are you sure you want to continue?'
});

yesno accepts yes, y , no, and n values by default.

All yesno responses are case insensitive.

Custom Yes/No values
 const ok = await yesno({
    question: 'Dude, Is this groovy or what?',
    yesValues: [ 'groovy' ],
    noValues: [ 'or what' ]
});

console.log(ok ? 'Tubular.' : 'Aw, why you gotta be like that?');

Now the question only responds to groovy as yes and or what as no.

No default value

Sometimes you may want to ensure the user didn't accidentally accept a default.
You can disable the default response by passing null as the defaultValue parameter.

 const ok = await yesno({
    question: 'Are you sure you want to 'rm-rf /' ?',
    defaultValue: null
});
Handling invalid responses

By default, if the user enters a value that isn't recognized as an acceptable response, it will
print out a message like:

Invalid response.
Answer either yes : (yes, y)
Or no : (no, n)

and re-ask the question. If you want to change this behavior, you can set the invalid handler before asking your question:

 const ok = await yesno({
    question: 'Ready to continue?',
    invalid: function ({ question, defaultValue, yesValues, noValues }) {
        process.stdout.write("\n Whoa. That was not a good answer. Well. No more tries for you.");
        process.exit(1);
    }
});

Dependencies

RELATED POST

10 Must-Know Windows Shortcuts That Will Save You Time

10 Must-Know Windows Shortcuts That Will Save You Time

Arrays vs Linked Lists: Which is Better for Memory Management in Data Structures?

Arrays vs Linked Lists: Which is Better for Memory Management in Data Structures?

Navigating AWS Networking: Essential Hacks for Smooth Operation

Navigating AWS Networking: Essential Hacks for Smooth Operation

Achieving Stunning Visuals with Unity's Global Illumination

Achieving Stunning Visuals with Unity's Global Illumination

Nim's Hidden Gems: Lesser-known Features for Writing Efficient Code

Nim's Hidden Gems: Lesser-known Features for Writing Efficient Code