X Tutup
Skip to content

Commit b5b7ea7

Browse files
committed
bash-completion: simplify udevadm completion
The AA is unnecessary and only adds needless complexity. Replace it with a case statement instead of repeatedly calling __contains_word to overglorify string equalities.
1 parent a72d698 commit b5b7ea7

File tree

1 file changed

+45
-52
lines changed

1 file changed

+45
-52
lines changed

shell-completion/bash/udevadm

Lines changed: 45 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -34,68 +34,61 @@ _udevadm() {
3434
local cur=${COMP_WORDS[COMP_CWORD]} prev=${COMP_WORDS[COMP_CWORD-1]}
3535
local OPTS='-h --help --version --debug'
3636

37-
local -A VERBS=(
38-
[INFO]='info'
39-
[TRIGGER]='trigger'
40-
[SETTLE]='settle'
41-
[CONTROL]='control'
42-
[MONITOR]='monitor'
43-
[HWDB]='hwdb'
44-
[TESTBUILTIN]='test-builtin'
45-
[TEST]='test'
46-
)
37+
local verbs=(info trigger settle control monitor hwdb test-builtin test)
4738

48-
for ((i=0; $i <= $COMP_CWORD; i++)); do
49-
if __contains_word "${COMP_WORDS[i]}" ${VERBS[*]} &&
39+
for ((i=0; i <= COMP_CWORD; i++)); do
40+
if __contains_word "${COMP_WORDS[i]}" "${verbs[@]}" &&
5041
! __contains_word "${COMP_WORDS[i-1]}" ${OPTS[ARG]}; then
5142
verb=${COMP_WORDS[i]}
5243
break
5344
fi
5445
done
5546

56-
if [[ -z $verb && $cur = -* ]]; then
57-
COMPREPLY=( $(compgen -W '${OPTS[*]}' -- "$cur") )
47+
if [[ -z $verb ]]; then
48+
COMPREPLY=( $(compgen -W '${OPTS[*]} ${verbs[*]}' -- "$cur") )
5849
return 0
5950
fi
6051

61-
if [[ -z $verb ]]; then
62-
comps=${VERBS[*]}
63-
64-
elif __contains_word "$verb" ${VERBS[INFO]}; then
65-
if [[ $cur = -* ]]; then
66-
comps='--help --query= --path= --name= --root --attribute-walk --export-db --cleanup-db'
67-
else
68-
comps=$( __get_all_sysdevs )
69-
fi
70-
71-
elif __contains_word "$verb" ${VERBS[TRIGGER]}; then
72-
comps='--help --verbose --dry-run --type= --action= --subsystem-match=
73-
--subsystem-nomatch= --attr-match= --attr-nomatch= --property-match=
74-
--tag-match= --sysname-match= --parent-match='
75-
76-
elif __contains_word "$verb" ${VERBS[SETTLE]}; then
77-
comps='--help --timeout= --seq-start= --seq-end= --exit-if-exists= --quiet'
78-
79-
elif __contains_word "$verb" ${VERBS[CONTROL]}; then
80-
comps='--help --exit --log-priority= --stop-exec-queue --start-exec-queue
81-
--reload --property= --children-max= --timeout='
82-
83-
elif __contains_word "$verb" ${VERBS[MONITOR]}; then
84-
comps='--help --kernel --udev --property --subsystem-match= --tag-match='
85-
86-
elif __contains_word "$verb" ${VERBS[HWDB]}; then
87-
comps='--help --update --test='
88-
89-
elif __contains_word "$verb" ${VERBS[TEST]}; then
90-
if [[ $cur = -* ]]; then
91-
comps='--help --action='
92-
else
93-
comps=$( __get_all_sysdevs )
94-
fi
95-
96-
elif __contains_word "$verb" ${VERBS[TESTBUILTIN]}; then
97-
comps='blkid btrfs hwdb input_id keyboard kmod net_id path_id usb_id uaccess'
98-
fi
52+
case $verb in
53+
'info')
54+
if [[ $cur = -* ]]; then
55+
comps='--help --query= --path= --name= --root --attribute-walk --export-db --cleanup-db'
56+
else
57+
comps=$( __get_all_sysdevs )
58+
fi
59+
;;
60+
'trigger')
61+
comps='--help --verbose --dry-run --type= --action= --subsystem-match=
62+
--subsystem-nomatch= --attr-match= --attr-nomatch= --property-match=
63+
--tag-match= --sysname-match= --parent-match='
64+
;;
65+
'settle')
66+
comps='--help --timeout= --seq-start= --seq-end= --exit-if-exists= --quiet'
67+
;;
68+
'control')
69+
comps='--help --exit --log-priority= --stop-exec-queue --start-exec-queue
70+
--reload --property= --children-max= --timeout='
71+
;;
72+
'monitor')
73+
comps='--help --kernel --udev --property --subsystem-match= --tag-match='
74+
;;
75+
'hwdb')
76+
comps='--help --update --test='
77+
;;
78+
'test')
79+
if [[ $cur = -* ]]; then
80+
comps='--help --action='
81+
else
82+
comps=$( __get_all_sysdevs )
83+
fi
84+
;;
85+
'test-builtin')
86+
comps='blkid btrfs hwdb input_id keyboard kmod net_id path_id usb_id uaccess'
87+
;;
88+
*)
89+
comps=${VERBS[*]}
90+
;;
91+
esac
9992

10093
COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
10194
return 0

0 commit comments

Comments
 (0)
X Tutup