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

Method addHelpText

lib/command.js:2670–2691  ·  view source on GitHub ↗

* Add additional text to be displayed with the built-in help. * * Position is 'before' or 'after' to affect just this command, * and 'beforeAll' or 'afterAll' to affect this command and all its subcommands. * * @param {string} position - before or after built-in help * @param {(str

(position, text)

Source from the content-addressed store, hash-verified

2668 */
2669
2670 addHelpText(position, text) {
2671 const allowedValues = ['beforeAll', 'before', 'after', 'afterAll'];
2672 if (!allowedValues.includes(position)) {
2673 throw new Error(`Unexpected value for position to addHelpText.
2674Expecting one of '${allowedValues.join("', '")}'`);
2675 }
2676
2677 const helpEvent = `${position}Help`;
2678 this.on(helpEvent, (/** @type {HelpTextEventContext} */ context) => {
2679 let helpStr;
2680 if (typeof text === 'function') {
2681 helpStr = text({ error: context.error, command: context.command });
2682 } else {
2683 helpStr = text;
2684 }
2685 // Ignore falsy value when nothing to output.
2686 if (helpStr) {
2687 context.write(`${helpStr}\n`);
2688 }
2689 });
2690 return this;
2691 }
2692
2693 /**
2694 * Output help information if help flags specified

Calls 1

writeMethod · 0.80

Tested by

no test coverage detected