refactor ternary
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 41bc6a9 · Browse files · 1 file·+25 added−25 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+25 added−25 removed
- other:+25 added−25 removed
Provenance
- Commit
cb44616- Tree
c641d7c- Author
TJ Holowaychuk <tj@vision-media.ca>- Committer
TJ Holowaychuk <tj@vision-media.ca>- Parent
41bc6a9
1 file changed+25 added−25 removed
modified:index.js+25 added-25 removed
112 unmodified lines113 * .version('0.0.1')114 * .option('-C, --chdir <path>', 'change the working directory')115 * .option('-c, --config <path>', 'set config path. defaults to ./deploy.conf')116 * .option('-T, --no-tests', 'ignore test hook')117- * 117+ *118 * program119 * .command('setup')120 * .description('run remote setup commands')121 * .action(function(){122 * console.log('setup');123 * });124- * 124+ *125 * program126 * .command('exec <cmd>')127 * .description('run the given remote command')128 * .action(function(cmd){129 * console.log('exec "%s"', cmd);130 * });131- * 131+ *132 * program133 * .command('*')134 * .description('deploy the given env')135 * .action(function(env){136 * console.log('deploying "%s"', env);137 * });138- * 138+ *139 * program.parse(process.argv);140 *141 * @param {String} name142 * @param {String} [desc]69 unmodified lines212 */213 214 Command.prototype.action = function(fn){215 var self = this;216- this.parent.on(this._name, function(args, unknown){ 216+ this.parent.on(this._name, function(args, unknown){217 // Parse any so-far unknown options218 unknown = unknown || [];219 var parsed = self.parseOptions(unknown);220- 220+221 // Output help if necessary222 outputHelpIfNecessary(self, parsed.unknown);223- 224- // If there are still any unknown options, then we simply 223+224+ // If there are still any unknown options, then we simply225 // die, unless someone asked for help, in which case we give it226 // to them, and then we die.227- if (parsed.unknown.length > 0) { 227+ if (parsed.unknown.length > 0) {228 self.unknownOption(parsed.unknown[0]);229 }230- 230+231 // Leftover arguments need to be pushed back. Fixes issue #56232 if (parsed.args.length) args = parsed.args.concat(args);233- 233+234 self._args.forEach(function(arg, i){235 if (arg.required && null == args[i]) {236 self.missingArgument(arg.name);237 }238 });239- 239+240 // Always append ourselves to the end of the arguments,241 // to make sure we match the number of arguments the user242 // expects243 if (self._args.length) {244 args[self._args.length] = self;245 } else {246 args.push(self);247 }248- 248+249 fn.apply(this, args);250 });251 return this;252 };253 254 /**255 * Define option with `flags`, `description` and optional256- * coercion `fn`. 256+ * coercion `fn`.257 *258 * The `flags` string should contain both the short and long flags,259 * separated by comma, a pipe or space. The following are all valid260 * all will output this way when `--help` is used.62 unmodified lines323 // when it's passed assign the value324 // and conditionally invoke the callback325 this.on(oname, function(val){326 // coercion327- if (null !== val && fn) val = fn(val, undefined !== self[name] ? self[name] : defaultValue);327+ if (null !== val && fn) val = fn(val, undefined === self[name] ? defaultValue : self[name]);328 329 // unassigned or bool330 if ('boolean' == typeof self[name] || 'undefined' == typeof self[name]) {331 // if no value, bool true, and we have a default, then use it!33 unmodified lines365 366 // process argv367 var parsed = this.parseOptions(this.normalize(argv.slice(2)));368 var args = this.args = parsed.args;369- 369+370 var result = this.parseArgs(this.args, parsed.unknown);371 372 // executable sub-commands373 var name = result.args[0];63 unmodified lines437 438 for (var i = 0, len = args.length; i < len; ++i) {439 arg = args[i];440 i > 0 && (lastOpt = this.optionFor(args[i-1]));441- 441+442 if (lastOpt && lastOpt.required) {443 ret.push(arg);444 } else if (arg.length > 1 && '-' == arg[0] && '-' != arg[1]) {445 arg.slice(1).split('').forEach(function(c){34 unmodified lines480 this.emit('*', args);481 }482 } else {483 outputHelpIfNecessary(this, unknown);484- 484+485 // If there were no args and we have unknown options,486 // then they are extraneous and we need to error.487- if (unknown.length > 0) { 487+ if (unknown.length > 0) {488 this.unknownOption(unknown[0]);489 }490 }491 73 unmodified lines565 this.emit(option.name());566 }567 continue;568 }569- 569+570 // looks like an option571 if (arg.length > 1 && '-' == arg[0]) {572 unknownOptions.push(arg);573- 573+574 // If the next argument looks like it might be575 // an argument for this option, we pass it on.576 // If it isn't, then it'll simply be ignored577 if (argv[i+1] && '-' != argv[i+1][0]) {578 unknownOptions.push(argv[++i]);579 }580 continue;581 }582- 582+583 // arg584 args.push(arg);585 }586- 586+587 return { args: args, unknown: unknownOptions };588 };589 590 /**129 unmodified lines720 */721 722 Command.prototype.optionHelp = function(){723 var width = this.largestOptionLength();724- 724+725 // Prepend the help information726 return [pad('-h, --help', width) + ' ' + 'output usage information']727 .concat(this.options.map(function(option){728 return pad(option.flags, width)22 unmodified lines751 : '[' + arg.name + ']';752 }).join(' ');753 754 return pad(cmd._name755- + (cmd.options.length 755+ + (cmd.options.length756 ? ' [options]'757 : '') + ' ' + args, 22)758 + (cmd.description()759 ? ' ' + cmd.description()93 unmodified lines