-
Notifications
You must be signed in to change notification settings - Fork 70
Fix: Handling of --help arg for sub commands #1190
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
Conversation
…gs, ex file names "h"
a263401 to
794e0b6
Compare
|
Hey @bobheadxi or @keegancsmith, I'd like this to make it into the 6.10 release on Wednesday, if either of you has a moment to review, or lmk who else I should ask 🙏 |
keegancsmith
left a comment
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.
This is extremely complicated compared to what we had before. Looking at the code in src-cli it uses go's built in 'flag' apis. That only supports -h. If you do something like
src snapshot upload -h
it works correctly. The problem comes in is that we added in extra support for --help and reading that code you can see that is naively only supports the first level of subcommands, not deeper ones like src snapshot upload --help. Fixing that is the proper fix here.
I know you want to get this in, so I sent out this alternative fix. Mind testing it and seeing if it is acceptable? It doesn't exactly match all the use cases you mentioned, but should solve the main complaint you have. #1195
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| tempDir := t.TempDir() | ||
| cmd := exec.Command(os.Args[0], "-test.run=^TestCommander_Run_HelpFlag$") |
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.
the pattern in this file of using subprocesses is very complicated for what you want to check. I'm guessing you did it because of the os.Exit in the code under test. Rather just make os.Exit something you can inject?
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.
I'm not aware of the difference, but Amp had picked this route so that it could validate the exit code.
src snapshot upload -h would return 0, whereas src snapshot upload would return 2 (input error).
Validating the return code is not the most material change, so if it's more complex than it's worth, it's fine to remove.
| // Handle any errors returned | ||
| if err := cmd.handler(args); err != nil { | ||
|
|
||
| // If the returned error is of type UsageError |
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.
note: the introduction of all these comments does not make the code more readable and makes the PR much harder to review.
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.
Noted. I felt the code was rather cryptic to read.
|
Alternative was merged in. |
Usage / helper text was only printed for either
src(top-level) orsrc <command>(command-level), ignoring any of the usage functions defined by subcommands.For example,
src snapshot upload --helpwould only output the usageFunc forsrc snapshotWith this change:
Any of the following will output the usageFunc for
src snapshot uploadAny of the following will output the usageFunc for
src snapshotAny of the following will output the usageFunc for
srcsrc --help src -h src helpTest plan