* Register callback `fn` for the command. * * @example * program * .command('serve') * .description('start service') * .action(function() { * // do work here * }); * * @param {Function} fn * @return {Command} `this` command for chaining
(fn)
| 555 | */ |
| 556 | |
| 557 | action(fn) { |
| 558 | const listener = (args) => { |
| 559 | // The .action callback takes an extra parameter which is the command or options. |
| 560 | const expectedArgsCount = this.registeredArguments.length; |
| 561 | const actionArgs = args.slice(0, expectedArgsCount); |
| 562 | if (this._storeOptionsAsProperties) { |
| 563 | actionArgs[expectedArgsCount] = this; // backwards compatible "options" |
| 564 | } else { |
| 565 | actionArgs[expectedArgsCount] = this.opts(); |
| 566 | } |
| 567 | actionArgs.push(this); |
| 568 | |
| 569 | return fn.apply(this, actionArgs); |
| 570 | }; |
| 571 | this._actionHandler = listener; |
| 572 | return this; |
| 573 | } |
| 574 | |
| 575 | /** |
| 576 | * Factory routine to create a new unattached option. |
no outgoing calls