From e8bea4aedd750418d094591d63699010f5c7b623 Mon Sep 17 00:00:00 2001 From: Wee Bit Date: Thu, 3 Aug 2023 13:04:15 +0300 Subject: [PATCH 1/5] Fix version() parameter type Borrowed from a3f0e28 that was supposed to land in the now-closed #1921. --- lib/command.js | 2 +- typings/index.d.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/command.js b/lib/command.js index 590a271dd..e014d957f 100644 --- a/lib/command.js +++ b/lib/command.js @@ -1807,7 +1807,7 @@ Expecting one of '${allowedValues.join("', '")}'`); * * You can optionally supply the flags and description to override the defaults. * - * @param {string} str + * @param {string} [str] * @param {string} [flags] * @param {string} [description] * @return {this | string} `this` command for chaining, or version string if no arguments diff --git a/typings/index.d.ts b/typings/index.d.ts index 695c3bd25..73c606a5d 100644 --- a/typings/index.d.ts +++ b/typings/index.d.ts @@ -293,7 +293,7 @@ export class Command { * * You can optionally supply the flags and description to override the defaults. */ - version(str: string, flags?: string, description?: string): this; + version(str?: string, flags?: string, description?: string): this; /** * Define a command, implemented using an action handler. From e4d00db9dfb5e6620ab38b7c24070c5b16868562 Mon Sep 17 00:00:00 2001 From: Wee Bit Date: Wed, 2 Aug 2023 17:11:24 +0300 Subject: [PATCH 2/5] Change initial variable values in test for better error messages (cherry picked from commit 87db4ba81c33b0db8a0146afdcaac7e1a4dbcce8) --- tests/command.addHelpText.test.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/command.addHelpText.test.js b/tests/command.addHelpText.test.js index e0bac2895..7103b0861 100644 --- a/tests/command.addHelpText.test.js +++ b/tests/command.addHelpText.test.js @@ -181,7 +181,7 @@ describe('context checks with full parse', () => { }); test('when help requested then context.error is false', () => { - let context = {}; + let context; const program = new commander.Command(); program .exitOverride() @@ -193,7 +193,7 @@ describe('context checks with full parse', () => { }); test('when help for error then context.error is true', () => { - let context = {}; + let context; const program = new commander.Command(); program .exitOverride() @@ -206,7 +206,7 @@ describe('context checks with full parse', () => { }); test('when help on program then context.command is program', () => { - let context = {}; + let context; const program = new commander.Command(); program .exitOverride() @@ -218,7 +218,7 @@ describe('context checks with full parse', () => { }); test('when help on subcommand and "before" subcommand then context.command is subcommand', () => { - let context = {}; + let context; const program = new commander.Command(); program .exitOverride(); @@ -231,7 +231,7 @@ describe('context checks with full parse', () => { }); test('when help on subcommand and "beforeAll" on program then context.command is subcommand', () => { - let context = {}; + let context; const program = new commander.Command(); program .exitOverride() From 35e95710f34717718e9471abe6b1d6453ead616f Mon Sep 17 00:00:00 2001 From: Wee Bit Date: Fri, 4 Aug 2023 02:25:32 +0300 Subject: [PATCH 3/5] Do not use undefined long help option flag in legacy code --- lib/command.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/command.js b/lib/command.js index e014d957f..d0eaa6845 100644 --- a/lib/command.js +++ b/lib/command.js @@ -1101,7 +1101,9 @@ Expecting one of '${allowedValues.join("', '")}'`); } // Fallback to parsing the help flag to invoke the help. - return this._dispatchSubcommand(subcommandName, [], [this._helpLongFlag]); + if (this._helpLongFlag) { + return this._dispatchSubcommand(subcommandName, [], [this._helpLongFlag]); + } } /** @@ -2034,7 +2036,9 @@ Expecting one of '${allowedValues.join("', '")}'`); } context.write(helpInformation); - this.emit(this._helpLongFlag); // deprecated + if (this._helpLongFlag) { + this.emit(this._helpLongFlag); // deprecated + } this.emit('afterHelp', context); getCommandAndParents(this).forEach(command => command.emit('afterAllHelp', context)); } From a253ec6a73a209d58ea5f1d71c8c34afc65e7e2b Mon Sep 17 00:00:00 2001 From: Wee Bit Date: Sun, 13 Aug 2023 01:10:13 +0200 Subject: [PATCH 4/5] Revert "Fix version() parameter type" This reverts commit e8bea4aedd750418d094591d63699010f5c7b623. --- lib/command.js | 2 +- typings/index.d.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/command.js b/lib/command.js index d0eaa6845..25464793c 100644 --- a/lib/command.js +++ b/lib/command.js @@ -1809,7 +1809,7 @@ Expecting one of '${allowedValues.join("', '")}'`); * * You can optionally supply the flags and description to override the defaults. * - * @param {string} [str] + * @param {string} str * @param {string} [flags] * @param {string} [description] * @return {this | string} `this` command for chaining, or version string if no arguments diff --git a/typings/index.d.ts b/typings/index.d.ts index 73c606a5d..695c3bd25 100644 --- a/typings/index.d.ts +++ b/typings/index.d.ts @@ -293,7 +293,7 @@ export class Command { * * You can optionally supply the flags and description to override the defaults. */ - version(str?: string, flags?: string, description?: string): this; + version(str: string, flags?: string, description?: string): this; /** * Define a command, implemented using an action handler. From 8dd417f31b5b289945e1b84ed7c71623ece29524 Mon Sep 17 00:00:00 2001 From: Wee Bit Date: Sun, 13 Aug 2023 03:02:26 +0200 Subject: [PATCH 5/5] Fix help for commands with executable handler & only a short help flag --- lib/command.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/command.js b/lib/command.js index 25464793c..4fca7f998 100644 --- a/lib/command.js +++ b/lib/command.js @@ -1101,9 +1101,9 @@ Expecting one of '${allowedValues.join("', '")}'`); } // Fallback to parsing the help flag to invoke the help. - if (this._helpLongFlag) { - return this._dispatchSubcommand(subcommandName, [], [this._helpLongFlag]); - } + return this._dispatchSubcommand(subcommandName, [], [ + this._helpLongFlag || this._helpShortFlag + ]); } /**