fix: Show message when showHelpOnFail is chained globally#2154
fix: Show message when showHelpOnFail is chained globally#2154bcoe merged 4 commits intoyargs:mainfrom jly36963:showHelpOnFail-global-usage
Conversation
lib/usage.ts
Outdated
|
|
||
| // If global context, set globalFailMessage | ||
| // Addresses: https://github.com/yargs/yargs/issues/2085 | ||
| if (yargs.getInternalMethods().getContext().commands.length === 0) { |
There was a problem hiding this comment.
Is there an easier way to figure out that this is in the global context? (I can move this logic to a helper function and call it here)
There was a problem hiding this comment.
I like the idea of pulling this into a private helper method, then if we can think of a better way to detect we're in the global context we can swap out the logic.
lib/usage.ts
Outdated
|
|
||
| // If global context, set globalFailMessage | ||
| // Addresses: https://github.com/yargs/yargs/issues/2085 | ||
| if (yargs.getInternalMethods().getContext().commands.length === 0) { |
There was a problem hiding this comment.
I like the idea of pulling this into a private helper method, then if we can think of a better way to detect we're in the global context we can swap out the logic.
|
LGTM, with a suggested refactor. I would be surprised if we're not already detecting the global context, vs., a command handler context somewhere else in the codebase. Perhaps look in |
|
I realized that checking |
bcoe
left a comment
There was a problem hiding this comment.
As always, thank you for the contributions.
Addresses: #2085
Problem
When
showHelpOnFailis used globally, it doesn't work.This is because
usage.reset()sets failMessage to null.Solution
I added a
globalFailMessagevariable to capture the global mesage.On fail,
failMessage || globalFailMessageis printed. I figured if both are provided, the more specific message should be shown.Other
I saw that
null | undefinedwas used a lot, so I made typenil. (Idea borrowed from lodash isNil)