* Internal implementation shared by .option() and .requiredOption() * * @return {Command} `this` command for chaining * @private
(config, flags, description, fn, defaultValue)
| 730 | * @private |
| 731 | */ |
| 732 | _optionEx(config, flags, description, fn, defaultValue) { |
| 733 | if (typeof flags === 'object' && flags instanceof Option) { |
| 734 | throw new Error( |
| 735 | 'To add an Option object use addOption() instead of option() or requiredOption()', |
| 736 | ); |
| 737 | } |
| 738 | const option = this.createOption(flags, description); |
| 739 | option.makeOptionMandatory(!!config.mandatory); |
| 740 | if (typeof fn === 'function') { |
| 741 | option.default(defaultValue).argParser(fn); |
| 742 | } else if (fn instanceof RegExp) { |
| 743 | // deprecated |
| 744 | const regex = fn; |
| 745 | fn = (val, def) => { |
| 746 | const m = regex.exec(val); |
| 747 | return m ? m[0] : def; |
| 748 | }; |
| 749 | option.default(defaultValue).argParser(fn); |
| 750 | } else { |
| 751 | option.default(fn); |
| 752 | } |
| 753 | |
| 754 | return this.addOption(option); |
| 755 | } |
| 756 | |
| 757 | /** |
| 758 | * Define option with `flags`, `description`, and optional argument parsing function or `defaultValue` or both. |
no test coverage detected