Skip to content

Commit

Permalink
Removing stuff we no longer use
Browse files Browse the repository at this point in the history
  • Loading branch information
dsmith committed Oct 6, 2011
1 parent 660efec commit 7600910
Showing 1 changed file with 4 additions and 194 deletions.
198 changes: 4 additions & 194 deletions lib/commander.js
Expand Up @@ -12,7 +12,6 @@ var EventEmitter = require('events').EventEmitter
, path = require('path')
, tty = require('tty')
, basename = path.basename
, util = require('util');

/**
* Expose the root command.
Expand Down Expand Up @@ -463,10 +462,7 @@ Command.prototype.parseOptions = function(argv){
*/

Command.prototype.missingArgument = function(name){
this.error();
this.error(util.format(" error: missing required argument `%s'", name));
this.error();
this.exit(1)
this.emit('missing', name)
};

/**
Expand All @@ -478,14 +474,11 @@ Command.prototype.missingArgument = function(name){
*/

Command.prototype.optionMissingArgument = function(option, flag){
this.error();
if (flag) {
this.error(util.format(" error: option `%s' argument missing, got `%s'", option.flags, flag));
this.emit('missingOption', option.flags, flag)
} else {
this.error(util.format(" error: option `%s' argument missing", option.flags));
this.emit('missingOption', option.flags)
}
this.error();
this.exit(1);
};

/**
Expand All @@ -496,10 +489,7 @@ Command.prototype.optionMissingArgument = function(option, flag){
*/

Command.prototype.unknownOption = function(flag){
this.error();
this.error(util.format(" error: unknown option `%s'", flag));
this.error();
this.exit(1);
this.emit('unknownOption', flag)
};

/**
Expand All @@ -519,7 +509,6 @@ Command.prototype.version = function(str){
this.option('-v, --version', 'output the version number');
this.on('version', function(){
this.log(str);
this.exit(0);
});
return this;
};
Expand Down Expand Up @@ -683,180 +672,6 @@ Command.prototype.promptSingleLine = function(str, fn){
}).resume();
};

/**
* Multi-line prompt.
*
* @param {String} str
* @param {Function} fn
* @api private
*/

Command.prototype.promptMultiLine = function(str, fn){
var buf = '';
this.log(str);
process.stdin.setEncoding('utf8');
process.stdin.on('data', function(val){
if ('\n' == val) {
process.stdin.removeAllListeners('data');
fn(buf);
} else {
buf += val;
}
}).resume();
};

/**
* Prompt `str` and callback `fn(val)`
*
* Commander supports single-line and multi-line prompts.
* To issue a single-line prompt simply add white-space
* to the end of `str`, something like "name: ", whereas
* for a multi-line prompt omit this "description:".
*
*
* Examples:
*
* program.prompt('Username: ', function(name){
* this.log('hi %s', name);
* });
*
* program.prompt('Description:', function(desc){
* this.log('description was "%s"', desc.trim());
* });
*
* @param {String} 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);
};

/**
* Prompt for password with `str`, `mask` char and callback `fn(val)`.
*
* The mask string defaults to '', aka no output is
* written while typing, you may want to use "*" etc.
*
* Examples:
*
* program.password('Password: ', function(pass){
* this.log('got "%s"', pass);
* process.stdin.destroy();
* });
*
* program.password('Password: ', '*', function(pass){
* this.log('got "%s"', pass);
* process.stdin.destroy();
* });
*
* @param {String} str
* @param {String} mask
* @param {Function} fn
* @api public
*/

Command.prototype.password = function(str, mask, fn){
var self = this
, buf = '';

// default mask
if ('function' == typeof mask) {
fn = mask;
mask = '';
}

tty.setRawMode(true);
process.stdout.write(str);

// keypress
process.stdin.on('keypress', function(c, key){
if (key && 'enter' == key.name) {
this.log();
process.stdin.removeAllListeners('keypress');
tty.setRawMode(false);
if (!buf.trim().length) return self.password(str, mask, fn);
fn(buf);
return;
}

if (key && key.ctrl && 'c' == key.name) {
this.log(util.format('%s', buf));
this.exit();
}

process.stdout.write(mask);
buf += c;
}).resume();
};

/**
* Confirmation prompt with `str` and callback `fn(bool)`
*
* Examples:
*
* program.confirm('continue? ', function(ok){
* this.log(' got %j', ok);
* process.stdin.destroy();
* });
*
* @param {String} str
* @param {Function} fn
* @api public
*/


Command.prototype.confirm = function(str, fn){
var self = this;
this.prompt(str, function(ok){
if (!ok.trim()) {
return self.confirm(str, fn);
}
fn(parseBool(ok));
});
};

/**
* Choice prompt with `list` of items and callback `fn(index, item)`
*
* Examples:
*
* var list = ['tobi', 'loki', 'jane', 'manny', 'luna'];
*
* this.log('Choose the coolest pet:');
* program.choose(list, function(i){
* this.log('you chose %d "%s"', i, list[i]);
* process.stdin.destroy();
* });
*
* @param {Array} list
* @param {Function} fn
* @api public
*/

Command.prototype.choose = function(list, fn){
var self = this;

list.forEach(function(item, i){
this.log(util.format(' %d) %s', i + 1, item));
});

function again() {
self.prompt(' : ', function(val){
val = parseInt(val, 10) - 1;
if (null == list[val]) {
again();
} else {
fn(val, list[val]);
}
});
}

again();
};

Command.prototype.exit = process.exit
Command.prototype.log = console.log
Command.prototype.error = console.error
Expand Down Expand Up @@ -906,8 +721,3 @@ function pad(str, width) {
*/

exports.option('-h, --help', 'output usage information');
exports.on('help', function(){
this.log(this.helpInformation());
exports.emit('--help');
this.exit()
});

0 comments on commit 7600910

Please sign in to comment.