Skip to content

Commit

Permalink
Add suppressWarnings() for warnings in tj#1915 tj#1917 tj#1931 tj#1938
Browse files Browse the repository at this point in the history
  • Loading branch information
aweebit committed Aug 11, 2023
1 parent d038570 commit c26d4c7
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 0 deletions.
15 changes: 15 additions & 0 deletions lib/command.js
Expand Up @@ -57,6 +57,7 @@ class Command extends EventEmitter {
this._showHelpAfterError = false;
this._showSuggestionAfterError = true;

this._suppressWarnings = false;
// see .configureOutput() for docs
this._outputConfiguration = {
writeOut: (str) => process.stdout.write(str),
Expand Down Expand Up @@ -88,6 +89,7 @@ class Command extends EventEmitter {
* @return {Command} `this` command for chaining
*/
copyInheritedSettings(sourceCommand) {
this._suppressWarnings = sourceCommand._suppressWarnings;
this._outputConfiguration = sourceCommand._outputConfiguration;
this._hasHelpOption = sourceCommand._hasHelpOption;
this._helpFlags = sourceCommand._helpFlags;
Expand All @@ -109,6 +111,19 @@ class Command extends EventEmitter {
return this;
}

/**
* Suppress warnings about library usage patterns that are not always wrong
* but are often connected with a developer mistake,
* or a different pattern better suited for the use case scenario is offered.
*
* @param {boolean} [suppress=true]
* @return {Command} `this` command for chaining
*/
suppressWarnings(suppress = true) {
this._suppressWarnings = !!suppress;
return this;
}

/**
* Define a command.
*
Expand Down
6 changes: 6 additions & 0 deletions tests/command.chain.test.js
Expand Up @@ -4,6 +4,12 @@ const { Command, Option, Argument } = require('../');
// parse and parseAsync are tested in command.parse.test.js

describe('Command methods that should return this for chaining', () => {
test('when call .suppressWarnings() then returns this', () => {
const program = new Command();
const result = program.suppressWarnings();
expect(result).toBe(program);
});

test('when call .command() with description for stand-alone executable then returns this', () => {
const program = new Command();
const result = program.command('foo', 'foo description');
Expand Down
9 changes: 9 additions & 0 deletions typings/index.d.ts
Expand Up @@ -285,6 +285,15 @@ export class Command {

constructor(name?: string);

/**
* Suppress warnings about library usage patterns that are not always wrong
* but are often connected with a developer mistake,
* or a different pattern better suited for the use case scenario is offered.
*
* @return {Command} `this` command for chaining
*/
suppressWarnings(suppress?: boolean): this;

/**
* Set the program version to `str`.
*
Expand Down
4 changes: 4 additions & 0 deletions typings/index.test-d.ts
Expand Up @@ -32,6 +32,10 @@ expectType<readonly commander.Command[]>(program.commands);
expectType<readonly commander.Option[]>(program.options);
expectType<commander.Command | null>(program.parent);

// suppressWarnings
expectType<commander.Command>(program.suppressWarnings());
expectType<commander.Command>(program.suppressWarnings(false));

// version
expectType<commander.Command>(program.version('1.2.3'));
expectType<commander.Command>(program.version('1.2.3', '-r,--revision'));
Expand Down

0 comments on commit c26d4c7

Please sign in to comment.