Skip to content

Commit

Permalink
fix: always cache help message when running commands
Browse files Browse the repository at this point in the history
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
  • Loading branch information
bcoe committed Feb 15, 2021
1 parent d731f9f commit 7d5fa35
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
4 changes: 1 addition & 3 deletions lib/command.ts
Expand Up @@ -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);
Expand All @@ -348,8 +348,6 @@ export function command(
// registered, run usage's default fail method.
}
});
} else if (isPromise(innerArgv)) {
yargs.getUsageInstance().cacheHelpMessage();
}
}

Expand Down
14 changes: 14 additions & 0 deletions test/yargs.cjs
Expand Up @@ -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']);
});
});
});

0 comments on commit 7d5fa35

Please sign in to comment.