Permalink
Browse files

Added quick `.prompt(obj, callback)` support. Closes #49

  • Loading branch information...
1 parent 8e26188 commit f2060ffcfc7e69765aedb369824ee122480679a7 @tj committed Apr 11, 2012
Showing with 56 additions and 3 deletions.
  1. +16 −0 examples/bulk-input
  2. +17 −0 examples/help
  3. +23 −3 lib/commander.js
View
@@ -0,0 +1,16 @@
+#!/usr/bin/env node
+
+/**
+ * Module dependencies.
+ */
+
+var program = require('../');
+
+program.prompt({
+ name: 'Username: ',
+ desc: 'Description:',
+ age: 'Age: '
+}, function(obj){
+ console.log(obj);
+ process.stdin.destroy();
+});
View
@@ -0,0 +1,17 @@
+#!/usr/bin/env node
+
+/**
+ * Module dependencies.
+ */
+
+var program = require('../');
+
+program
+ .version('0.0.1')
+ .option('-f, --foo', 'enable some foo')
+ .option('-b, --bar', 'enable some bar')
+ .option('-B, --baz', 'enable some baz')
+ .parse(process.argv);
+
+if (!process.argv.length) program.help();
+
View
@@ -798,14 +798,34 @@ Command.prototype.promptMultiLine = function(str, fn){
* console.log('description was "%s"', desc.trim());
* });
*
- * @param {String} str
+ * @param {String|Object} str
* @param {Function} fn
* @api public
*/
Command.prototype.prompt = function(str, fn){
- if (/ $/.test(str)) return this.promptSingleLine.apply(this, arguments);
- this.promptMultiLine(str, fn);
+ var self = this;
+
+ if ('string' == typeof str) {
+ if (/ $/.test(str)) return this.promptSingleLine.apply(this, arguments);
+ this.promptMultiLine(str, fn);
+ } else {
+ var keys = Object.keys(str)
+ , obj = {};
+
+ function next() {
+ var key = keys.shift()
+ , label = str[key];
+
+ if (!key) return fn(obj);
+ self.prompt(label, function(val){
+ obj[key] = val;
+ next();
+ });
+ }
+
+ next();
+ }
};
/**

0 comments on commit f2060ff

Please sign in to comment.