CDK command shall have autocomplete feature
When using command line (bash, zsh, etc.) autocomplete should show available options.
Use Case
It would be really useful for users as they won't have to memorize all options.
Proposed Solution
On pressing tab it should either autocomplete partially types options or show all available options.
Other
- [ ] :wave: I may be able to implement this feature request
- [ ] :warning: This feature might incur a breaking change
This is a :rocket: Feature Request
Quick and dirty version in bash: https://gist.github.com/kirtfitzpatrick/e7a7828e99bae609955f08b35fc2c8b1
Is there a plan to implement this in near future?
This issue has received a significant amount of attention so we are automatically upgrading its priority. A member of the community will see the re-prioritization and provide an update on the issue.
I'm sure there are nuances to this, but yargs (which powers the cli) does already provide a baseline for this. Might need some extra tweaking.
See https://yargs.js.org/docs/#api-reference-completioncmd-description-fn
Turns out the completion feature is already available, just not the ability to get the required script for your .bashrc / .zshrc
For zsh:
###-begin-cdk-completions-###
_cdk_yargs_completions()
{
local reply
local si=$IFS
IFS=$'
' reply=($(COMP_CWORD="$((CURRENT-1))" COMP_LINE="$BUFFER" COMP_POINT="$CURSOR" cdk --get-yargs-completions "${words[@]}"))
IFS=$si
_describe 'values' reply
}
compdef _cdk_yargs_completions cdk
###-end-cdk-completions-###
For bash:
###-begin-cdk-completions-###
_cdk_yargs_completions()
{
local cur_word args type_list
cur_word="${COMP_WORDS[COMP_CWORD]}"
args=("${COMP_WORDS[@]}")
# ask yargs to generate completions.
type_list=$(cdk --get-yargs-completions "${args[@]}")
COMPREPLY=( $(compgen -W "${type_list}" -- ${cur_word}) )
# if no match was found, fall back to filename completion
if [ ${#COMPREPLY[@]} -eq 0 ]; then
COMPREPLY=()
fi
return 0
}
complete -o default -F _cdk_yargs_completions cdk
###-end-cdk-completions-###
Now the tricky part is which CDK binary to use.
- If you have installed
cdkglobally (which is not recommended) the above will just work. - If you are running
cdkinside a project we need to do some extra work. I'll assume you'll be usingnpxfor this, since it has a lot of the auto detection already build in.
Option A: Wrapper function
Add this to your .zshrc or .bashrc:
cdk() { npx -- cdk "$@" }
Simple and portable. Can easily be adjusted to use any other command (e.g. yarn).
Option B: Alias & forward npx completions (zsh only)
npx currently does not have official completions. So we can "forward" all completions to the actual command:
alias cdk="npx cdk"
compdef _precommand npx
However this won't work with anything else but npx or if you have custom npx completions setup.
Option C: Alias & completealiases (zsh only)
Zsh has a feature that will enable completions for aliases, however it might break other things.
Add this to your .zshrc:
alias cdk="npx cdk"
setopt completealiases
Thank you for submitting this feature request! After closer examination of the issue by CDK product and development team, there are no plans to implement this in the near feature. Luckily, this feature is already available, it just requires some environment configuration. I am moving this to a GitHub discussion so the information in this thread can continue to be accessible.