Skip to content

Commit

Permalink
Refactor Option from prototype to class
Browse files Browse the repository at this point in the history
  • Loading branch information
shadowspawn committed Jan 1, 2020
1 parent dd23b4c commit 80b3587
Showing 1 changed file with 51 additions and 49 deletions.
100 changes: 51 additions & 49 deletions index.js
Expand Up @@ -27,66 +27,68 @@ exports = module.exports = new Command();

exports.Command = Command;

/**
* Expose `Option`.
*/
class Option {
/**
* Initialize a new `Option` with the given `flags` and `description`.
*
* @param {String} flags
* @param {String} description
* @api public
*/

exports.Option = Option;
constructor(flags, description) {
this.flags = flags;
this.required = flags.indexOf('<') >= 0; // A value must be supplied when the option is specified.
this.optional = flags.indexOf('[') >= 0; // A value is optional when the option is specified.
this.mandatory = false; // The option must have a value after parsing, which usually means it must be specified on command line.
this.negate = flags.indexOf('-no-') !== -1;
flags = flags.split(/[ ,|]+/);
if (flags.length > 1 && !/^[[<]/.test(flags[1])) this.short = flags.shift();
this.long = flags.shift();
this.description = description || '';
}

/**
* Initialize a new `Option` with the given `flags` and `description`.
*
* @param {String} flags
* @param {String} description
* @api public
*/
/**
* Return option name.
*
* @return {String}
* @api private
*/

function Option(flags, description) {
this.flags = flags;
this.required = flags.indexOf('<') >= 0; // A value must be supplied when the option is specified.
this.optional = flags.indexOf('[') >= 0; // A value is optional when the option is specified.
this.mandatory = false; // The option must have a value after parsing, which usually means it must be specified on command line.
this.negate = flags.indexOf('-no-') !== -1;
flags = flags.split(/[ ,|]+/);
if (flags.length > 1 && !/^[[<]/.test(flags[1])) this.short = flags.shift();
this.long = flags.shift();
this.description = description || '';
}
name() {
return this.long.replace(/^--/, '');
};

/**
* Return option name.
*
* @return {String}
* @api private
*/
/**
* Return option name, in a camelcase format that can be used
* as a object attribute key.
*
* @return {String}
* @api private
*/

Option.prototype.name = function() {
return this.long.replace(/^--/, '');
};
attributeName() {
return camelcase(this.name().replace(/^no-/, ''));
};

/**
* Return option name, in a camelcase format that can be used
* as a object attribute key.
*
* @return {String}
* @api private
*/
/**
* Check if `arg` matches the short or long flag.
*
* @param {String} arg
* @return {Boolean}
* @api private
*/

Option.prototype.attributeName = function() {
return camelcase(this.name().replace(/^no-/, ''));
};
is(arg) {
return this.short === arg || this.long === arg;
};
}

/**
* Check if `arg` matches the short or long flag.
*
* @param {String} arg
* @return {Boolean}
* @api private
* Expose `Option`.
*/

Option.prototype.is = function(arg) {
return this.short === arg || this.long === arg;
};
exports.Option = Option;

/**
* CommanderError class
Expand Down

0 comments on commit 80b3587

Please sign in to comment.