Skip to content

Commit d6e342d

Browse files
authoredJan 9, 2022
fix: failed command usage string is missing arg descriptions and optional args (#2105)
1 parent bfc7e41 commit d6e342d

File tree

3 files changed

+61
-13
lines changed

3 files changed

+61
-13
lines changed
 

‎lib/command.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ export class CommandInstance {
312312
// if this is the case, we should show the root usage instructions
313313
// rather than the usage instructions for the nested default command:
314314
if (isDefaultCommand)
315-
innerYargs.getInternalMethods().getUsageInstance().unfreeze();
315+
innerYargs.getInternalMethods().getUsageInstance().unfreeze(true);
316316
if (this.shouldUpdateUsage(innerYargs)) {
317317
innerYargs
318318
.getInternalMethods()

‎lib/usage.ts

+21-12
Original file line numberDiff line numberDiff line change
@@ -707,22 +707,31 @@ export function usage(yargs: YargsInstance, shim: PlatformShim) {
707707
descriptions,
708708
});
709709
};
710-
self.unfreeze = function unfreeze() {
710+
self.unfreeze = function unfreeze(defaultCommand = false) {
711711
const frozen = frozens.pop();
712712
// In the case of running a defaultCommand, we reset
713713
// usage early to ensure we receive the top level instructions.
714714
// unfreezing again should just be a noop:
715715
if (!frozen) return;
716-
({
717-
failMessage,
718-
failureOutput,
719-
usages,
720-
usageDisabled,
721-
epilogs,
722-
examples,
723-
commands,
724-
descriptions,
725-
} = frozen);
716+
// Addresses: https://github.com/yargs/yargs/issues/2030
717+
if (defaultCommand) {
718+
descriptions = {...frozen.descriptions, ...descriptions};
719+
commands = [...frozen.commands, ...commands];
720+
usages = [...frozen.usages, ...usages];
721+
examples = [...frozen.examples, ...examples];
722+
epilogs = [...frozen.epilogs, ...epilogs];
723+
} else {
724+
({
725+
failMessage,
726+
failureOutput,
727+
usages,
728+
usageDisabled,
729+
epilogs,
730+
examples,
731+
commands,
732+
descriptions,
733+
} = frozen);
734+
}
726735
};
727736

728737
return self;
@@ -759,7 +768,7 @@ export interface UsageInstance {
759768
showHelpOnFail(enabled?: boolean | string, message?: string): UsageInstance;
760769
showVersion(level?: 'error' | 'log' | ((message: string) => void)): void;
761770
stringifiedValues(values?: any[], separator?: string): string;
762-
unfreeze(): void;
771+
unfreeze(defaultCommand?: boolean): void;
763772
usage(msg: string | null, description?: string | false): UsageInstance;
764773
version(ver: any): void;
765774
wrap(cols: number | null | undefined): void;

‎test/usage.cjs

+39
Original file line numberDiff line numberDiff line change
@@ -3861,6 +3861,45 @@ describe('usage tests', () => {
38613861
' --uuid [required]',
38623862
]);
38633863
});
3864+
3865+
// Addresses: https://github.com/yargs/yargs/issues/2030
3866+
it('should display options (with descriptions) on failed default command', () => {
3867+
const r = checkUsage(() =>
3868+
yargs('')
3869+
.command({
3870+
command: '$0 <arg1>',
3871+
desc: 'default desc',
3872+
builder: yargs =>
3873+
yargs
3874+
.option('arg1', {
3875+
type: 'string',
3876+
desc: 'arg1 desc',
3877+
demandOption: true,
3878+
})
3879+
.option('arg2', {
3880+
type: 'string',
3881+
desc: 'arg2 desc',
3882+
}),
3883+
handler: noop,
3884+
})
3885+
.strict()
3886+
.parse()
3887+
);
3888+
console.log(r);
3889+
r.errors[0]
3890+
.split('\n')
3891+
.should.deep.equal([
3892+
'usage <arg1>',
3893+
'',
3894+
'default desc',
3895+
'',
3896+
'Options:',
3897+
' --help Show help [boolean]',
3898+
' --version Show version number [boolean]',
3899+
' --arg1 arg1 desc [string] [required]',
3900+
' --arg2 arg2 desc [string]',
3901+
]);
3902+
});
38643903
});
38653904

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

0 commit comments

Comments
 (0)
Please sign in to comment.