Skip to content
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

rush: bash completion? #2343

Closed
1 of 2 tasks
btilford opened this issue Nov 8, 2020 · 5 comments
Closed
1 of 2 tasks

rush: bash completion? #2343

btilford opened this issue Nov 8, 2020 · 5 comments

Comments

@btilford
Copy link

btilford commented Nov 8, 2020

Please prefix the issue title with the project name i.e. [rush], [api-extractor] etc.

Is this a feature or a bug?

  • Feature
  • Bug

I've been using this script for bash completion for a while. It uses the --help arg to parse available commands and supported args. It would be nice if this were built in though.

#!/usr/bin/env bash
# shellcheck disable=SC2207

__rush_command() {
  ##############################################################################
  # rush --help
  # Lists commands:
  # * indented 4 spaces
  ##############################################################################
  local subcmds
  subcmds="$(rush -h | grep -e "^\s\s\s\s\w" | awk '{print $1}')"
  COMPREPLY+=($(compgen -W "$subcmds" -- "$1"))
}
__rush_command_option() {
  ##############################################################################
  # rush <command> --help
  # Lists options for <command>:
  # * indented by 2 spaces and starts with "-" : grep -e "\s\s-" : <awk group 1> (--?\w[-\w]*)
  # * may be either a flag or require a value : <awk group 2> ([,|\s\w+[\w|_]*,\s])*
  # * may or may not have shorthand : <awk group 1 "shortand or longhand"> (--?\w[-\w]*) and <awk group 3 "longhand"> (--\w[-\w]*)?
  ##############################################################################
  local options
#  local options="$(rush $1 -h | grep -e "\s\s-" | awk --source '/(--?\w[-\w]*)([,|\s\w+[\w|_]*,\s])*(--\w[-\w]*)?/{print $1} {print $3}' | grep "-" | awk 1 ORS=' ')"
  # need them unescaped
  # shellcheck disable=SC2086
  options="$(rush $1 -h | grep -e "\s\s-" | awk --source '/([\-\-?]?\w[-\w]*)([,|\s\w+[\w|_]*,\s])*(--\w[-\w]*)?/{print $1} {print $2} {print $3}' | sed 's/,//' | grep "-" | awk 1 ORS=' ')"
  COMPREPLY+=($(compgen -W "$options" -- "$2"))
}
_rush_complete() {
  local subcmd
  # This might not be used but has side effects?
  # shellcheck disable=SC2034
  subcmd="${COMP_WORDS[1]}"

  local cur=${COMP_WORDS[COMP_CWORD]}
  COMPREPLY=($(compgen -W "-h --help" -- "${cur}"))

  if [[ ${#COMP_WORDS[@]} == 2 ]]; then
    # Suggest rush commands
    __rush_command "${cur}"
  else
    # Suggest options for command
    __rush_command_option "${COMP_WORDS[1]}" "${cur}"
  fi

}

complete -F _rush_complete -o nospace rush
@iclanton
Copy link
Member

iclanton commented Nov 9, 2020

Bash completion should actually work if you add this snippet to your .bashrc file:

# bash parameter completion for the Rush CLI

_rush_bash_complete()
{
  local word=${COMP_WORDS[COMP_CWORD]}

  local completions
  completions="$(rush tab-complete --position "${COMP_POINT}" --word "${COMP_LINE}" 2>/dev/null)"
  if [ $? -ne 0 ]; then
    completions=""
  fi

  COMPREPLY=( $(compgen -W "$completions" -- "$word") )
}

complete -f -F _rush_bash_complete rush

@sachinjoseph added shell completion in this PR: #2060

@btilford
Copy link
Author

Nice! I couldn't find anything mentioned from --help or the docs, so I assumed it didn't exist.

@iclanton
Copy link
Member

We should probably stick a page on the documentation website. @octogonz - opinion on where that should go?

@sachinjoseph
Copy link
Member

@octogonz I have created two PRs, please feel free to make any changes:

  1. Document tab-completion rushjs.io-website#77
  2. Mention support for tab-completion rushstack.io-website#27

@octogonz
Copy link
Collaborator

octogonz commented Dec 2, 2020

I've merged both this PRs. I also made some content updates. It's live now, so we can close this issue.

@octogonz octogonz closed this as completed Dec 2, 2020
@iclanton iclanton moved this to Closed in Bug Triage Aug 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Archived in project
Development

No branches or pull requests

4 participants