From 3a50c5daf15ef9451e9b02abdc7db95bc396d2a0 Mon Sep 17 00:00:00 2001 From: Wee Bit Date: Fri, 11 Aug 2023 19:58:12 +0200 Subject: [PATCH] Add suppressWarnings() for warnings in #1915 #1931 #1938 #1940 Motivation: https://github.com/tj/commander.js/pull/1940#issuecomment-1675099093 --- lib/command.js | 15 +++++++++++++++ tests/command.chain.test.js | 6 ++++++ typings/index.d.ts | 9 +++++++++ typings/index.test-d.ts | 4 ++++ 4 files changed, 34 insertions(+) diff --git a/lib/command.js b/lib/command.js index 590a271dd..99660fba3 100644 --- a/lib/command.js +++ b/lib/command.js @@ -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), @@ -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; @@ -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. * diff --git a/tests/command.chain.test.js b/tests/command.chain.test.js index 3f907c869..166ef97ed 100644 --- a/tests/command.chain.test.js +++ b/tests/command.chain.test.js @@ -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'); diff --git a/typings/index.d.ts b/typings/index.d.ts index 695c3bd25..76aa34118 100644 --- a/typings/index.d.ts +++ b/typings/index.d.ts @@ -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`. * diff --git a/typings/index.test-d.ts b/typings/index.test-d.ts index 734036fad..b1b7d2ffd 100644 --- a/typings/index.test-d.ts +++ b/typings/index.test-d.ts @@ -32,6 +32,10 @@ expectType(program.commands); expectType(program.options); expectType(program.parent); +// suppressWarnings +expectType(program.suppressWarnings()); +expectType(program.suppressWarnings(false)); + // version expectType(program.version('1.2.3')); expectType(program.version('1.2.3', '-r,--revision'));