Skip to content

Commit

Permalink
fix: address display bug with default sub-commands (#2303)
Browse files Browse the repository at this point in the history
Fixes #2291, #2247
  • Loading branch information
bcoe committed Feb 21, 2023
1 parent 663c1b6 commit 9aa2490
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/command.ts
Expand Up @@ -263,6 +263,7 @@ export class CommandInstance {
if (isCommandBuilderCallback(builder)) {
// A function can be provided, which builds
// up a yargs chain and possibly returns it.
yargs.getInternalMethods().getUsageInstance().freeze();
const builderOutput = builder(
yargs.getInternalMethods().reset(aliases),
helpOrVersionSet
Expand All @@ -284,6 +285,7 @@ export class CommandInstance {
} else if (isCommandBuilderOptionDefinitions(builder)) {
// as a short hand, an object can instead be provided, specifying
// the options that a command takes.
yargs.getInternalMethods().getUsageInstance().freeze();
innerYargs = yargs.getInternalMethods().reset(aliases);
Object.keys(commandHandler.builder).forEach(key => {
innerYargs.option(key, builder[key]);
Expand Down
51 changes: 51 additions & 0 deletions test/usage.cjs
Expand Up @@ -3991,6 +3991,57 @@ describe('usage tests', () => {
' --arg2 arg2 desc [string]',
]);
});

// See: https://github.com/yargs/yargs/issues/2291
it('should display help output for nested default command on failure', () => {
const r = checkUsage(() =>
yargs()
.command(
'root',
'root command',
y => {
y.command({
command: 'nested',
desc: 'nested command',
builder: y => {
y.command({
command: 'deep-a',
aliases: ['$0'],
desc: 'deeply nested default',
builder: noop,
handler: noop,
}).command({
command: 'deep-b',
desc: 'a deeply nested command',
builder: noop,
handler: noop,
});
},
handler: noop,
});
},
() => {}
)
.strict()
.demandCommand()
.parse(['root', 'nested', 'bloop'])
);
r.errors[0]
.split('\n')
.should.deep.equal([
'usage root nested',
'',
'nested command',
'',
'Commands:',
' usage root nested deep-a deeply nested default [default]',
' usage root nested deep-b a deeply nested command',
'',
'Options:',
' --help Show help [boolean]',
' --version Show version number [boolean]',
]);
});
});

describe('positional', () => {
Expand Down

0 comments on commit 9aa2490

Please sign in to comment.