-
Notifications
You must be signed in to change notification settings - Fork 72
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
Autocompletion for multiple positional arguments when flags are present #146
Comments
Hi Scott, |
I was hoping you might since I couldn't see any other way either 😅 I've already solved it and the workaround isn't so bad so I'm fine regardless. Feel free to close this if you want. |
I ran into troubles when implementing autocompletion for multiple positional arguments if flags are present.
Scenario:
In order to implement autocompletion for both position arguments, you need to check how many args have already been completed (via
args.Completed
). Here's an example from terraform (and the implementation).And here's another example from my own CLI package.
These work fine when no flags are present, but it breaks down if there's flags because those flags will be included in
args.Completed
which means you can't naively just check the length of the array.Example:
Solution
I think I've been able to fix this by passing in the
flag.FlagSet
and parsing the args on every prediction. Then instead of checking the length ofargs.Completed
, you use the length of parsed positional args. However, this adds a lot of complexity and seemed non-intuitive. Here's my solution so far: https://github.com/roots/trellis-cli/blob/dd2156bf2c923978ef3866c2b00517d903553cc8/trellis/complete.go#L38-L55Since args and flags are handled separately in
complete.Command
, I'd kind of expect that the predict function just for args wouldn't include the flags. Or if it did (because that might be useful in case positional args depend on flags?),complete.Args
would at least handle this automatically and provide both separately?Idea
This would be a breaking change, but could this package move completed flags out of
args.Completed
and intoargs.FlagsCompleted
? Or maybePredict
could be called like this for arguments:Though that seems much more breaking. I'm probably missing other solutions as well though 😅
The text was updated successfully, but these errors were encountered: