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
.showHelp() and .getHelp() now return same output for commands as --help #1826
Conversation
@OsmanAltun 👏 thank you, I've been traveling recently, but will give you review soon. |
@OsmanAltun this would have perhaps been an alternate approach? But I believe the problem was addressed as I started refactoring how help messages were cached, which was necessary to implement |
Ohkay, I get it now. |
@OsmanAltun I was looking at this implementation, and I'm a little concerned by the additional complexity that registering commands with I was wondering if instead we could update the helper
☝️ couldn't we figure out how the display the proper usage behavior here, if a default command is registered? |
The helper function is (as far as I know) only responsible for the first line in the usage output, which is the command call order. If u add all commands here, it would display between the description and the command call order. Besides, to add the commands back, u would still have to loop over every command using The best solution might be to make sure the EDIT: |
@OsmanAltun I want to encourage external contributions to yargs, and I feel bad for keeping this issue open for so long. To get patches landed quickly, it's best if:
If you provide a test, and a clear description of what you're fixing, I will help guide you towards the part of the codebase where I think we can best fix things. Very much appreciate you taking your time to make a contribution. Edit: reading through issues yesterday, I think I got a clearer picture of what you're aiming to fix in this PR 👍. I think my comment below should point you in the right direction. Although, I believe #1820 to be a slightly different issue (we'll probably want to dig into, and fix in a separate PR). |
@OsmanAltun I did some digging this afternoon, and think I see a simpler patch: diff --git a/lib/yargs-factory.ts b/lib/yargs-factory.ts
index 57d1295..69f6308 100644
--- a/lib/yargs-factory.ts
+++ b/lib/yargs-factory.ts
@@ -1550,10 +1550,11 @@ function Yargs(
const handlerKeys = command.getCommands();
const requestCompletions = completion!.completionKey in argv;
- const skipRecommendation = argv[helpOpt!] || requestCompletions;
+ const skipRecommendation =
+ argv[helpOpt!] || requestCompletions || helpOnly;
const skipDefaultCommand =
skipRecommendation &&
- (handlerKeys.length > 1 || handlerKeys[0] !== '$0');
+ (handlerKeys.length > 1 || !command.hasDefaultCommand());
if (argv._.length) {
if (handlerKeys.length) { The underlying issue seems to be that we were calling the default command when we shouldn't have. The default command should not be called when:
The default command should be called when:
Do you want to run with this patch I've shared? The work that needs to be done is adding the following test cases:
|
Great job, everything works as expected for me now. One small question though. if (isCommandBuilderCallback(builder)) {
// logic
} else if (isCommandBuilderOptionDefinitions(builder)) {
// freeze()
// reset()
// unfreeze()
// similar logic to first block
} Should freeze/unfreeze not also be applied in the first block? |
This reverts commit 380782d.
I'm betting we can pull the logic right to the top of the function, and always If we're running the default command:
|
I misread, but yeah, having everything on the root of the function seems better. Right now, I am skipping the let innerYargs: YargsInstance;
if (isCommandBuilderCallback(builder)) {
const builderOutput = builder(yargs.reset(parsed.aliases));
innerYargs = isYargsInstance(builderOutput) ? builderOutput : yargs;
} else {
innerYargs = yargs.reset(parsed.aliases);
Object.keys(commandHandler.builder).forEach(key => {
innerYargs.option(key, builder[key]);
});
} instead of } else if (isCommandBuilderOptionDefinitions(builder)) { I have no idea if this is a problem, all tests work, so I am guessing it's ok? |
I'm confused as to why this code needs to be removed as part of the change. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@OsmanAltun I pushed a couple updates to tests, and tweaked the implementation a bit.
Could you try it out for your use case and make sure I haven't broken it?
I just checked and the tests don't fail, however the sample code I use for testing fails. I realized this is caused by using yargs.argv instead of yargs.parse(). The tests don't take this into consideration. Adding freeze() fixes this. I assumed that you forgot to add freeze(), since the code snippet you sent didn't work at first. The code snippet I use for testing: const argv = yargs.usage('This is my awesome program')
.commands([
{ command: '*', description: 'testing 123', handler: () => {}},
{ command: 'foo', description: 'testing 1932', handler: () => {}}
]).argv;
yargs.showHelp();
console.log(argv); |
Good catch. The problem is that To fix things I switched to making |
Alright, I think everything works as expected now. Go ahead and merge it 👍🏼 |
Closes issues: