Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add .suppressWarnings() for warnings in #1915 #1931 #1938 #1940 #1955

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 15 additions & 0 deletions lib/command.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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