Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork2
This repository has been archived, use Enquirer instead.
License
enquirer/prompt-checkbox
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
Multiple-choice/checkbox prompt. Can be used standalone or with a prompt system likeEnquirer.
Install withnpm:
$ npm install --save prompt-checkbox
varPrompt=require('prompt-checkbox');varprompt=newPrompt({name:'colors',message:'What are your favorite colors?',choices:['red','blue','yellow']});// promisesprompt.run().then(function(answers){console.log(answers)}).catch(function(err){console.log(err)})// asyncprompt.ask(function(answers){console.log(answers)});
Features you won't find with other prompts!
Define choices as a function. This allows you to dynamically generate the choices when the question is asked.
varprompt=newPrompt({name:'colors',message:'What are your favorite colors?',choices:function(){// dynamically build choicesreturn['red','blue','green'];}});
Easy to configure!
Just pass an object of arrays onchoices, and each key in the object will be used as the group "toggle":
varPrompt=require('prompt-checkbox');varprompt=newPrompt({name:'install',message:'Which packages do you want to install?',choices:{dependencies:['generate','micromatch'],devDependencies:['mocha','kind-of']}});
Addsall andnone choices, which select or deselect all choices, respectively. Named "radio choices" since it acts like a hybrid between checkboxes and radio buttons.
Code example
varPrompt=require('prompt-checkbox');varprompt=newPrompt({name:'install',message:'Which packages do you want to install?',radio:true,choices:['foo','bar','baz']});
Use "radio" choices with choice groups.
varPrompt=require('prompt-checkbox');varprompt=newPrompt({name:'install',message:'Which packages do you want to install?',radio:true,choices:{dependencies:['generate','micromatch'],devDependencies:['mocha','kind-of']}});
The following options are either specific to prompt-checkbox, or have behavior that differs in some way from the built-in options fromprompt-base.(Any other options fromprompt-base may be used as well.)
Type:string|number|array
Default:undefined
Specify the "default" choices to check when the prompt is initialized. Default can be a choice name (string), index (number), or an array of choice names or indices.
Examples
Specify default as a string (choice name):
varprompt=newPrompt({name:'colors',message:'Best flavor?',default:'chocolate',choices:['chocolate']// <= hmm, I wonder what they'll choose?});
Specify an array of defaults (choice names or indices):
varprompt=newPrompt({name:'colors',message:'Favorite colors?',default:[1,'blue'],choices:['red','blue','yellow']});
Type:boolean
Default:undefined
Enable hybrid radio-checkbox support, which addsall andnone radio options for toggling all options on and off.
varPrompt=require('prompt-checkbox');varprompt=newPrompt({name:'colors',message:'What are your favorite colors?',radio:true,choices:['red','blue','yellow']});
Type:function
Default:undefined
Modify answer values before they're returned.
Example
Useoptions.transform and theprompt.choices.get() method to convert answers (checked choices) to an array of objects (versus of an array of strings).
varPrompt=require('prompt-checkbox');varprompt=newPrompt({name:'colors',message:'What are your favorite colors?',choices:['red','blue','yellow'],transform:function(answer){// - "this" is the prompt instance// - "this.choices.get()" returns the choice object for each choicereturnanswer ?answer.map(this.choices.get.bind(this.choices)) :[];}});
In addition to the keypresses that are supported byprompt-base, the following keypress offer different behavior that is specific to checklists:
- down - move the pointer (cursor) down one row for each keypress
- up - move the pointer (cursor) up one row for each keypress
- i - toggle all choices to the opposite of their current state.
- a - enable or disable all choices
- space - toggle a choice
- number - toggle the choice at the given index (starting at 1)
Usage withenquirer
Register the prompt with enquirer:
varEnquirer=require('enquirer');varenquirer=newEnquirer();enquirer.register('checkbox',require('prompt-checkbox'));
For formatting questions,enquirer supports either:
- declarative, inquirer-style question format
- functional format using the
.questionmethod.
Inquirer-style questions
Declarative questions format, similar toinquirer.
varquestions=[{name:'color',message:'What is your favorite color?',type:'checkbox',default:'blue',choices:['red','yellow','blue']}];enquirer.prompt(questions).then(function(answers){console.log(answers)});
Or:
enquirer.prompt({name:'color',message:'What is your favorite color?',type:'checkbox',default:'blue',choices:['red','yellow','blue']}).then(function(answers){console.log(answers)});
Functional-style questions
Use the.question method to pre-register questions, so they can be called later. Also, themessage may be passed as the second argument, or as a property on the question options.
enquirer.question('letter','What are your favorite letters?',{type:'checkbox',//<= specify the prompt typechoices:['a','b','c']});enquirer.question('numbers',{type:'checkbox',//<= specify the prompt typemessage:'What are your favorite numbers?',choices:['1','2','3']});// pass the name(s) or questions to askenquirer.prompt(['letters','numbers']).then(function(answers){console.log(answers)});
- enquirer: Intuitive, plugin-based prompt system for node.js. |homepage
- prompt-base: Base prompt module used for creating custom prompts. |homepage
- prompt-choices: Create an array of multiple choice objects for use in prompts. |homepage
- prompt-question: Question object, used by Enquirer and prompt plugins. |homepage
Pull requests and stars are always welcome. For bugs and feature requests,please create an issue.
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&& npmtest
Jon Schlinkert
Copyright © 2017,Jon Schlinkert.Released under theMIT License.
This file was generated byverb-generate-readme, v0.6.0, on July 08, 2017.
About
This repository has been archived, use Enquirer instead.
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Sponsor this project
Uh oh!
There was an error while loading.Please reload this page.
Packages0
Uh oh!
There was an error while loading.Please reload this page.



