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
Provenance
- Commit
bf5e1f5- Tree
70fd48e- Author
TJ Holowaychuk <tj@vision-media.ca>- Committer
TJ Holowaychuk <tj@vision-media.ca>- Parent
bf22882
8 files changed+7 added−492 removed
deleted:examples/bulk-input-16 removed
1-#!/usr/bin/env node2-3-/**4- * Module dependencies.5- */6-7-var program = require('../');8-9-program.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 node2-3-/**4- * Module dependencies.5- */6-7-var program = require('../');8-9-var list = ['tobi', 'loki', 'jane', 'manny', 'luna'];10-11-console.log('Choose the coolest pet:');12-program.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 node2-3-/**4- * Module dependencies.5- */6-7-var program = require('../');8-9-program.confirm('continue? ', function(ok){10- console.log(' got %j', ok);11- process.stdin.destroy();12-});deleted:examples/input-24 removed
1-#!/usr/bin/env node2-3-/**4- * Module dependencies.5- */6-7-var program = require('../');8-9-program.prompt('Username: ', function(name){10- console.log('hi %s\n', name);11-12- program.prompt('Description:', function(desc){13- console.log('description was "%s"', desc.trim());14-15- program.prompt('Age: ', Number, function(age){16- console.log('age: %j\n', age);17-18- 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 node2-3-/**4- * Module dependencies.5- */6-7-var program = require('../');8-9-program.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- */1 2 /**3 * Module dependencies.4 */5 11-var 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;6+var EventEmitter = require('events').EventEmitter;7+var spawn = require('child_process').spawn;8+var fs = require('fs');9+var exists = fs.existsSync;10+var path = require('path');11+var dirname = path.dirname;12+var basename = path.basename;13 14 /**15 * Expose the root command.16 */761 unmodified lines778 ].join('\n');779 };780 781 /**789- * Prompt for a `Number`.790- *791- * @param {String} str792- * @param {Function} fn793- * @api private794- */795-796-Command.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-};804-805-/**806- * Prompt for a `Date`.807- *808- * @param {String} str809- * @param {Function} fn810- * @api private811- */812-813-Command.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-};821-822-823-/**824- * Prompt for a `Regular Expression`.825- *826- * @param {String} str827- * @param {Object} pattern regular expression object to test828- * @param {Function} fn829- * @api private830- */831-832-Command.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-};839-840-841-/**842- * Single-line prompt.843- *844- * @param {String} str845- * @param {Function} fn846- * @api private847- */848-849-Command.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- }856-857- process.stdout.write(str);858- process.stdin.setEncoding('utf8');859- process.stdin.once('data', function(val){860- fn(val.trim());861- }).resume();862-};863-864-/**865- * Multi-line prompt.866- *867- * @param {String} str868- * @param {Function} fn869- * @api private870- */871-872-Command.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-};885-886-/**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- */909-910-Command.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 = {};918-919- function next() {920- var key = keys.shift()921- , label = str[key];922-923- if (!key) return fn(obj);924- self.prompt(label, function(val){925- obj[key] = val;926- next();927- });928- }929-930- next();931- }932-};933-934-/**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- */957-958-Command.prototype.password = function(str, mask, fn){959- var self = this960- , buf = '';961-962- // default mask963- if ('function' == typeof mask) {964- fn = mask;965- mask = '';966- }967-968- keypress(process.stdin);969-970- 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);979-980- // 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- }991-992- if (key && key.ctrl && 'c' == key.name) {993- console.log('%s', buf);994- process.exit();995- }996-997- process.stdout.write(mask);998- buf += c;999- }).resume();1000-};1001-1002-/**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- */1016-1017-1018-Command.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-};1028-1029-/**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- */1047-1048-Command.prototype.choose = function(list, index, fn){1049- var self = this1050- , hasDefault = 'number' == typeof index;1051-1052- if (!hasDefault) {1053- fn = index;1054- index = null;1055- }1056-1057- 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- });1064-1065- function again() {1066- self.prompt(' : ', function(val){1067- val = parseInt(val, 10) - 1;1068- if (hasDefault && isNaN(val)) val = index;1069-1070- if (null == list[val]) {1071- again();1072- } else {1073- fn(val, list[val]);1074- }1075- });1076- }1077-1078- again();1079-};1080-1081-1082-/**782 * Output help information for this command783 *784 * @api public785 */28 unmodified lines814 });815 }816 817 /**1119- * Parse a boolean `str`.1120- *1121- * @param {String} str1122- * @return {Boolean}1123- * @api private1124- */1125-1126-function parseBool(str) {1127- return /^y|yes|ok|true$/i.test(str);1128-}1129-1130-/**818 * Pad `str` to `width`.819 *820 * @param {String} str821 * @param {Number} width26 unmodified linesmodified:package.json-1 removed
3 unmodified lines45678891011123 unmodified lines , "description": "the complete solution for node.js command-line programs" , "keywords": ["command", "option", "parser", "prompt", "stdin"] , "author": "TJ Holowaychuk <tj@vision-media.ca>" , "repository": { "type": "git", "url": "https://github.com/visionmedia/commander.js.git" } , "dependencies": { "keypress": "0.1.x"} , "devDependencies": { "should": ">= 0.0.1" } , "scripts": { "test": "make test" } , "main": "index" , "engines": { "node": ">= 0.6.x" }}modified:Readme.md-81 removed
151 unmodified lines152 $ custom-help -h153 154 ```155 156-## .prompt(msg, fn)157-158- Single-line prompt:159-160-```js161-program.prompt('name: ', function(name){162- console.log('hi %s', name);163-});164-```165-166- Multi-line prompt:167-168-```js169-program.prompt('description:', function(name){170- console.log('hi %s', name);171-});172-```173-174- Coercion:175-176-```js177-program.prompt('Age: ', Number, function(age){178- console.log('age: %j', age);179-});180-```181-182-```js183-program.prompt('Birthdate: ', Date, function(date){184- console.log('date: %s', date);185-});186-```187-188-```js189-program.prompt('Email: ', /^.+@.+\..+$/, function(email){190- console.log('email: %j', email);191-});192-```193-194-## .password(msg[, mask], fn)195-196-Prompt for password without echoing:197-198-```js199-program.password('Password: ', function(pass){200- console.log('got "%s"', pass);201- process.stdin.destroy();202-});203-```204-205-Prompt for password with mask char "*":206-207-```js208-program.password('Password: ', '*', function(pass){209- console.log('got "%s"', pass);210- process.stdin.destroy();211-});212-```213-214-## .confirm(msg, fn)215-216- Confirm with the given `msg`:217-218-```js219-program.confirm('continue? ', function(ok){220- console.log(' got %j', ok);221-});222-```223-224-## .choose(list, fn)225-226- Let the user choose from a `list`:227-228-```js229-var list = ['tobi', 'loki', 'jane', 'manny', 'luna'];230-231-console.log('Choose the coolest pet:');232-program.choose(list, function(i){233- console.log('you chose %d "%s"', i, list[i]);234-});235-```236-156 ## .outputHelp()157 158 Output help information without exiting.159 36 unmodified lines