Skip to content
Myr

Merge pull request #986 from shadowspawn/feature/928-refactor-negate

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

Rename bool property for clarity

authored by John Gee · committed by GitHub · parents f1924dd, 58c2ad8 · Browse files · 1 file+10 added−10 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+10 added−10 removed

  • other:+10 added−10 removed

1 file+10 added−10 removed

modified:index.js+10 added-10 removed
43 unmodified lines
44function Option(flags, description) {45 this.flags = flags;46 this.required = flags.indexOf('<') >= 0;47 this.optional = flags.indexOf('[') >= 0;48 this.bool = flags.indexOf('-no-') === -1;48 this.negate = flags.indexOf('-no-') !== -1;49 flags = flags.split(/[ ,|]+/);50 if (flags.length > 1 && !/^[[<]/.test(flags[1])) this.short = flags.shift();51 this.long = flags.shift();52 this.description = description || '';
349 unmodified lines
402 }403 }404405 // preassign default value only for --no-*, [optional], or <required>406 if (!option.bool || option.optional || option.required) {406 if (option.negate || option.optional || option.required) {407 // when --no-foo we make sure default is true, unless a --foo option is already defined408 if (!option.bool) {408 if (option.negate) {409 var opts = self.opts();410 defaultValue = Object.prototype.hasOwnProperty.call(opts, name) ? opts[name] : true;411 }412 // preassign only if we have a default
15 unmodified lines
428 }429430 // unassigned or bool431 if (typeof self[name] === 'boolean' || typeof self[name] === 'undefined') {432 // if no value, bool true, and we have a default, then use it!432 // if no value, negate false, and we have a default, then use it!433 if (val == null) {434 self[name] = option.bool435 ? defaultValue || true436 : false;434 self[name] = option.negate435 ? false436 : defaultValue || true;437 } else {438 self[name] = val;439 }440 } else if (val !== null) {441 // reassign442 self[name] = option.bool ? val : false;442 self[name] = option.negate ? false : val;443 }444 });445446 return this;
319 unmodified lines
766 } else {767 ++i;768 }769 this.emit('option:' + option.name(), arg);770 // bool770 // flag771 } else {772 this.emit('option:' + option.name());773 }774 continue;
301 unmodified lines
10761077 // Append the help information1078 return this.options.map(function(option) {1079 return pad(option.flags, width) + ' ' + option.description +1080 ((option.bool && option.defaultValue !== undefined) ? ' (default: ' + JSON.stringify(option.defaultValue) + ')' : '');1080 ((!option.negate && option.defaultValue !== undefined) ? ' (default: ' + JSON.stringify(option.defaultValue) + ')' : '');1081 }).concat([pad(this._helpFlags, width) + ' ' + this._helpDescription])1082 .join('\n');1083};1084
211 unmodified lines