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
Provenance
- Commit
80b3587- Tree
0acc3c3- Author
John Gee <john@ruru.gen.nz>- Committer
John Gee <john@ruru.gen.nz>- Parent
dd23b4c
1 file changed+51 added−49 removed
modified:index.js+51 added-49 removed
25 unmodified lines26 */27 28 exports.Command = Command;29 30+class 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+ */38 34-exports.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+ }50 36-/**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+ */57 44-function 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+ };61 56-/**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+ */69 63-Option.prototype.name = function() {64- return this.long.replace(/^--/, '');65-};70+ attributeName() {71+ return camelcase(this.name().replace(/^no-/, ''));72+ };73 67-/**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+ */81 75-Option.prototype.attributeName = function() {76- return camelcase(this.name().replace(/^no-/, ''));77-};82+ is(arg) {83+ return this.short === arg || this.long === arg;84+ };85+}86 87 /**80- * Check if `arg` matches the short or long flag.81- *82- * @param {String} arg83- * @return {Boolean}84- * @api private88+ * Expose `Option`.89 */90 87-Option.prototype.is = function(arg) {88- return this.short === arg || this.long === arg;89-};91+exports.Option = Option;92 93 /**94 * CommanderError class95 * @class1539 unmodified lines