Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add string and object support to question

  • Loading branch information...
commit acfbcd4a21244da5fc6d91b4d140f8d5e9585895 1 parent 2a9ff60
Worms David authored
Showing with 51 additions and 12 deletions.
  1. +25 −0 bin/dev
  2. +23 −11 lib/Shell.coffee
  3. +3 −1 package.json
25 bin/dev
View
@@ -36,4 +36,29 @@ app.cmd 'test long', (req, res) ->
message.push ' This was long'
res.cyan i + message.join(' ') + '\n'
res.prompt()
+
+app.cmd 'test question string', (req, res) ->
+ req.question 'question_1', (values) ->
+ console.log values
+ res.prompt()
+
+app.cmd 'test question array', (req, res) ->
+ req.question [
+ name: 'question_1'
+ value: 'default 1'
+ ,
+ name: 'question_2'
+ value: 'default 2'
+ ], (values) ->
+ console.log values
+ res.prompt()
+
+app.cmd 'test question object', (req, res) ->
+ req.question
+ 'question_1': 'default 1'
+ 'question_2':
+ value: 'default 2'
+ , (values) ->
+ console.log values
+ res.prompt()
34 lib/Shell.coffee
View
@@ -5,6 +5,7 @@ path = require 'path'
readline = require 'readline'
events = require 'events'
EventEmitter = events.EventEmitter
+each = require 'each'
styles = require './Styles'
Request = require './Request'
Response = require './Response'
@@ -137,17 +138,28 @@ module.exports = class Shell extends EventEmitter
# Ask one or more questions
question: (questions, callback) ->
- isArray = Array.isArray questions
- answers = []
- index = 0
- next = =>
- question = questions[index]
- @interface().question "#{question.name} [#{question.value}]", (answer) ->
- answers[question.name] = if answer is '' then question.value else answer
- unless ++index is questions.length
- then next index
- else callback if isArray then answers else answers[questions[0].name]
- next()
+ isObject = questions? and typeof questions is 'object'
+ answers = {}
+ if isObject
+ answers = []
+ each questions, (question, settings, next) =>
+ unless next
+ return callback answers
+ settings = {value: settings} unless settings and typeof settings is 'object'
+ @interface().question "#{question} [#{settings.value}]", (answer) ->
+ answers[question] = if answer is '' then settings.value else answer
+ next()
+ else
+ isArray = Array.isArray questions
+ questions = [name: questions, value: ''] if typeof questions is 'string'
+ each questions, (question, next) =>
+ if next is null
+ answers = answers[questions[0].name] unless isArray
+ return callback answers
+ @interface().question "#{question.name} [#{question.value}]", (answer) ->
+ answers[question.name] = if answer is '' then question.value else answer
+ next()
+
# Ask a question with a boolean answer
confirm: (msg, defaultTrue, callback) ->
4 package.json
View
@@ -11,7 +11,9 @@
"node": ">= 0.1.90"
},
"dependencies": {
- "coffee-script": "latest"
+ "coffee-script": "latest",
+ "each": "latest",
+ "coffeekup": "latest"
},
"keywords": ["console", "colors", "xterm"],
"repository": {
Please sign in to comment.
Something went wrong with that request. Please try again.