Giter VIP home page Giter VIP logo

prompt-confirm's Introduction

prompt-confirm NPM version NPM monthly downloads NPM total downloads Linux Build Status Windows Build Status

Confirm (yes/no) prompt. Can be used standalone or with a prompt system like Enquirer.

Please consider following this project's author, Jon Schlinkert, and consider starring the project to show your ❤️ and support.

prompt-confirm example

Install

Install with npm:

$ npm install --save prompt-confirm

Usage

Question

Pass a string or question object to the constructor:

const Confirm = require('prompt-confirm');
const prompt = new Confirm('Do you like chocolate?');

// or
const prompt = new Confirm({
  name: 'chocolate', 
  message: 'Do you like chocolate?'
});

Run the prompt

You can use one of the following two methods for running the prompt:

// async
prompt.ask(function(answer) {
  console.log(answer);
});

// or promise
prompt.run()
  .then(function(answer) {
    console.log(answer);
  });

Examples

const confirm = new Confirm('Like chocolate?')
  .ask(function(answer) {
    console.log(answer);
  });

const confirm = new Confirm('Like chocolate?')
  .run()
  .then(function(answer) {
    console.log(answer);
  });

Usage with enquirer

const Enquirer = require('enquirer');
const enquirer = new Enquirer();

enquirer.register('confirm', require('prompt-confirm'));

Enquirer example

Enquirer supports either the declarative object-oriented (inquirer-style) question format or a more expressive format using the .question method.

Declarative

Inquirer-style declarative format (takes an array or object):

const questions = [
  {
    type: 'confirm',
    name: 'chocolate',
    message: 'Like chocolate?'
  },
  {
    type: 'confirm',
    name: 'vanilla',
    message: 'Like vanilla?'
  }
];

enquirer.ask(questions)
  .then(function(answers) {
    console.log(answers)
  });

Expressive

Pre-define questions and easily compose prompts by passing the name(s) of the prompts to run:

enquirer.question('chocolate', 'Like chocolate?', {type: 'confirm'});
enquirer.question('vanilla', 'Like vanilla?', {type: 'confirm'});

enquirer
  .prompt(['chocolate', 'vanilla'])
  .then(function(answers) {
    console.log(answers)
  });

About

Contributing

Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.

Running Tests

Running and reviewing unit tests is a great way to get familiarized with a library and its API. You can install dependencies and run tests with the following command:

$ npm install && npm test
Building docs

(This project's readme.md is generated by verb, please don't edit the readme directly. Any changes to the readme must be made in the .verb.md readme template.)

To generate the readme, run the following command:

$ npm install -g verbose/verb#dev verb-generate-readme && verb

Related projects

You might also be interested in these projects:

Contributors

Commits Contributor
39 jonschlinkert
9 doowb

Author

Jon Schlinkert

License

Copyright © 2018, Jon Schlinkert. Released under the MIT License.


This file was generated by verb-generate-readme, v0.6.0, on May 28, 2018.

prompt-confirm's People

Contributors

doowb avatar jonschlinkert avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

prompt-confirm's Issues

please could you put this issue in your documentation? I think could help other people

I am a junior programmer. Please, could you explain me why:

var questions = {
  type: 'confirm',
  name: 'myQuestion',
  message: 'Do you want to create a backup?'
};

enquirer.ask(questions)
  .then(function (answers) {
     console.log(answers);
  })

This does not work

if (answers.myQuestion) {
  console.log('true condition')
} else {
  console.log('false condition')
}

This work

if (!answers.myQuestion){
  console.log('false condition')
} else {
  console.log('true condition')
}

What the enquirer is doing with the answers variable? if you console.log(answer) you see true/false, but you cant manage that as a boolean.

Thank you in advance! :)


(edited on 10/20/2017 by @jonschlinkert to add backticks around code and fix code formatting for readability)

Answer is always rendered "Yes"

Code to reproduce:

new Confirm('Proceed?').run()

Output:

? Proceed? (Y/n) n
? Proceed? Yes

The promise still resolves to false; only the rendered output is incorrect.

Can't define default without name

