1
1
# clush bash completion
2
2
#
3
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
+ # complete either files (copy mode) or commands (if target set)
9
+ case "$target_set,$mode" in
10
+ *,copy) _comp_compgen_filedir;;
11
+ 1,command) _comp_command_offset "$i";;
12
+ esac
13
+ }
14
+
4
15
_clush()
5
16
{
6
17
# shellcheck disable=SC2034 # set/used by _init_completion
7
18
local cur prev words cword split
8
- local word options="" compopts="" skip=argv0 groupsource="" cleangroup=""
19
+ local i word options="" compopts="" skip=argv0 groupsource="" cleangroup=""
20
+ local mode=command target_set=""
9
21
10
22
_init_completion -s -n : || return
11
23
12
24
# stop parsing if there had been any non-option before (or --)
13
- for word in "${words[@]}"; do
25
+ for i in "${!words[@]}"; do
26
+ word="${words[i]}"
14
27
case "$skip" in
15
28
"") ;;
16
29
groupsource)
@@ -23,7 +36,13 @@ _clush()
23
36
esac
24
37
case "$word" in
25
38
"") ;;
26
- --) return;;
39
+ --)
40
+ i=$((i+1)) # command from next word!
41
+ _clush_command_or_file
42
+ return
43
+ ;;
44
+ -c|--copy|--rcopy) mode=copy;;
45
+ -w|-g|--group) target_set=1; skip=any;;
27
46
# no-arg options
28
47
--version|-h|--help|-n|--nostdin|-a|--all|-q|--quiet|\
29
48
-v|--verbose|-d|--debug) ;;
@@ -34,7 +53,10 @@ _clush()
34
53
# options with = included in word
35
54
--*=*) ;;
36
55
-*) skip=any;;
37
- *) return;; # was non-option
56
+ *)
57
+ # likely non-option, continue file or command completion from here
58
+ _clush_command_or_file
59
+ return;;
38
60
esac
39
61
done
40
62
@@ -54,6 +76,7 @@ _clush()
54
76
if [ "$prev" = "-w" ]; then
55
77
compopts="@*" # include all nodes
56
78
fi
79
+ # shellcheck disable=SC2086 ## $compopts expanded on purpose
57
80
options="$(cluset ${groupsource:+-s "$groupsource"} --completion $compopts)"
58
81
if [ -n "$cleangroup" ]; then
59
82
options=${options//@"$groupsource":/@}
@@ -75,17 +98,28 @@ _clush()
75
98
;;
76
99
# no-arg options
77
100
--version|-h|--help|-n|--nostdin|-a|--all|-q|--quiet|\
78
- -v|--verbose|-d|--debug) ;;
79
- # any other option: just ignore.
101
+ -v|--verbose|-d|--debug|-c|--copy|--rcopy ) ;;
102
+ # any other option: ignore next word (likely argument)
80
103
-*)
81
104
return;;
82
105
esac
83
- # get all options from help text... not 100% accurate but good enough.
84
- [ -n "$options" ] || options="$(clush --help | grep -oP -- '(?<=[ \t])(-[a-z]|--[^= \t]*)')"
106
+ # new option or no option:
107
+ if [ -z "$options" ]; then
108
+ case "$cur" in
109
+ -*)
110
+ # starts with dash - get all options from help text...
111
+ options="$(clush --help | grep -oP -- '(?<=[ \t])(-[a-z]|--[^= \t]*)')"
112
+ ;;
113
+ *)
114
+ # otherwise complete command or file if appropriate and stop here
115
+ _clush_command_or_file
116
+ return
117
+ esac
118
+ fi
85
119
86
120
# append space for everything that doesn't end in `:` (likely a groupsource)
87
121
mapfile -t COMPREPLY < <(compgen -W "$options" -- "$cur" | sed -e 's/[^:]$/& /')
88
122
# remove the prefix from COMPREPLY if $cur contains colons and
89
123
# COMP_WORDBREAKS splits on colons...
90
124
__ltrim_colon_completions "$cur"
91
- } && complete -o nospace -F _clush ${BASH_SOURCE##*/}
125
+ } && complete -o nospace -F _clush " ${BASH_SOURCE##*/}"
0 commit comments