Debian Codegraph @

bash-completion/dpll

raw
1 # bash completion for dpll(8) -*- shell-script -*-
2
3 # Get all the optional commands for dpll
4 _dpll_get_optional_commands()
5 {
6 local object=$1; shift
7
8 local filter_options=""
9 local options="$(dpll $object help 2>&1 \
10 | command sed -n -e "s/^.*dpll $object //p" \
11 | cut -d " " -f 1)"
12
13 # Remove duplicate options from "dpll $OBJECT help" command
14 local opt
15 for opt in $options; do
16 if [[ $filter_options =~ $opt ]]; then
17 continue
18 else
19 filter_options="$filter_options $opt"
20 fi
21 done
22
23 echo $filter_options
24 }
25
26 # Complete based on given word
27 _dpll_direct_complete()
28 {
29 local device_id pin_id value
30
31 case $1 in
32 device_id)
33 value=$(dpll -j device show 2>/dev/null \
34 | jq '.device[].id' 2>/dev/null)
35 ;;
36 pin_id)
37 value=$(dpll -j pin show 2>/dev/null \
38 | jq '.pin[].id' 2>/dev/null)
39 ;;
40 module_name)
41 value=$(dpll -j device show 2>/dev/null \
42 | jq -r '.device[]."module-name"' 2>/dev/null \
43 | sort -u)
44 ;;
45 esac
46
47 echo $value
48 }
49
50 # Handle device subcommands
51 _dpll_device()
52 {
53 local command=$1
54
55 case $command in
56 show)
57 case $prev in
58 id)
59 COMPREPLY=( $( compgen -W \
60 "$(_dpll_direct_complete device_id)" -- "$cur" ) )
61 return 0
62 ;;
63 *)
64 COMPREPLY=( $( compgen -W "id" -- "$cur" ) )
65 return 0
66 ;;
67 esac
68 ;;
69 set)
70 case $prev in
71 id)
72 COMPREPLY=( $( compgen -W \
73 "$(_dpll_direct_complete device_id)" -- "$cur" ) )
74 return 0
75 ;;
76 mode)
77 COMPREPLY=( $( compgen -W "automatic manual" -- "$cur" ) )
78 return 0
79 ;;
80 phase-offset-monitor)
81 COMPREPLY=( $( compgen -W "enable disable true false 0 1" -- "$cur" ) )
82 return 0
83 ;;
84 phase-offset-avg-factor)
85 # numeric value, no completion
86 return 0
87 ;;
88 *)
89 COMPREPLY=( $( compgen -W "id mode phase-offset-monitor \
90 phase-offset-avg-factor" -- "$cur" ) )
91 return 0
92 ;;
93 esac
94 ;;
95 id-get)
96 case $prev in
97 module-name)
98 COMPREPLY=( $( compgen -W \
99 "$(_dpll_direct_complete module_name)" -- "$cur" ) )
100 return 0
101 ;;
102 clock-id)
103 # numeric value, no completion
104 return 0
105 ;;
106 type)
107 COMPREPLY=( $( compgen -W "pps eec" -- "$cur" ) )
108 return 0
109 ;;
110 *)
111 COMPREPLY=( $( compgen -W "module-name clock-id type" \
112 -- "$cur" ) )
113 return 0
114 ;;
115 esac
116 ;;
117 esac
118 }
119
120 # Handle pin subcommands
121 _dpll_pin()
122 {
123 local command=$1
124
125 case $command in
126 show)
127 case $prev in
128 id)
129 COMPREPLY=( $( compgen -W \
130 "$(_dpll_direct_complete pin_id)" -- "$cur" ) )
131 return 0
132 ;;
133 device)
134 COMPREPLY=( $( compgen -W \
135 "$(_dpll_direct_complete device_id)" -- "$cur" ) )
136 return 0
137 ;;
138 *)
139 COMPREPLY=( $( compgen -W "id device" -- "$cur" ) )
140 return 0
141 ;;
142 esac
143 ;;
144 set)
145 case $prev in
146 id|parent-device)
147 COMPREPLY=( $( compgen -W \
148 "$(_dpll_direct_complete device_id)" -- "$cur" ) )
149 return 0
150 ;;
151 parent-pin|reference-sync)
152 COMPREPLY=( $( compgen -W \
153 "$(_dpll_direct_complete pin_id)" -- "$cur" ) )
154 return 0
155 ;;
156 frequency|phase-adjust|esync-frequency|prio)
157 # numeric value, no completion
158 return 0
159 ;;
160 direction)
161 COMPREPLY=( $( compgen -W "input output" -- "$cur" ) )
162 return 0
163 ;;
164 state)
165 COMPREPLY=( $( compgen -W \
166 "connected disconnected selectable" -- "$cur" ) )
167 return 0
168 ;;
169 *)
170 # Check if we are in nested context (after parent-device/parent-pin/reference-sync)
171 local i nested_keyword=""
172 for (( i=cword-1; i>0; i-- )); do
173 case "${words[i]}" in
174 parent-device)
175 nested_keyword="parent-device"
176 break
177 ;;
178 parent-pin|reference-sync)
179 nested_keyword="parent-pin"
180 break
181 ;;
182 id|frequency|phase-adjust|esync-frequency)
183 # Hit a top-level keyword, not in nested context
184 break
185 ;;
186 esac
187 done
188
189 if [[ "$nested_keyword" == "parent-device" ]]; then
190 COMPREPLY=( $( compgen -W "direction prio state" -- "$cur" ) )
191 elif [[ "$nested_keyword" == "parent-pin" ]]; then
192 COMPREPLY=( $( compgen -W "state" -- "$cur" ) )
193 else
194 COMPREPLY=( $( compgen -W "id frequency phase-adjust \
195 esync-frequency parent-device parent-pin reference-sync" \
196 -- "$cur" ) )
197 fi
198 return 0
199 ;;
200 esac
201 ;;
202 id-get)
203 case $prev in
204 module-name)
205 COMPREPLY=( $( compgen -W \
206 "$(_dpll_direct_complete module_name)" -- "$cur" ) )
207 return 0
208 ;;
209 clock-id)
210 # numeric value, no completion
211 return 0
212 ;;
213 board-label|panel-label|package-label)
214 # string value, no completion
215 return 0
216 ;;
217 type)
218 COMPREPLY=( $( compgen -W "mux ext synce-eth-port \
219 int-oscillator gnss" -- "$cur" ) )
220 return 0
221 ;;
222 *)
223 COMPREPLY=( $( compgen -W "module-name clock-id \
224 board-label panel-label package-label type" \
225 -- "$cur" ) )
226 return 0
227 ;;
228 esac
229 ;;
230 esac
231 }
232
233 # Handle monitor subcommand
234 _dpll_monitor()
235 {
236 # monitor has no additional arguments
237 return 0
238 }
239
240 # Complete any dpll command
241 _dpll()
242 {
243 local cur prev words cword
244 local opt='--Version --json --pretty'
245 local objects="device pin monitor"
246
247 _init_completion || return
248 # Gets the word-to-complete without considering the colon as word breaks
249 _get_comp_words_by_ref -n : cur prev words cword
250
251 if [[ $cword -eq 1 ]]; then
252 case $cur in
253 -*)
254 COMPREPLY=( $( compgen -W "$opt" -- "$cur" ) )
255 return 0
256 ;;
257 *)
258 COMPREPLY=( $( compgen -W "$objects help" -- "$cur" ) )
259 return 0
260 ;;
261 esac
262 fi
263
264 # Deal with options
265 if [[ $prev == -* ]]; then
266 case $prev in
267 -V|--Version)
268 return 0
269 ;;
270 -j|--json)
271 COMPREPLY=( $( compgen -W "--pretty $objects" -- "$cur" ) )
272 return 0
273 ;;
274 *)
275 COMPREPLY=( $( compgen -W "$objects" -- "$cur" ) )
276 return 0
277 ;;
278 esac
279 fi
280
281 # Remove all options so completions don't have to deal with them.
282 local i
283 for (( i=1; i < ${#words[@]}; )); do
284 if [[ ${words[i]::1} == - ]]; then
285 words=( "${words[@]:0:i}" "${words[@]:i+1}" )
286 [[ $i -le $cword ]] && cword=$(( cword - 1 ))
287 else
288 i=$(( ++i ))
289 fi
290 done
291
292 local object=${words[1]}
293 local command=${words[2]}
294 local pprev=${words[cword - 2]}
295 local prev=${words[cword - 1]}
296
297 case $object in
298 device|pin|monitor)
299 if [[ $cword -eq 2 ]]; then
300 COMPREPLY=( $( compgen -W "help" -- "$cur") )
301 if [[ $object != "monitor" ]]; then
302 COMPREPLY+=( $( compgen -W \
303 "$(_dpll_get_optional_commands $object)" -- "$cur" ) )
304 fi
305 else
306 "_dpll_$object" $command
307 fi
308 ;;
309 help)
310 return 0
311 ;;
312 *)
313 COMPREPLY=( $( compgen -W "$objects help" -- "$cur" ) )
314 ;;
315 esac
316
317 } &&
318 complete -F _dpll dpll
319
320 # ex: ts=4 sw=4 et filetype=sh