-
Notifications
You must be signed in to change notification settings - Fork 0
/
rush-completion
48 lines (43 loc) · 1.89 KB
/
rush-completion
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#!/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