Skip to content

Commit 9aa2490

Browse files
author
Benjamin E. Coe
authored
fix: address display bug with default sub-commands (#2303)
Fixes #2291, #2247
1 parent 663c1b6 commit 9aa2490

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

lib/command.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,7 @@ export class CommandInstance {
263263
if (isCommandBuilderCallback(builder)) {
264264
// A function can be provided, which builds
265265
// up a yargs chain and possibly returns it.
266+
yargs.getInternalMethods().getUsageInstance().freeze();
266267
const builderOutput = builder(
267268
yargs.getInternalMethods().reset(aliases),
268269
helpOrVersionSet
@@ -284,6 +285,7 @@ export class CommandInstance {
284285
} else if (isCommandBuilderOptionDefinitions(builder)) {
285286
// as a short hand, an object can instead be provided, specifying
286287
// the options that a command takes.
288+
yargs.getInternalMethods().getUsageInstance().freeze();
287289
innerYargs = yargs.getInternalMethods().reset(aliases);
288290
Object.keys(commandHandler.builder).forEach(key => {
289291
innerYargs.option(key, builder[key]);

test/usage.cjs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3991,6 +3991,57 @@ describe('usage tests', () => {
39913991
' --arg2 arg2 desc [string]',
39923992
]);
39933993
});
3994+
3995+
// See: https://github.com/yargs/yargs/issues/2291
3996+
it('should display help output for nested default command on failure', () => {
3997+
const r = checkUsage(() =>
3998+
yargs()
3999+
.command(
4000+
'root',
4001+
'root command',
4002+
y => {
4003+
y.command({
4004+
command: 'nested',
4005+
desc: 'nested command',
4006+
builder: y => {
4007+
y.command({
4008+
command: 'deep-a',
4009+
aliases: ['$0'],
4010+
desc: 'deeply nested default',
4011+
builder: noop,
4012+
handler: noop,
4013+
}).command({
4014+
command: 'deep-b',
4015+
desc: 'a deeply nested command',
4016+
builder: noop,
4017+
handler: noop,
4018+
});
4019+
},
4020+
handler: noop,
4021+
});
4022+
},
4023+
() => {}
4024+
)
4025+
.strict()
4026+
.demandCommand()
4027+
.parse(['root', 'nested', 'bloop'])
4028+
);
4029+
r.errors[0]
4030+
.split('\n')
4031+
.should.deep.equal([
4032+
'usage root nested',
4033+
'',
4034+
'nested command',
4035+
'',
4036+
'Commands:',
4037+
' usage root nested deep-a deeply nested default [default]',
4038+
' usage root nested deep-b a deeply nested command',
4039+
'',
4040+
'Options:',
4041+
' --help Show help [boolean]',
4042+
' --version Show version number [boolean]',
4043+
]);
4044+
});
39944045
});
39954046

39964047
describe('positional', () => {

0 commit comments

Comments
 (0)