Skip to content
Myr

Refactor Option from prototype to class

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

authored by John Gee · committed by John Gee · parent dd23b4c · Browse files · 1 file+51 added−49 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+51 added−49 removed

  • other:+51 added−49 removed

1 file+51 added−49 removed

modified:index.js+51 added-49 removed
25 unmodified lines
26 */2728exports.Command = Command;2930class Option {30/**31 * Expose `Option`.32 */31 /**32 * Initialize a new `Option` with the given `flags` and `description`.33 *34 * @param {String} flags35 * @param {String} description36 * @api public37 */3834exports.Option = Option;39 constructor(flags, description) {40 this.flags = flags;41 this.required = flags.indexOf('<') >= 0; // A value must be supplied when the option is specified.42 this.optional = flags.indexOf('[') >= 0; // A value is optional when the option is specified.43 this.mandatory = false; // The option must have a value after parsing, which usually means it must be specified on command line.44 this.negate = flags.indexOf('-no-') !== -1;45 flags = flags.split(/[ ,|]+/);46 if (flags.length > 1 && !/^[[<]/.test(flags[1])) this.short = flags.shift();47 this.long = flags.shift();48 this.description = description || '';49 }5036/**37 * Initialize a new `Option` with the given `flags` and `description`.38 *39 * @param {String} flags40 * @param {String} description41 * @api public42 */51 /**52 * Return option name.53 *54 * @return {String}55 * @api private56 */5744function Option(flags, description) {45 this.flags = flags;46 this.required = flags.indexOf('<') >= 0; // A value must be supplied when the option is specified.47 this.optional = flags.indexOf('[') >= 0; // A value is optional when the option is specified.48 this.mandatory = false; // The option must have a value after parsing, which usually means it must be specified on command line.49 this.negate = flags.indexOf('-no-') !== -1;50 flags = flags.split(/[ ,|]+/);51 if (flags.length > 1 && !/^[[<]/.test(flags[1])) this.short = flags.shift();52 this.long = flags.shift();53 this.description = description || '';54}58 name() {59 return this.long.replace(/^--/, '');60 };6156/**57 * Return option name.58 *59 * @return {String}60 * @api private61 */62 /**63 * Return option name, in a camelcase format that can be used64 * as a object attribute key.65 *66 * @return {String}67 * @api private68 */6963Option.prototype.name = function() {64 return this.long.replace(/^--/, '');65};70 attributeName() {71 return camelcase(this.name().replace(/^no-/, ''));72 };7367/**68 * Return option name, in a camelcase format that can be used69 * as a object attribute key.70 *71 * @return {String}72 * @api private73 */74 /**75 * Check if `arg` matches the short or long flag.76 *77 * @param {String} arg78 * @return {Boolean}79 * @api private80 */8175Option.prototype.attributeName = function() {76 return camelcase(this.name().replace(/^no-/, ''));77};82 is(arg) {83 return this.short === arg || this.long === arg;84 };85}8687/**80 * Check if `arg` matches the short or long flag.81 *82 * @param {String} arg83 * @return {Boolean}84 * @api private88 * Expose `Option`.89 */9087Option.prototype.is = function(arg) {88 return this.short === arg || this.long === arg;89};91exports.Option = Option;9293/**94 * CommanderError class95 * @class
1539 unmodified lines