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']); + }); }); });