-
-
Notifications
You must be signed in to change notification settings - Fork 8
script: Add tab completion for flags and arguments #57
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
629cd66
to
714c023
Compare
714c023
to
485c985
Compare
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.
@dylandreimerink Very cool!! a few suggestions feel free to ignore...
|
||
state.suggestions = nil | ||
for _, suggestion := range argsCB(engineState, nil, "") { | ||
state.suggestions = append(state.suggestions, line[:state.pos]+suggestion) |
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 think it might be a good idea to actually add behavior on the autocompletestate
. Perhaps this would allow us to make sure adequate bound checks are in placed in a central location.
Not to mention we can test the various inputs.
Speaking of which might be great to add some tests here to ensure we produce the correct suggestions under various conditions.
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.
Yes. Agreed that would be a good idea. I will as this in a followup PR to ensure we don't regress this on accident.
This commit adds tab completion for the shell. When in the REPL the <tab> key can be used to give suggestions. When typing the initial command it will suggest commands starting with the typed characters. Repeated presses of the <tab> key will cycle through the suggestions. When pressing <tab> while in an argument or flag value, a callback on the command is called if it exists. This callback can return a slice of values to suggest. For arguments, all arguments up and including the current argument are passed to the callback. For flags, the flag name and value typed so far are passed to the callback. To accommodate the "remote shell" used by Cilium, a hidden flag is now implicitly added to all commands `--autocomplete`. When a command is called with this flag the engine will intercept the command and return a line separated list of suggestions instead of executing the command. For example: `test --autocomplete='arg1 --flag1=val1 ar'` will be interpreted the same as `test arg1 --flag1=val1 ar` would on the REPL and the output will be a list of suggestions: ``` > test --autocomplete='arg1 --flag1=val1 ar' test arg1 --flag1=val1 argument2 test arg1 --flag1=val1 arg2 test arg1 --flag1=val1 args2 ``` The `help` command also has a hidden flag `-a` which if passed will simply list all available commands without additional help text. ``` > help -a cat cd chmod ``` This should allow the remote shell in Cilium to provide tab completion in the same way as the REPL. Signed-off-by: Dylan Reimerink <dylan.reimerink@isovalent.com>
485c985
to
d159142
Compare
This commit adds tab completion for the shell. When in the REPL the key can be used to give suggestions. When typing the initial command it will suggest commands starting with the typed characters. Repeated presses of the key will cycle through the suggestions.
When pressing while in an argument or flag value, a callback on the command is called if it exists. This callback can return a slice of values to suggest. For arguments, all arguments up and including the current argument are passed to the callback. For flags, the flag name and value typed so far are passed to the callback.
To accommodate the "remote shell" used by Cilium, a hidden flag is now implicitly added to all commands
--autocomplete
. When a command is called with this flag the engine will intercept the command and return a line separated list of suggestions instead of executing the command. For example:test --autocomplete='arg1 --flag1=val1 ar'
will be interpreted the same astest arg1 --flag1=val1 ar
would on the REPL and the output will be a list of suggestions:The
help
command also has a hidden flag-a
which if passed will simply list all available commands without additional help text.This should allow the remote shell in Cilium to provide tab completion in the same way as the REPL.
Fixes: #54