diff options
Diffstat (limited to 'node_modules/commander/index.js')
-rw-r--r-- | node_modules/commander/index.js | 341 |
1 files changed, 220 insertions, 121 deletions
diff --git a/node_modules/commander/index.js b/node_modules/commander/index.js index d5dbe1868..3ad0cacfd 100644 --- a/node_modules/commander/index.js +++ b/node_modules/commander/index.js @@ -10,6 +10,12 @@ var basename = path.basename; var fs = require('fs'); /** + * Inherit `Command` from `EventEmitter.prototype`. + */ + +require('util').inherits(Command, EventEmitter); + +/** * Expose the root command. */ @@ -37,9 +43,9 @@ exports.Option = Option; function Option(flags, description) { this.flags = flags; - this.required = ~flags.indexOf('<'); - this.optional = ~flags.indexOf('['); - this.bool = !~flags.indexOf('-no-'); + this.required = flags.indexOf('<') >= 0; + this.optional = flags.indexOf('[') >= 0; + this.bool = flags.indexOf('-no-') === -1; flags = flags.split(/[ ,|]+/); if (flags.length > 1 && !/^[[<]/.test(flags[1])) this.short = flags.shift(); this.long = flags.shift(); @@ -60,6 +66,18 @@ Option.prototype.name = function() { }; /** + * Return option name, in a camelcase format that can be used + * as a object attribute key. + * + * @return {String} + * @api private + */ + +Option.prototype.attributeName = function() { + return camelcase(this.name()); +}; + +/** * Check if `arg` matches the short or long flag. * * @param {String} arg @@ -68,7 +86,7 @@ Option.prototype.name = function() { */ Option.prototype.is = function(arg) { - return arg == this.short || arg == this.long; + return this.short === arg || this.long === arg; }; /** @@ -88,12 +106,6 @@ function Command(name) { } /** - * Inherit from `EventEmitter.prototype`. - */ - -Command.prototype.__proto__ = EventEmitter.prototype; - -/** * Add command `name`. * * The `.action()` callback is invoked when the @@ -155,6 +167,10 @@ Command.prototype.__proto__ = EventEmitter.prototype; */ Command.prototype.command = function(name, desc, opts) { + if (typeof desc === 'object' && desc !== null) { + opts = desc; + desc = null; + } opts = opts || {}; var args = name.split(/ +/); var cmd = new Command(args.shift()); @@ -165,7 +181,6 @@ Command.prototype.command = function(name, desc, opts) { this._execs[cmd._name] = true; if (opts.isDefault) this.defaultExecutable = cmd._name; } - cmd._noHelp = !!opts.noHelp; this.commands.push(cmd); cmd.parseExpectedArgs(args); @@ -181,7 +196,7 @@ Command.prototype.command = function(name, desc, opts) { * @api public */ -Command.prototype.arguments = function (desc) { +Command.prototype.arguments = function(desc) { return this.parseExpectedArgs(desc.split(/ +/)); }; @@ -277,7 +292,7 @@ Command.prototype.action = function(fn) { if (parsed.args.length) args = parsed.args.concat(args); self._args.forEach(function(arg, i) { - if (arg.required && null == args[i]) { + if (arg.required && args[i] == null) { self.missingArgument(arg.name); } else if (arg.variadic) { if (i !== self._args.length - 1) { @@ -356,32 +371,34 @@ Command.prototype.action = function(fn) { */ Command.prototype.option = function(flags, description, fn, defaultValue) { - var self = this - , option = new Option(flags, description) - , oname = option.name() - , name = camelcase(oname); + var self = this, + option = new Option(flags, description), + oname = option.name(), + name = option.attributeName(); // default as 3rd arg - if (typeof fn != 'function') { + if (typeof fn !== 'function') { if (fn instanceof RegExp) { var regex = fn; fn = function(val, def) { var m = regex.exec(val); return m ? m[0] : def; - } - } - else { + }; + } else { defaultValue = fn; fn = null; } } // preassign default value only for --no-*, [optional], or <required> - if (false == option.bool || option.optional || option.required) { + if (!option.bool || option.optional || option.required) { // when --no-* we make sure default is true - if (false == option.bool) defaultValue = true; + if (!option.bool) defaultValue = true; // preassign only if we have a default - if (undefined !== defaultValue) self[name] = defaultValue; + if (defaultValue !== undefined) { + self[name] = defaultValue; + option.defaultValue = defaultValue; + } } // register the option @@ -391,21 +408,21 @@ Command.prototype.option = function(flags, description, fn, defaultValue) { // and conditionally invoke the callback this.on('option:' + oname, function(val) { // coercion - if (null !== val && fn) val = fn(val, undefined === self[name] - ? defaultValue - : self[name]); + if (val !== null && fn) { + val = fn(val, self[name] === undefined ? defaultValue : self[name]); + } // unassigned or bool - if ('boolean' == typeof self[name] || 'undefined' == typeof self[name]) { + if (typeof self[name] === 'boolean' || typeof self[name] === 'undefined') { // if no value, bool true, and we have a default, then use it! - if (null == val) { + if (val == null) { self[name] = option.bool ? defaultValue || true : false; } else { self[name] = val; } - } else if (null !== val) { + } else if (val !== null) { // reassign self[name] = val; } @@ -422,8 +439,8 @@ Command.prototype.option = function(flags, description, fn, defaultValue) { * @api public */ Command.prototype.allowUnknownOption = function(arg) { - this._allowUnknownOption = arguments.length === 0 || arg; - return this; + this._allowUnknownOption = arguments.length === 0 || arg; + return this; }; /** @@ -467,7 +484,7 @@ Command.prototype.parse = function(argv) { })[0]; } - if (this._execs[name] && typeof this._execs[name] != "function") { + if (this._execs[name] && typeof this._execs[name] !== 'function') { return this.executeSubCommand(argv, args, parsed.unknown); } else if (aliasCommand) { // is alias of a subCommand @@ -495,10 +512,10 @@ Command.prototype.executeSubCommand = function(argv, args, unknown) { args = args.concat(unknown); if (!args.length) this.help(); - if ('help' == args[0] && 1 == args.length) this.help(); + if (args[0] === 'help' && args.length === 1) this.help(); // <cmd> --help - if ('help' == args[0]) { + if (args[0] === 'help') { args[0] = args[1]; args[1] = '--help'; } @@ -508,15 +525,14 @@ Command.prototype.executeSubCommand = function(argv, args, unknown) { // name of the subcommand, link `pm-install` var bin = basename(f, '.js') + '-' + args[0]; - // In case of globally installed, get the base dir where executable // subcommand file should be located at - var baseDir - , link = fs.lstatSync(f).isSymbolicLink() ? fs.readlinkSync(f) : f; + var baseDir, + link = fs.lstatSync(f).isSymbolicLink() ? fs.readlinkSync(f) : f; // when symbolink is relative path if (link !== f && link.charAt(0) !== '/') { - link = path.join(dirname(f), link) + link = path.join(dirname(f), link); } baseDir = dirname(link); @@ -541,28 +557,28 @@ Command.prototype.executeSubCommand = function(argv, args, unknown) { // add executable arguments to spawn args = (process.execArgv || []).concat(args); - proc = spawn('node', args, { stdio: 'inherit', customFds: [0, 1, 2] }); + proc = spawn(process.argv[0], args, { stdio: 'inherit', customFds: [0, 1, 2] }); } else { proc = spawn(bin, args, { stdio: 'inherit', customFds: [0, 1, 2] }); } } else { args.unshift(bin); - proc = spawn(process.execPath, args, { stdio: 'inherit'}); + proc = spawn(process.execPath, args, { stdio: 'inherit' }); } var signals = ['SIGUSR1', 'SIGUSR2', 'SIGTERM', 'SIGINT', 'SIGHUP']; signals.forEach(function(signal) { - process.on(signal, function(){ - if ((proc.killed === false) && (proc.exitCode === null)){ + process.on(signal, function() { + if (proc.killed === false && proc.exitCode === null) { proc.kill(signal); } }); }); proc.on('close', process.exit.bind(process)); proc.on('error', function(err) { - if (err.code == "ENOENT") { + if (err.code === 'ENOENT') { console.error('\n %s(1) does not exist, try --help\n', bin); - } else if (err.code == "EACCES") { + } else if (err.code === 'EACCES') { console.error('\n %s(1) not executable. try chmod or run with root\n', bin); } process.exit(1); @@ -583,15 +599,15 @@ Command.prototype.executeSubCommand = function(argv, args, unknown) { */ Command.prototype.normalize = function(args) { - var ret = [] - , arg - , lastOpt - , index; + var ret = [], + arg, + lastOpt, + index; for (var i = 0, len = args.length; i < len; ++i) { arg = args[i]; if (i > 0) { - lastOpt = this.optionFor(args[i-1]); + lastOpt = this.optionFor(args[i - 1]); } if (arg === '--') { @@ -600,7 +616,7 @@ Command.prototype.normalize = function(args) { break; } else if (lastOpt && lastOpt.required) { ret.push(arg); - } else if (arg.length > 1 && '-' == arg[0] && '-' != arg[1]) { + } else if (arg.length > 1 && arg[0] === '-' && arg[1] !== '-') { arg.slice(1).split('').forEach(function(c) { ret.push('-' + c); }); @@ -644,6 +660,10 @@ Command.prototype.parseArgs = function(args, unknown) { if (unknown.length > 0) { this.unknownOption(unknown[0]); } + if (this.commands.length === 0 && + this._args.filter(function(a) { return a.required }).length === 0) { + this.emit('command:*'); + } } return this; @@ -675,11 +695,11 @@ Command.prototype.optionFor = function(arg) { */ Command.prototype.parseOptions = function(argv) { - var args = [] - , len = argv.length - , literal - , option - , arg; + var args = [], + len = argv.length, + literal, + option, + arg; var unknownOptions = []; @@ -693,7 +713,7 @@ Command.prototype.parseOptions = function(argv) { continue; } - if ('--' == arg) { + if (arg === '--') { literal = true; continue; } @@ -706,12 +726,12 @@ Command.prototype.parseOptions = function(argv) { // requires arg if (option.required) { arg = argv[++i]; - if (null == arg) return this.optionMissingArgument(option); + if (arg == null) return this.optionMissingArgument(option); this.emit('option:' + option.name(), arg); // optional arg } else if (option.optional) { - arg = argv[i+1]; - if (null == arg || ('-' == arg[0] && '-' != arg)) { + arg = argv[i + 1]; + if (arg == null || (arg[0] === '-' && arg !== '-')) { arg = null; } else { ++i; @@ -725,13 +745,13 @@ Command.prototype.parseOptions = function(argv) { } // looks like an option - if (arg.length > 1 && '-' == arg[0]) { + if (arg.length > 1 && arg[0] === '-') { unknownOptions.push(arg); // If the next argument looks like it might be // an argument for this option, we pass it on. // If it isn't, then it'll simply be ignored - if (argv[i+1] && '-' != argv[i+1][0]) { + if ((i + 1) < argv.length && argv[i + 1][0] !== '-') { unknownOptions.push(argv[++i]); } continue; @@ -751,12 +771,12 @@ Command.prototype.parseOptions = function(argv) { * @api public */ Command.prototype.opts = function() { - var result = {} - , len = this.options.length; + var result = {}, + len = this.options.length; - for (var i = 0 ; i < len; i++) { - var key = camelcase(this.options[i].name()); - result[key] = key === 'version' ? this._version : this[key]; + for (var i = 0; i < len; i++) { + var key = this.options[i].attributeName(); + result[key] = key === this._versionOptionName ? this._version : this[key]; } return result; }; @@ -836,11 +856,13 @@ Command.prototype.variadicArgNotLast = function(name) { */ Command.prototype.version = function(str, flags) { - if (0 == arguments.length) return this._version; + if (arguments.length === 0) return this._version; this._version = str; flags = flags || '-V, --version'; - this.option(flags, 'output the version number'); - this.on('option:version', function() { + var versionOption = new Option(flags, 'output the version number'); + this._versionOptionName = versionOption.long.substr(2) || 'version'; + this.options.push(versionOption); + this.on('option:' + this._versionOptionName, function() { process.stdout.write(str + '\n'); process.exit(0); }); @@ -851,13 +873,15 @@ Command.prototype.version = function(str, flags) { * Set the description to `str`. * * @param {String} str + * @param {Object} argsDescription * @return {String|Command} * @api public */ -Command.prototype.description = function(str) { - if (0 === arguments.length) return this._description; +Command.prototype.description = function(str, argsDescription) { + if (arguments.length === 0) return this._description; this._description = str; + this._argsDescription = argsDescription; return this; }; @@ -871,12 +895,14 @@ Command.prototype.description = function(str) { Command.prototype.alias = function(alias) { var command = this; - if(this.commands.length !== 0) { - command = this.commands[this.commands.length - 1] + if (this.commands.length !== 0) { + command = this.commands[this.commands.length - 1]; } if (arguments.length === 0) return command._alias; + if (alias === command._name) throw new Error('Command alias can\'t be the same as its name'); + command._alias = alias; return this; }; @@ -894,11 +920,11 @@ Command.prototype.usage = function(str) { return humanReadableArgName(arg); }); - var usage = '[options]' - + (this.commands.length ? ' [command]' : '') - + (this._args.length ? ' ' + args.join(' ') : ''); + var usage = '[options]' + + (this.commands.length ? ' [command]' : '') + + (this._args.length ? ' ' + args.join(' ') : ''); - if (0 == arguments.length) return this._usage || usage; + if (arguments.length === 0) return this._usage || usage; this._usage = str; return this; @@ -913,12 +939,51 @@ Command.prototype.usage = function(str) { */ Command.prototype.name = function(str) { - if (0 === arguments.length) return this._name; + if (arguments.length === 0) return this._name; this._name = str; return this; }; /** + * Return prepared commands. + * + * @return {Array} + * @api private + */ + +Command.prototype.prepareCommands = function() { + return this.commands.filter(function(cmd) { + return !cmd._noHelp; + }).map(function(cmd) { + var args = cmd._args.map(function(arg) { + return humanReadableArgName(arg); + }).join(' '); + + return [ + cmd._name + + (cmd._alias ? '|' + cmd._alias : '') + + (cmd.options.length ? ' [options]' : '') + + (args ? ' ' + args : ''), + cmd._description + ]; + }); +}; + +/** + * Return the largest command length. + * + * @return {Number} + * @api private + */ + +Command.prototype.largestCommandLength = function() { + var commands = this.prepareCommands(); + return commands.reduce(function(max, command) { + return Math.max(max, command[0].length); + }, 0); +}; + +/** * Return the largest option length. * * @return {Number} @@ -926,12 +991,53 @@ Command.prototype.name = function(str) { */ Command.prototype.largestOptionLength = function() { - return this.options.reduce(function(max, option) { + var options = [].slice.call(this.options); + options.push({ + flags: '-h, --help' + }); + return options.reduce(function(max, option) { return Math.max(max, option.flags.length); }, 0); }; /** + * Return the largest arg length. + * + * @return {Number} + * @api private + */ + +Command.prototype.largestArgLength = function() { + return this._args.reduce(function(max, arg) { + return Math.max(max, arg.name.length); + }, 0); +}; + +/** + * Return the pad width. + * + * @return {Number} + * @api private + */ + +Command.prototype.padWidth = function() { + var width = this.largestOptionLength(); + if (this._argsDescription && this._args.length) { + if (this.largestArgLength() > width) { + width = this.largestArgLength(); + } + } + + if (this.commands && this.commands.length) { + if (this.largestCommandLength() > width) { + width = this.largestCommandLength(); + } + } + + return width; +}; + +/** * Return help for options. * * @return {String} @@ -939,12 +1045,13 @@ Command.prototype.largestOptionLength = function() { */ Command.prototype.optionHelp = function() { - var width = this.largestOptionLength(); + var width = this.padWidth(); // Append the help information return this.options.map(function(option) { - return pad(option.flags, width) + ' ' + option.description; - }).concat([pad('-h, --help', width) + ' ' + 'output usage information']) + return pad(option.flags, width) + ' ' + option.description + + ((option.bool && option.defaultValue !== undefined) ? ' (default: ' + option.defaultValue + ')' : ''); + }).concat([pad('-h, --help', width) + ' ' + 'output usage information']) .join('\n'); }; @@ -958,35 +1065,17 @@ Command.prototype.optionHelp = function() { Command.prototype.commandHelp = function() { if (!this.commands.length) return ''; - var commands = this.commands.filter(function(cmd) { - return !cmd._noHelp; - }).map(function(cmd) { - var args = cmd._args.map(function(arg) { - return humanReadableArgName(arg); - }).join(' '); - - return [ - cmd._name - + (cmd._alias ? '|' + cmd._alias : '') - + (cmd.options.length ? ' [options]' : '') - + ' ' + args - , cmd._description - ]; - }); - - var width = commands.reduce(function(max, command) { - return Math.max(max, command[0].length); - }, 0); + var commands = this.prepareCommands(); + var width = this.padWidth(); return [ - '' - , ' Commands:' - , '' - , commands.map(function(cmd) { + ' Commands:', + '', + commands.map(function(cmd) { var desc = cmd[1] ? ' ' + cmd[1] : ''; - return pad(cmd[0], width) + desc; - }).join('\n').replace(/^/gm, ' ') - , '' + return (desc ? pad(cmd[0], width) : cmd[0]) + desc; + }).join('\n').replace(/^/gm, ' '), + '' ].join('\n'); }; @@ -1001,9 +1090,20 @@ Command.prototype.helpInformation = function() { var desc = []; if (this._description) { desc = [ - ' ' + this._description - , '' + ' ' + this._description, + '' ]; + + var argsDescription = this._argsDescription; + if (argsDescription && this._args.length) { + var width = this.padWidth(); + desc.push(' Arguments:'); + desc.push(''); + this._args.forEach(function(arg) { + desc.push(' ' + pad(arg.name, width) + ' ' + argsDescription[arg.name]); + }); + desc.push(''); + } } var cmdName = this._name; @@ -1011,9 +1111,9 @@ Command.prototype.helpInformation = function() { cmdName = cmdName + '|' + this._alias; } var usage = [ + '', + ' Usage: ' + cmdName + ' ' + this.usage(), '' - ,' Usage: ' + cmdName + ' ' + this.usage() - , '' ]; var cmds = []; @@ -1021,17 +1121,17 @@ Command.prototype.helpInformation = function() { if (commandHelp) cmds = [commandHelp]; var options = [ + ' Options:', + '', + '' + this.optionHelp().replace(/^/gm, ' '), '' - , ' Options:' - , '' - , '' + this.optionHelp().replace(/^/gm, ' ') - , '' ]; return usage .concat(desc) .concat(options) .concat(cmds) + .concat(['']) .join('\n'); }; @@ -1045,7 +1145,7 @@ Command.prototype.outputHelp = function(cb) { if (!cb) { cb = function(passthru) { return passthru; - } + }; } process.stdout.write(cb(this.helpInformation())); this.emit('--help'); @@ -1101,7 +1201,7 @@ function pad(str, width) { function outputHelpIfNecessary(cmd, options) { options = options || []; for (var i = 0; i < options.length; i++) { - if (options[i] == '--help' || options[i] == '-h') { + if (options[i] === '--help' || options[i] === '-h') { cmd.outputHelp(); process.exit(0); } @@ -1121,7 +1221,7 @@ function humanReadableArgName(arg) { return arg.required ? '<' + nameOutput + '>' - : '[' + nameOutput + ']' + : '[' + nameOutput + ']'; } // for versions before node v0.8 when there weren't `fs.existsSync` @@ -1134,4 +1234,3 @@ function exists(file) { return false; } } - |