Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: address display bug with default sub-commands #2303

Merged
merged 2 commits into from Feb 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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