| 1 |
# clush bash completion |
| 2 |
# |
| 3 |
# to install in /usr/share/bash-completion/completions/ or ~/.local/share/bash-completion/completions/ |
| 4 |
_clush_command_or_file() { |
| 5 |
# undo our nospace setting... |
| 6 |
compopt +o nospace |
| 7 |
|
| 8 |
# skip if shortopt is set -- these helpers cannot "restore" shortopt at start of |
| 9 |
# the completions they provide easily |
| 10 |
[ -n "$shortopt" ] && return |
| 11 |
|
| 12 |
# complete either files (copy mode) or commands (if target set) |
| 13 |
case "$target_set,$mode" in |
| 14 |
*,copy) |
| 15 |
# available since bash-completion 2.12 |
| 16 |
if declare -F _comp_compgen_filedir >/dev/null; then |
| 17 |
_comp_compgen_filedir |
| 18 |
else |
| 19 |
_filedir |
| 20 |
fi |
| 21 |
;; |
| 22 |
1,command) |
| 23 |
# available since bash-completion 2.12 |
| 24 |
if declare -F _comp_command_offset >/dev/null; then |
| 25 |
_comp_command_offset "$i" |
| 26 |
else |
| 27 |
_command_offset "$i" |
| 28 |
fi |
| 29 |
;; |
| 30 |
esac |
| 31 |
} |
| 32 |
|
| 33 |
_clush() |
| 34 |
{ |
| 35 |
# shellcheck disable=SC2034 # set/used by _init_completion |
| 36 |
local cur prev words cword split |
| 37 |
local i word options="" compopts="" skip=argv0 groupsource="" cleangroup="" |
| 38 |
local mode=command target_set="" |
| 39 |
local shortopt="" |
| 40 |
|
| 41 |
_init_completion -s -n : || return |
| 42 |
|
| 43 |
# stop parsing if there had been any non-option before (or --) |
| 44 |
for i in "${!words[@]}"; do |
| 45 |
word="${words[i]}" |
| 46 |
case "$skip" in |
| 47 |
"") ;; |
| 48 |
groupsource) |
| 49 |
groupsource="$word" |
| 50 |
;& # fallthrough |
| 51 |
*) |
| 52 |
skip="" |
| 53 |
continue |
| 54 |
;; |
| 55 |
esac |
| 56 |
case "$word" in |
| 57 |
"") ;; |
| 58 |
--) |
| 59 |
i=$((i+1)) # command from next word! |
| 60 |
_clush_command_or_file |
| 61 |
return |
| 62 |
;; |
| 63 |
-c|--copy|--rcopy) mode=copy;; |
| 64 |
-w|-g|--group) target_set=1; skip=any;; |
| 65 |
# no-arg options |
| 66 |
--version|-h|--help|-n|--nostdin|-a|--all|-q|--quiet|\ |
| 67 |
-b|-B|--dshbak|-G|--groupbase|-L|-N|-P|--progress|\ |
| 68 |
-r|--regroup|-S|--maxrc|--diff|-p|-v|--verbose|-d|--debug) ;; |
| 69 |
# get source separately... |
| 70 |
--groupsource=*) groupsource="${word#*=}";; |
| 71 |
-s|--groupsource) skip=groupsource;; |
| 72 |
# assume all the rest as options... |
| 73 |
# options with = included in word |
| 74 |
--*=*) ;; |
| 75 |
-*) skip=any;; |
| 76 |
*) |
| 77 |
# likely non-option, in copy mode options like -w can come |
| 78 |
# later so just skip, otherwise likely start of command |
| 79 |
[ "$mode" = copy ] && continue |
| 80 |
_clush_command_or_file |
| 81 |
return;; |
| 82 |
esac |
| 83 |
done |
| 84 |
|
| 85 |
# split short opts without space... |
| 86 |
case "$cur" in |
| 87 |
-[a-z]*) |
| 88 |
shortopt="${cur:0:2}" |
| 89 |
prev="$shortopt" |
| 90 |
cur="${cur:2}" |
| 91 |
;; |
| 92 |
esac |
| 93 |
|
| 94 |
case "$prev" in |
| 95 |
-w|-x|-g|--group|-X) |
| 96 |
case "$cur" in |
| 97 |
*:*) |
| 98 |
groupsource="${cur%%:*}" |
| 99 |
groupsource="${groupsource#@}" |
| 100 |
;; |
| 101 |
*) |
| 102 |
if [ -n "$groupsource" ]; then |
| 103 |
cleangroup=1 |
| 104 |
fi |
| 105 |
;; |
| 106 |
esac |
| 107 |
if [ "$prev" = "-w" ]; then |
| 108 |
compopts="@*" # include all nodes |
| 109 |
fi |
| 110 |
# shellcheck disable=SC2086 ## $compopts expanded on purpose |
| 111 |
options="$(cluset ${groupsource:+-s "$groupsource"} --completion $compopts)" |
| 112 |
if [ -n "$cleangroup" ]; then |
| 113 |
options=${options//@"$groupsource":/@} |
| 114 |
fi |
| 115 |
case "$prev" in |
| 116 |
-g|--group|-X) |
| 117 |
options=${options//@/} |
| 118 |
;; |
| 119 |
esac |
| 120 |
;; |
| 121 |
-s|--groupsource) |
| 122 |
options=$(cluset --groupsources --quiet) |
| 123 |
;; |
| 124 |
--color) |
| 125 |
options="never always auto" |
| 126 |
;; |
| 127 |
-R|--worker) |
| 128 |
options="ssh exec rsh" |
| 129 |
;; |
| 130 |
# no-arg options |
| 131 |
--version|-h|--help|-n|--nostdin|-a|--all|-q|--quiet|\ |
| 132 |
-b|-B|--dshbak|-G|--groupbase|-L|-N|-P|--progress|\ |
| 133 |
-r|--regroup|-S|--maxrc|--diff|-p|-v|--verbose|-d|--debug|-c|--copy|--rcopy) ;; |
| 134 |
# any other option: ignore next word (likely argument) |
| 135 |
-*) |
| 136 |
return;; |
| 137 |
esac |
| 138 |
# new option or no option: |
| 139 |
if [ -z "$options" ]; then |
| 140 |
case "$cur" in |
| 141 |
-*) |
| 142 |
# starts with dash - get all options from help text... |
| 143 |
options="$(clush --help | grep -oP -- '(?<=[ \t])(-[a-z]|--[^= \t]*)')" |
| 144 |
;; |
| 145 |
*) |
| 146 |
# otherwise complete command or file if appropriate and stop here |
| 147 |
_clush_command_or_file |
| 148 |
return |
| 149 |
esac |
| 150 |
fi |
| 151 |
|
| 152 |
# append space for everything that doesn't end in `:` (likely a groupsource) |
| 153 |
mapfile -t COMPREPLY \ |
| 154 |
< <(compgen -W "$options" -- "$cur" \ |
| 155 |
| sed -e 's/[^:]$/& /' -e "s/^/$shortopt/") |
| 156 |
# remove the prefix from COMPREPLY if $cur contains colons and |
| 157 |
# COMP_WORDBREAKS splits on colons... |
| 158 |
__ltrim_colon_completions "$cur" |
| 159 |
} && complete -o nospace -F _clush "${BASH_SOURCE##*/}" |