From d57ca7751d533d7e0f216cd9fbf7c2b0ec98f791 Mon Sep 17 00:00:00 2001 From: "Benjamin E. Coe" Date: Mon, 15 Feb 2021 13:23:38 -0800 Subject: [PATCH] fix: always cache help message when running commands (#1865) Prior to this fix, we only cached the help message for commands when using async commands. I believe we always want to cache the help message for cases like #1853. Fixes #1853 --- lib/command.ts | 4 +--- test/yargs.cjs | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/lib/command.ts b/lib/command.ts index c21c417e3..3509c3574 100644 --- a/lib/command.ts +++ b/lib/command.ts @@ -338,8 +338,8 @@ export function command( } } + yargs.getUsageInstance().cacheHelpMessage(); if (isPromise(innerArgv) && !yargs._hasParseCallback()) { - yargs.getUsageInstance().cacheHelpMessage(); innerArgv.catch(error => { try { yargs.getUsageInstance().fail(null, error); @@ -348,8 +348,6 @@ export function command( // registered, run usage's default fail method. } }); - } else if (isPromise(innerArgv)) { - yargs.getUsageInstance().cacheHelpMessage(); } } diff --git a/test/yargs.cjs b/test/yargs.cjs index a9f3844fa..bec35d5c3 100644 --- a/test/yargs.cjs +++ b/test/yargs.cjs @@ -3065,5 +3065,19 @@ describe('yargs dsl tests', () => { commandCalled.should.equal(false); middlewareCalled.should.equal(false); }); + // Refs: #1853 + it('should use cached help message for nested synchronous commands', async () => { + const y = yargs('object').command( + 'object', + 'object command', + (yargs) => { + yargs.command('get', 'get command'); + } + ); + const argv = y.argv; + const help = (await y.getHelp()); + help.should.match(/node object get/); + argv._.should.eql(['object']); + }); }); });