Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor Option from prototype to class #1133

Merged
merged 1 commit into from Jan 1, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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