Skip to content
Myr

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

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by TJ Holowaychuk · committed by TJ Holowaychuk · parent 8e26188 · Browse files · 3 files+56 added−3 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+56 added−3 removed

  • other:+56 added−3 removed

3 files+56 added−3 removed

added:examples/bulk-input+16 added
1#!/usr/bin/env node23/**4 * Module dependencies.5 */67var program = require('../');89program.prompt({10 name: 'Username: ',11 desc: 'Description:',12 age: 'Age: '13}, function(obj){14 console.log(obj);15 process.stdin.destroy();16});
added:examples/help+17 added
1#!/usr/bin/env node23/**4 * Module dependencies.5 */67var program = require('../');89program10 .version('0.0.1')11 .option('-f, --foo', 'enable some foo')12 .option('-b, --bar', 'enable some bar')13 .option('-B, --baz', 'enable some baz')14 .parse(process.argv);1516if (!process.argv.length) program.help();17
modified:lib/commander.js+23 added-3 removed
796 unmodified lines
797 * program.prompt('Description:', function(desc){798 * console.log('description was "%s"', desc.trim());799 * });800 *801 * @param {String} str801 * @param {String|Object} str802 * @param {Function} fn803 * @api public804 */805806Command.prototype.prompt = function(str, fn){807 var self = this;808809 if ('string' == typeof str) {807 if (/ $/.test(str)) return this.promptSingleLine.apply(this, arguments);808 this.promptMultiLine(str, fn);810 if (/ $/.test(str)) return this.promptSingleLine.apply(this, arguments);811 this.promptMultiLine(str, fn);812 } else {813 var keys = Object.keys(str)814 , obj = {};815816 function next() {817 var key = keys.shift()818 , label = str[key];819820 if (!key) return fn(obj);821 self.prompt(label, function(val){822 obj[key] = val;823 next();824 });825 }826827 next();828 }829};830831/**832 * Prompt for password with `str`, `mask` char and callback `fn(val)`.
193 unmodified lines