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: failed command usage string is missing arg descriptions and optional args #2105

Merged
merged 4 commits into from Jan 9, 2022
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: 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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like that you just made this a boolean setting rather than adding a new method.

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