Skip to content
Myr

Added default support to .choose(). Closes #41

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 25705d6 · Browse files · 2 files+22 added−4 removed
This commit

State

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

Diff stats

2 files+22 added−4 removed

  • other:+22 added−4 removed

2 files+22 added−4 removed

modified:examples/choice+6 added-1 removed
9 unmodified lines
1011console.log('Choose the coolest pet:');12program.choose(list, function(i){13 console.log('you chose %d "%s"', i, list[i]);14 console.log();15 console.log('Choose the coolest pet, defaulting to loki:');16 program.choose(list, 2, function(i){17 console.log('you chose %d "%s"', i, list[i]);14 process.stdin.destroy();18 process.stdin.destroy();19 });20});
modified:lib/commander.js+16 added-3 removed
905 unmodified lines
906 * process.stdin.destroy();907 * });908 *909 * @param {Array} list910 * @param {Number|Function} index or fn911 * @param {Function} fn912 * @api public913 */914914Command.prototype.choose = function(list, fn){915 var self = this;915Command.prototype.choose = function(list, index, fn){916 var self = this917 , hasDefault = 'number' == typeof index;918919 if (!hasDefault) {920 fn = index;921 index = null;922 }923924 list.forEach(function(item, i){925 if (hasDefault && i == index) {926 console.log('* %d) %s', i + 1, item);927 } else {918 console.log(' %d) %s', i + 1, item);928 console.log(' %d) %s', i + 1, item);929 }930 });931932 function again() {933 self.prompt(' : ', function(val){934 val = parseInt(val, 10) - 1;935 if (hasDefault && isNaN(val)) val = index;936937 if (null == list[val]) {938 again();939 } else {940 fn(val, list[val]);
65 unmodified lines