new Prompt({ message:"Are you Sure?", default:false }).run()
TypeError: expected name to be a non-empty string
    at new Question (/project/node_modules/prompt-question/index.js:61:11)
    at Confirm.Prompt (/project/node_modules/prompt-base/index.js:50:16)
    at new Confirm (/project/node_modules/prompt-confirm/index.js:14:10)
    at promiseTry.then (/project/scripts/dropdb.js:27:13)
    at <anonymous>
    at process._tickCallback (internal/process/next_tick.js:169:7)
    at Function.Module.runMain (module.js:607:11)
    at startup (bootstrap_node.js:158:16)
    at bootstrap_node.js:575:3

I don't even know what name is for and I shouldn't have to define it.

Can't use prompt-confirm

Hi,
Sorry it's me again.
I don't know if this is linked or not (because I had not used prompt-confirm before), but since I pulled the latest version of enquirer, to solve enquirer/enquirer#6, I cannot use the prompt-confirm.

For information (maybe this is useful) I have git pulled the enquirer project, then I cded to my workspace and did npm install /path/to/enquirer/clone, it installed and I could use enquirer.
Now I am trying to use prompt-confirm and this fails, here is a minimal snippet that fails on me:

'use strict';

const Enquirer = require('enquirer')
const inspect = require ('util').inspect


function query (i) {
    let enquirer = new Enquirer ()

    enquirer.register ('confirm', require ('enquirer-prompt-confirm'))

    enquirer.question ('QuestionA', 'Question A (' + i + ')?', {
        type: 'confirm'
    })
    enquirer.question ('QuestionB', 'Question B (' + i + ')?', {
        type: 'confirme',
    })

    enquirer.ask()
    .then (answers => {
        console.log ('Answers #' + i)
        console.log (inspect (answers))
        console.log ('--')

        if (i > 0) {
            console.log ('Asking for argument ' + i + ' now...')
            query (i-1)
        }
    })
    .catch( err => {
        console.error ('Err: ' + err)
    })
}

query (3)

As you noticed, I have used the same template to chain prompt-confirm commands, but it fails at the first: it asks me the first question, but then when I answer it fails. Here is the console session:

? Question A (3)? Yesreadline.js:982
            throw err;
            ^

TypeError: this.ui.write is not a function
    at Confirm.onSubmit (/home/nschoe/workspace/xxx/node_modules/enquirer-prompt-confirm/index.js:71:11)
    at UI.on (/home/nschoe/workspace/xxx/node_modules/component-emitter/index.js:65:8)
    at UI.Emitter.emit (/home/nschoe/workspace/xxx/node_modules/component-emitter/index.js:133:20)
    at emitOne (events.js:96:13)
    at Interface.emit (events.js:188:7)
    at Interface._onLine (readline.js:239:10)
    at Interface._line (readline.js:585:8)
    at Interface._ttyWrite (readline.js:862:14)
    at ReadStream.onkeypress (readline.js:126:10)
    at emitTwo (events.js:111:20)
    at ReadStream.emit (events.js:191:7)
    at emitKeys (internal/readline.js:389:14)
    at next (native)
    at ReadStream.onData (readline.js:972:36)
    at emitOne (events.js:96:13)
    at ReadStream.emit (events.js:188:7)

This is weird, so might it be a problem induced by the fix on enquirer? Or is it a cache or dependency error on my side?

How to set default value to false?

Hi,

I'm trying to use prompt-confirm to ask the user a question, but the prompts defaults to (Y/n) and when simply pressing <Enter> the answer is set to true.

By using {default: false} (hinted by this) I managed to make the prompt defaults to (y/N), but even so, just pressing <Enter> without writing anything sets the answer to false.

Basic example below:

'use strict';

const inspect  = require ('util').inspect
const Enquirer = require ('enquirer')

let enquirer = new Enquirer ()
enquirer.register ('confirm', require ('prompt-confirm'))

enquirer.question ('agree', 'Do you agree?', {
    type: 'confirm',
    default: false
})

enquirer.ask()
.then( answer => {
    console.log ('Answer: ' + inspect (answer, {colors: true}))
})

And the console session that goes with it:

? Do you agree? (y/N) #Pressing <Enter>

? Do you agree? true
Answer: { agree: true }

(Note that I displayed the the question twice simply to show that the prompt indeeds switches to (y/N), but there are no display problem, it doesn't actually get repeated).

As you see, simply pressing enter sets the answer to false.

I tried setting defaultValue: false as well in the parameters, but it doesn't work.

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.