From 31c783176f0c1ebbf451b6229b1f97e3429c5a71 Mon Sep 17 00:00:00 2001 From: Landon Yarrington Date: Mon, 27 Dec 2021 12:47:05 -0700 Subject: [PATCH 1/3] fix: failed command is missing argument descriptions and optional arguments --- test/usage.cjs | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/test/usage.cjs b/test/usage.cjs index eef18facc..09031546e 100644 --- a/test/usage.cjs +++ b/test/usage.cjs @@ -3840,6 +3840,45 @@ describe('usage tests', () => { ' --uuid [required]', ]); }); + + // Addresses: https://github.com/yargs/yargs/issues/2030 + it('should display options (with descriptions) on failed default command', () => { + const r = checkUsage(() => + yargs('') + .command({ + command: '$0 ', + desc: 'default desc', + builder: yargs => + yargs + .option('arg1', { + type: 'string', + desc: 'arg1 desc', + demandOption: true, + }) + .option('arg2', { + type: 'string', + desc: 'arg2 desc', + }), + handler: noop, + }) + .strict() + .parse() + ); + console.log(r); + r.errors[0] + .split('\n') + .should.deep.equal([ + 'usage ', + '', + 'default desc', + '', + 'Options:', + ' --help Show help [boolean]', + ' --version Show version number [boolean]', + ' --arg1 arg1 desc (default command) [string] [required]', + ' --arg2 arg2 desc (default command) [string]', + ]); + }); }); describe('positional', () => { From 0a298baf9a8a18491fd142ae57b9859f51c26122 Mon Sep 17 00:00:00 2001 From: Landon Yarrington Date: Mon, 27 Dec 2021 12:53:14 -0700 Subject: [PATCH 2/3] fixes --- test/usage.cjs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/usage.cjs b/test/usage.cjs index 09031546e..9a4505e05 100644 --- a/test/usage.cjs +++ b/test/usage.cjs @@ -3875,8 +3875,8 @@ describe('usage tests', () => { 'Options:', ' --help Show help [boolean]', ' --version Show version number [boolean]', - ' --arg1 arg1 desc (default command) [string] [required]', - ' --arg2 arg2 desc (default command) [string]', + ' --arg1 arg1 desc [string] [required]', + ' --arg2 arg2 desc [string]', ]); }); }); From 9dff0ceb344a026158ce8c55808fcf5a3f74f5fe Mon Sep 17 00:00:00 2001 From: Landon Yarrington Date: Sun, 2 Jan 2022 14:39:02 -0700 Subject: [PATCH 3/3] fixes --- lib/command.ts | 2 +- lib/usage.ts | 33 +++++++++++++++++++++------------ 2 files changed, 22 insertions(+), 13 deletions(-) diff --git a/lib/command.ts b/lib/command.ts index 19d8053c6..b588b7e40 100644 --- a/lib/command.ts +++ b/lib/command.ts @@ -312,7 +312,7 @@ export class CommandInstance { // if this is the case, we should show the root usage instructions // rather than the usage instructions for the nested default command: if (isDefaultCommand) - innerYargs.getInternalMethods().getUsageInstance().unfreeze(); + innerYargs.getInternalMethods().getUsageInstance().unfreeze(true); if (this.shouldUpdateUsage(innerYargs)) { innerYargs .getInternalMethods() diff --git a/lib/usage.ts b/lib/usage.ts index 9374d2d16..cea2f7c22 100644 --- a/lib/usage.ts +++ b/lib/usage.ts @@ -707,22 +707,31 @@ export function usage(yargs: YargsInstance, shim: PlatformShim) { descriptions, }); }; - self.unfreeze = function unfreeze() { + self.unfreeze = function unfreeze(defaultCommand = false) { const frozen = frozens.pop(); // In the case of running a defaultCommand, we reset // usage early to ensure we receive the top level instructions. // unfreezing again should just be a noop: if (!frozen) return; - ({ - failMessage, - failureOutput, - usages, - usageDisabled, - epilogs, - examples, - commands, - descriptions, - } = frozen); + // Addresses: https://github.com/yargs/yargs/issues/2030 + if (defaultCommand) { + descriptions = {...frozen.descriptions, ...descriptions}; + commands = [...frozen.commands, ...commands]; + usages = [...frozen.usages, ...usages]; + examples = [...frozen.examples, ...examples]; + epilogs = [...frozen.epilogs, ...epilogs]; + } else { + ({ + failMessage, + failureOutput, + usages, + usageDisabled, + epilogs, + examples, + commands, + descriptions, + } = frozen); + } }; return self; @@ -759,7 +768,7 @@ export interface UsageInstance { showHelpOnFail(enabled?: boolean | string, message?: string): UsageInstance; showVersion(level?: 'error' | 'log' | ((message: string) => void)): void; stringifiedValues(values?: any[], separator?: string): string; - unfreeze(): void; + unfreeze(defaultCommand?: boolean): void; usage(msg: string | null, description?: string | false): UsageInstance; version(ver: any): void; wrap(cols: number | null | undefined): void;