MCPcopy
hub / github.com/tj/commander.js / _optionEx

Method _optionEx

lib/command.js:732–755  ·  view source on GitHub ↗

* Internal implementation shared by .option() and .requiredOption() * * @return {Command} `this` command for chaining * @private

(config, flags, description, fn, defaultValue)

Source from the content-addressed store, hash-verified

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.

Callers 2

optionMethod · 0.95
requiredOptionMethod · 0.95

Calls 5

createOptionMethod · 0.95
addOptionMethod · 0.95
makeOptionMandatoryMethod · 0.80
argParserMethod · 0.45
defaultMethod · 0.45

Tested by

no test coverage detected