Giter Site home page Giter Site logo

node-yesno's Introduction

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

node-yesno's People

Contributors

gnuletik avatar mreinstein avatar tcql avatar ypresto avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.