Skip to content

Commit

Permalink
fix: failed command usage string is missing arg descriptions and opti…
Browse files Browse the repository at this point in the history
…onal args (#2105)
  • Loading branch information
jly36963 committed Jan 9, 2022
1 parent bfc7e41 commit d6e342d
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 13 deletions.
2 changes: 1 addition & 1 deletion lib/command.ts
Expand Up @@ -312,7 +312,7 @@ export class CommandInstance {
// if this is the case, we should show the root usage instructions
// rather than the usage instructions for the nested default command:
if (isDefaultCommand)
innerYargs.getInternalMethods().getUsageInstance().unfreeze();
innerYargs.getInternalMethods().getUsageInstance().unfreeze(true);
if (this.shouldUpdateUsage(innerYargs)) {
innerYargs
.getInternalMethods()
Expand Down
33 changes: 21 additions & 12 deletions lib/usage.ts
Expand Up @@ -707,22 +707,31 @@ export function usage(yargs: YargsInstance, shim: PlatformShim) {
descriptions,
});
};
self.unfreeze = function unfreeze() {
self.unfreeze = function unfreeze(defaultCommand = false) {
const frozen = frozens.pop();
// In the case of running a defaultCommand, we reset
// usage early to ensure we receive the top level instructions.
// unfreezing again should just be a noop:
if (!frozen) return;
({
failMessage,
failureOutput,
usages,
usageDisabled,
epilogs,
examples,
commands,
descriptions,
} = frozen);
// Addresses: https://github.com/yargs/yargs/issues/2030
if (defaultCommand) {
descriptions = {...frozen.descriptions, ...descriptions};
commands = [...frozen.commands, ...commands];
usages = [...frozen.usages, ...usages];
examples = [...frozen.examples, ...examples];
epilogs = [...frozen.epilogs, ...epilogs];
} else {
({
failMessage,
failureOutput,
usages,
usageDisabled,
epilogs,
examples,
commands,
descriptions,
} = frozen);
}
};

return self;
Expand Down Expand Up @@ -759,7 +768,7 @@ export interface UsageInstance {
showHelpOnFail(enabled?: boolean | string, message?: string): UsageInstance;
showVersion(level?: 'error' | 'log' | ((message: string) => void)): void;
stringifiedValues(values?: any[], separator?: string): string;
unfreeze(): void;
unfreeze(defaultCommand?: boolean): void;
usage(msg: string | null, description?: string | false): UsageInstance;
version(ver: any): void;
wrap(cols: number | null | undefined): void;
Expand Down
39 changes: 39 additions & 0 deletions test/usage.cjs
Expand Up @@ -3861,6 +3861,45 @@ describe('usage tests', () => {
' --uuid [required]',
]);
});

// Addresses: https://github.com/yargs/yargs/issues/2030
it('should display options (with descriptions) on failed default command', () => {
const r = checkUsage(() =>
yargs('')
.command({
command: '$0 <arg1>',
desc: 'default desc',
builder: yargs =>
yargs
.option('arg1', {
type: 'string',
desc: 'arg1 desc',
demandOption: true,
})
.option('arg2', {
type: 'string',
desc: 'arg2 desc',
}),
handler: noop,
})
.strict()
.parse()
);
console.log(r);
r.errors[0]
.split('\n')
.should.deep.equal([
'usage <arg1>',
'',
'default desc',
'',
'Options:',
' --help Show help [boolean]',
' --version Show version number [boolean]',
' --arg1 arg1 desc [string] [required]',
' --arg2 arg2 desc [string]',
]);
});
});

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

0 comments on commit d6e342d

Please sign in to comment.