Skip to content
Myr

remove input methods (.prompt, .confirm, etc)

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

they belong in a different library

authored by TJ Holowaychuk · committed by TJ Holowaychuk · parent bf22882 · Browse files · 8 files+7 added−492 removed
This commit

State

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

Diff stats

8 files+7 added−492 removed

  • other:+7 added−492 removed

8 files+7 added−492 removed

deleted:examples/bulk-input-16 removed
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});
deleted:examples/choice-20 removed
1#!/usr/bin/env node23/**4 * Module dependencies.5 */67var program = require('../');89var list = ['tobi', 'loki', 'jane', 'manny', 'luna'];1011console.log('Choose the coolest pet:');12program.choose(list, function(i){13 console.log('you chose %d "%s"', i+1, list[i]);14 console.log();15 console.log('Choose the coolest pet, defaulting to loki:');16 program.choose(list, 1, function(i){17 console.log('you chose %d "%s"', i+1, list[i]);18 process.stdin.destroy();19 });20});
deleted:examples/confirm-12 removed
1#!/usr/bin/env node23/**4 * Module dependencies.5 */67var program = require('../');89program.confirm('continue? ', function(ok){10 console.log(' got %j', ok);11 process.stdin.destroy();12});
deleted:examples/input-24 removed
1#!/usr/bin/env node23/**4 * Module dependencies.5 */67var program = require('../');89program.prompt('Username: ', function(name){10 console.log('hi %s\n', name);1112 program.prompt('Description:', function(desc){13 console.log('description was "%s"', desc.trim());1415 program.prompt('Age: ', Number, function(age){16 console.log('age: %j\n', age);1718 program.prompt('Birthdate: ', Date, function(date){19 console.log('date: %s\n', date);20 process.stdin.destroy();21 });22 });23 });24});
deleted:examples/password-18 removed
1#!/usr/bin/env node23/**4 * Module dependencies.5 */67var program = require('../');89program.password('Password: ', function(pass){10 console.log('got "%s"', pass);11 program.password('Password: ', '*', function(pass){12 console.log('got "%s"', pass);13 program.password('Password: ', '-', function(pass){14 console.log('got "%s"', pass);15 process.stdin.destroy();16 });17 });18});
modified:index.js+7 added-320 removed
1/*!2 * commander3 * Copyright(c) 2011 TJ Holowaychuk <tj@vision-media.ca>4 * MIT Licensed5 */12/**3 * Module dependencies.4 */511var EventEmitter = require('events').EventEmitter12 , spawn = require('child_process').spawn13 , keypress = require('keypress')14 , fs = require('fs')15 , exists = fs.existsSync16 , path = require('path')17 , tty = require('tty')18 , dirname = path.dirname19 , basename = path.basename;6var EventEmitter = require('events').EventEmitter;7var spawn = require('child_process').spawn;8var fs = require('fs');9var exists = fs.existsSync;10var path = require('path');11var dirname = path.dirname;12var basename = path.basename;1314/**15 * Expose the root command.16 */
761 unmodified lines
778 ].join('\n');779};780781/**789 * Prompt for a `Number`.790 *791 * @param {String} str792 * @param {Function} fn793 * @api private794 */795796Command.prototype.promptForNumber = function(str, fn){797 var self = this;798 this.promptSingleLine(str, function parseNumber(val){799 val = Number(val);800 if (isNaN(val)) return self.promptSingleLine(str + '(must be a number) ', parseNumber);801 fn(val);802 });803};804805/**806 * Prompt for a `Date`.807 *808 * @param {String} str809 * @param {Function} fn810 * @api private811 */812813Command.prototype.promptForDate = function(str, fn){814 var self = this;815 this.promptSingleLine(str, function parseDate(val){816 val = new Date(val);817 if (isNaN(val.getTime())) return self.promptSingleLine(str + '(must be a date) ', parseDate);818 fn(val);819 });820};821822823/**824 * Prompt for a `Regular Expression`.825 *826 * @param {String} str827 * @param {Object} pattern regular expression object to test828 * @param {Function} fn829 * @api private830 */831832Command.prototype.promptForRegexp = function(str, pattern, fn){833 var self = this;834 this.promptSingleLine(str, function parseRegexp(val){835 if(!pattern.test(val)) return self.promptSingleLine(str + '(regular expression mismatch) ', parseRegexp);836 fn(val);837 });838};839840841/**842 * Single-line prompt.843 *844 * @param {String} str845 * @param {Function} fn846 * @api private847 */848849Command.prototype.promptSingleLine = function(str, fn){850 // determine if the 2nd argument is a regular expression851 if (arguments[1].global !== undefined && arguments[1].multiline !== undefined) {852 return this.promptForRegexp(str, arguments[1], arguments[2]);853 } else if ('function' == typeof arguments[2]) {854 return this['promptFor' + (fn.name || fn)](str, arguments[2]);855 }856857 process.stdout.write(str);858 process.stdin.setEncoding('utf8');859 process.stdin.once('data', function(val){860 fn(val.trim());861 }).resume();862};863864/**865 * Multi-line prompt.866 *867 * @param {String} str868 * @param {Function} fn869 * @api private870 */871872Command.prototype.promptMultiLine = function(str, fn){873 var buf = [];874 console.log(str);875 process.stdin.setEncoding('utf8');876 process.stdin.on('data', function(val){877 if ('\n' == val || '\r\n' == val) {878 process.stdin.removeAllListeners('data');879 fn(buf.join('\n'));880 } else {881 buf.push(val.trimRight());882 }883 }).resume();884};885886/**887 * Prompt `str` and callback `fn(val)`888 *889 * Commander supports single-line and multi-line prompts.890 * To issue a single-line prompt simply add white-space891 * to the end of `str`, something like "name: ", whereas892 * for a multi-line prompt omit this "description:".893 *894 *895 * Examples:896 *897 * program.prompt('Username: ', function(name){898 * console.log('hi %s', name);899 * });900 * 901 * program.prompt('Description:', function(desc){902 * console.log('description was "%s"', desc.trim());903 * });904 *905 * @param {String|Object} str906 * @param {Function} fn907 * @api public908 */909910Command.prototype.prompt = function(str, fn){911 var self = this;912 if ('string' == typeof str) {913 if (/ $/.test(str)) return this.promptSingleLine.apply(this, arguments);914 this.promptMultiLine(str, fn);915 } else {916 var keys = Object.keys(str)917 , obj = {};918919 function next() {920 var key = keys.shift()921 , label = str[key];922923 if (!key) return fn(obj);924 self.prompt(label, function(val){925 obj[key] = val;926 next();927 });928 }929930 next();931 }932};933934/**935 * Prompt for password with `str`, `mask` char and callback `fn(val)`.936 *937 * The mask string defaults to '', aka no output is938 * written while typing, you may want to use "*" etc.939 *940 * Examples:941 *942 * program.password('Password: ', function(pass){943 * console.log('got "%s"', pass);944 * process.stdin.destroy();945 * });946 *947 * program.password('Password: ', '*', function(pass){948 * console.log('got "%s"', pass);949 * process.stdin.destroy();950 * });951 *952 * @param {String} str953 * @param {String} mask954 * @param {Function} fn955 * @api public956 */957958Command.prototype.password = function(str, mask, fn){959 var self = this960 , buf = '';961962 // default mask963 if ('function' == typeof mask) {964 fn = mask;965 mask = '';966 }967968 keypress(process.stdin);969970 function setRawMode(mode) {971 if (process.stdin.setRawMode) {972 process.stdin.setRawMode(mode);973 } else {974 tty.setRawMode(mode);975 }976 };977 setRawMode(true);978 process.stdout.write(str);979980 // keypress981 process.stdin.on('keypress', function(c, key){982 if (key && 'enter' == key.name) {983 console.log();984 process.stdin.pause();985 process.stdin.removeAllListeners('keypress');986 setRawMode(false);987 if (!buf.trim().length) return self.password(str, mask, fn);988 fn(buf);989 return;990 }991992 if (key && key.ctrl && 'c' == key.name) {993 console.log('%s', buf);994 process.exit();995 }996997 process.stdout.write(mask);998 buf += c;999 }).resume();1000};10011002/**1003 * Confirmation prompt with `str` and callback `fn(bool)`1004 *1005 * Examples:1006 *1007 * program.confirm('continue? ', function(ok){1008 * console.log(' got %j', ok);1009 * process.stdin.destroy();1010 * });1011 *1012 * @param {String} str1013 * @param {Function} fn1014 * @api public1015 */101610171018Command.prototype.confirm = function(str, fn, verbose){1019 var self = this;1020 this.prompt(str, function(ok){1021 if (!ok.trim()) {1022 if (!verbose) str += '(yes or no) ';1023 return self.confirm(str, fn, true);1024 }1025 fn(parseBool(ok));1026 });1027};10281029/**1030 * Choice prompt with `list` of items and callback `fn(index, item)`1031 *1032 * Examples:1033 *1034 * var list = ['tobi', 'loki', 'jane', 'manny', 'luna'];1035 * 1036 * console.log('Choose the coolest pet:');1037 * program.choose(list, function(i){1038 * console.log('you chose %d "%s"', i, list[i]);1039 * process.stdin.destroy();1040 * });1041 *1042 * @param {Array} list1043 * @param {Number|Function} index or fn1044 * @param {Function} fn1045 * @api public1046 */10471048Command.prototype.choose = function(list, index, fn){1049 var self = this1050 , hasDefault = 'number' == typeof index;10511052 if (!hasDefault) {1053 fn = index;1054 index = null;1055 }10561057 list.forEach(function(item, i){1058 if (hasDefault && i == index) {1059 console.log('* %d) %s', i + 1, item);1060 } else {1061 console.log(' %d) %s', i + 1, item);1062 }1063 });10641065 function again() {1066 self.prompt(' : ', function(val){1067 val = parseInt(val, 10) - 1;1068 if (hasDefault && isNaN(val)) val = index;10691070 if (null == list[val]) {1071 again();1072 } else {1073 fn(val, list[val]);1074 }1075 });1076 }10771078 again();1079};108010811082/**782 * Output help information for this command783 *784 * @api public785 */
28 unmodified lines
814 });815}816817/**1119 * Parse a boolean `str`.1120 *1121 * @param {String} str1122 * @return {Boolean}1123 * @api private1124 */11251126function parseBool(str) {1127 return /^y|yes|ok|true$/i.test(str);1128}11291130/**818 * Pad `str` to `width`.819 *820 * @param {String} str821 * @param {Number} width
26 unmodified lines
modified:package.json-1 removed
modified:Readme.md-81 removed
151 unmodified lines
152 $ custom-help -h153154```155156## .prompt(msg, fn)157158 Single-line prompt:159160```js161program.prompt('name: ', function(name){162 console.log('hi %s', name);163});164```165166 Multi-line prompt:167168```js169program.prompt('description:', function(name){170 console.log('hi %s', name);171});172```173174 Coercion:175176```js177program.prompt('Age: ', Number, function(age){178 console.log('age: %j', age);179});180```181182```js183program.prompt('Birthdate: ', Date, function(date){184 console.log('date: %s', date);185});186```187188```js189program.prompt('Email: ', /^.+@.+\..+$/, function(email){190 console.log('email: %j', email);191});192```193194## .password(msg[, mask], fn)195196Prompt for password without echoing:197198```js199program.password('Password: ', function(pass){200 console.log('got "%s"', pass);201 process.stdin.destroy();202});203```204205Prompt for password with mask char "*":206207```js208program.password('Password: ', '*', function(pass){209 console.log('got "%s"', pass);210 process.stdin.destroy();211});212```213214## .confirm(msg, fn)215216 Confirm with the given `msg`:217218```js219program.confirm('continue? ', function(ok){220 console.log(' got %j', ok);221});222```223224## .choose(list, fn)225226 Let the user choose from a `list`:227228```js229var list = ['tobi', 'loki', 'jane', 'manny', 'luna'];230231console.log('Choose the coolest pet:');232program.choose(list, function(i){233 console.log('you chose %d "%s"', i, list[i]);234});235```236156## .outputHelp()157158 Output help information without exiting.159
36 unmodified lines