Skip to content
This repository has been archived by the owner on Dec 7, 2017. It is now read-only.

Latest commit

 

History

History
184 lines (126 loc) · 3.51 KB

.verb.md

File metadata and controls

184 lines (126 loc) · 3.51 KB

Example

{%= example(read('example.js'), name) %}

Screen capture

screen shot 2015-07-13 at 8 05 20 am

See the working examples.

Basic Usage

See the working examples.

var questions = require('{%= name %}')();

// question type "input" is used by default
questions
  .set('name', 'What is your name?')
  .ask('name', function (err, answers) {
    console.log(answers);
  });

[inquirer2][]

You may optionally pass your own instance of inquirer to the constructor:

// on the options
var questions = require('{%= name %}');
var questions = new Questions({
  inquirer: require('inquirer2')
});

// or if inquirer is the only thing passed
var questions = new Questions(require('inquirer2'));

Getting started

question-cache is a wrapper around [inquirer2][]. If you have any issues related to the interface (like scrolling, colors, styling, etc), then please create an issue on the [inquirer2][] project.

Asking questions

The simplest way to ask a question is by passing a string and a callback:

questions.ask('name', function (err, answers) {
  console.log(answers);
});

Ask all cached questions

questions.ask(function (err, answers) {
  console.log(answers);
});

API

{%= apidocs("index.js") %}

Dot notation

See the working examples.

Qestions may be cached using object-path notatation (e.g. a.b.c).

Example

All of the following will be cached on the name object:

questions
  .set('name.first', 'What is your first name?')
  .set('name.middle', 'What is your middle name?')
  .set('name.last', 'What is your last name?')

Dot notation usage

When cached using dot-notation, there are a few different ways questions that may be asked.

Dot notation usage

ask one

Ask a single name question:

questions.ask('name.first', function (err, answers) {
  console.log(answers);
});

ask all "name" questions

Ask all name questions, first, middle and last:

questions.ask('name', function (err, answers) {
  console.log(answers);
});

array of questions

Ask specific questions on name:

questions.ask(['name.first', 'name.last'], function (err, answers) {
  console.log(answers);
});

ask all questions

Ask specific questions on name:

questions
  .set('name.first', {
    message: 'What is your first name?',
  })
  .set('name.last', {
    message: 'What is your last name?',
  })
  .set('foo', {
    message: 'Any thoughts about foo?',
  })

questions.ask(['name', 'foo'], function (err, answers) {
  console.log(answers);
});

nested questions

Ask one question at a time, based on feedback:

questions.ask('name.first', function (err, answers) {
  console.log(answers);
  //=> {name: { first: 'Brian' }}

  questions.ask('name.last', function (err, answers) {
    console.log(answers);
    //=> {name: { last: 'Woodward' }}
  });
});

More examples

Mixture of dot notation and non-dot-notation

Given you have the following questions:

questions
  .set('name.first', 'What is your first name?')
  .set('name.last', 'What is your last name?')
  .set('foo', 'Any thoughts about foo?')
  .set('bar', 'Any thoughts about bar?')

The following will ask questions: name.first, name.last and foo

questions.ask(['name', 'foo'], function (err, answers) {
  console.log(answers);
});