aws-cdk icon indicating copy to clipboard operation
aws-cdk copied to clipboard

CDK command shall have autocomplete feature

Open raakasf opened this issue 7 years ago • 6 comments

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

raakasf avatar Nov 26 '19 19:11 raakasf

Quick and dirty version in bash: https://gist.github.com/kirtfitzpatrick/e7a7828e99bae609955f08b35fc2c8b1

kirtfitzpatrick avatar May 09 '20 20:05 kirtfitzpatrick

Is there a plan to implement this in near future?

moby04 avatar Nov 20 '20 08:11 moby04

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.

github-actions[bot] avatar Jul 07 '22 23:07 github-actions[bot]

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

mrgrain avatar Jul 19 '22 14:07 mrgrain

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 cdk globally (which is not recommended) the above will just work.
  • If you are running cdk inside a project we need to do some extra work. I'll assume you'll be using npx for 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

mrgrain avatar Jul 20 '22 12:07 mrgrain

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.

madeline-k avatar Feb 28 '23 19:02 madeline-k