From 3f158325736b92991089fcc9df5c8f6be4dad1ed Mon Sep 17 00:00:00 2001 From: John Gee Date: Tue, 31 Dec 2019 10:33:00 +1300 Subject: [PATCH] Refactor Option from prototype to class --- index.js | 100 ++++++++++++++++++++++++++++--------------------------- 1 file changed, 51 insertions(+), 49 deletions(-) diff --git a/index.js b/index.js index 918d04a2b..5e831a574 100644 --- a/index.js +++ b/index.js @@ -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