-
Notifications
You must be signed in to change notification settings - Fork 132
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
Turn off optparse backtracking to show help for the current command #1278
Conversation
I always had an issue with all of this - the context is:
So I'd like to end up in a place where:
The concrete scenarios would be:
I have the suspicion that none of these scenarios is possible 🥲 But thought I'd bring this up with you in case you have any idea how to go at it |
That's not how it works currently. With the current parser structure you can't put global args wherever - only after the command name. So you can do
We don't have to find a way. If we go back to global options and keep backtracking, the global options will be parseable anywhere. That's what backtracking is for. It enables parsing of global options after the command name. But then issue #1146 will remain. The only way to fix #1146 is to disable backtracking. Backtracking is what makes the parser "forget" the current command after it's been parsed satisfactorily.
I think this might be possible by hijacking how the help is printed. Not sure though. Also, these limitations ultimately stem from the particular way |
Good thread, thanks for the details. You're right that we need this to fix #1146, so I'll merge this now and we'll eventually figure out the rest |
Description of the change
Fixes #1146
This is kind of by design for
optparse
, even though it ends up being nonsensical.This happens for commands that don't have any non-optional options. When the parser parses such command and encounters a mistyped option, the command parsing has already succeeded. It doesn't need any options, so it succeeds as soon as its name is parsed. And then the parser tries to parse remaining options as "global". They still fail, but by that time we're already back in "global" context. The command parsing is over. So
optparse
shows global help.This behavior - trying to parse remaining options as "global" - is called "backtracking", and it's governed by an option. Turning backtracking off has a subtle, but not insignificant effect. It manifests for programs that have truly "global" options as well as commands.
Suppose you have a parser like this:
So you can run your program as
prog --foo bar command
, which will result in{ command: ..., anotherOption: "bar" }
.With backtracking enabled, you can also run
prog command --foo bar
. The--foo
option will be parsed after the command.But with backtracking disabled you can't. Supplying
--foo
aftercommand
will be an error, and you will be shown help for the command.Fortunately, Spago's parser is setup in a way that doesn't have any global options. What we call
GlobalArgs
are actually options of every individual command fromoptparse
's point of view. And this means that we don't care for backtracking, so it can be safely turned off. Which is what this PR does.Checklist:
[ ] Added some example of the new feature to theREADME