Skip to content
Myr

Back to shared implementation method for .option and .requiredOption (#1363)

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

authored by John Gee · committed by GitHub · parent e5512a4 · Browse files · 1 file+27 added−19 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+27 added−19 removed

  • other:+27 added−19 removed

1 file+27 added−19 removed

modified:index.js+27 added-19 removed
665 unmodified lines
666 return this;667 }668669 /**670 * Internal implementation shared by .option() and .requiredOption()671 *672 * @api private673 */674 _optionEx(config, flags, description, fn, defaultValue) {675 const option = new Option(flags, description);676 option.makeOptionMandatory(!!config.mandatory);677 if (typeof fn === 'function') {678 option.default(defaultValue).argParser(fn);679 } else if (fn instanceof RegExp) {680 // deprecated681 const regex = fn;682 fn = (val, def) => {683 const m = regex.exec(val);684 return m ? m[0] : def;685 };686 option.default(defaultValue).argParser(fn);687 } else {688 option.default(fn);689 }690691 return this.addOption(option);692 }693694 /**695 * Define option with `flags`, `description` and optional696 * coercion `fn`.697 *698 * The `flags` string contains the short and/or long flags,
44 unmodified lines
743 * @api public744 */745746 option(flags, description, fn, defaultValue) {722 const option = new Option(flags, description);723 if (typeof fn === 'function') {724 option.default(defaultValue).argParser(fn);725 } else if (fn instanceof RegExp) {726 // deprecated727 const regex = fn;728 fn = (val, def) => {729 const m = regex.exec(val);730 return m ? m[0] : def;731 };732 option.default(defaultValue).argParser(fn);733 } else {734 option.default(fn);735 }736737 return this.addOption(option);747 return this._optionEx({}, flags, description, fn, defaultValue);748 };749750 /**751 * Add a required option which must have a value after parsing. This usually means
9 unmodified lines
761 * @api public762 */763764 requiredOption(flags, description, fn, defaultValue) {755 this.option(flags, description, fn, defaultValue);756 this.options[this.options.length - 1].makeOptionMandatory();757 return this;765 return this._optionEx({ mandatory: true }, flags, description, fn, defaultValue);766 };767768 /**769 * Alter parsing of short flags with optional values.
1311 unmodified lines