Skip to content

Commit 29007b5

Browse files
committed
refactor(completions/*): avoid [[ -v var ]] for localvar_inherit
When the assigned values are ensured to be non-empty strings, [[ -v var ]] is converted to [[ $var ]]. Otherwise, another local variable `has_var` is added to manage the assigned state, and [[ -v var ]] is converted to [[ $has_var ]]. I here did not consider whether the empty value of $var actually makes a sense or not.
1 parent 7a4dcfe commit 29007b5

35 files changed

+115
-138
lines changed

completions/_adb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,16 @@ _comp_cmd_adb()
2525
;;
2626
esac
2727

28-
local cmd i
29-
unset -v cmd # workaround for localvar_inherit
28+
local cmd has_cmd="" i
3029
for ((i = 1; i < cword; i++)); do
3130
if [[ ${words[i]} != -* && ${words[i - 1]} != -[sp] ]]; then
3231
cmd="${words[i]}"
32+
has_cmd=yes
3333
break
3434
fi
3535
done
3636

37-
if [[ ! -v cmd ]]; then
37+
if [[ ! $has_cmd ]]; then
3838
local tmp=()
3939
if [[ ! $cur || $cur == -* ]]; then
4040
tmp+=($(compgen -W '$(_parse_help "$1" help)' -- "$cur"))

completions/_chsh

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,13 @@ _comp_cmd_chsh()
88
local cur prev words cword comp_args
99
_comp_initialize -- "$@" || return
1010

11-
local word chroot
12-
unset -v chroot # workaround for localvar_inherit
11+
local word chroot="" has_chroot=""
1312
for word in "${words[@]}"; do
14-
if [[ -v chroot ]]; then
13+
if [[ $has_chroot ]]; then
1514
chroot=$word
1615
break
1716
fi
18-
[[ $word != -@(R|-root) ]] || chroot=
17+
[[ $word != -@(R|-root) ]] || has_chroot=yes
1918
done
2019

2120
case $prev in

completions/_op

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,28 +30,28 @@ _comp_cmd_op()
3030
local cur prev words cword split comp_args
3131
_comp_initialize -s -- "$@" || return
3232

33-
local command i
34-
unset -v command # workaround for localvar_inherit
33+
local command has_command="" i
3534
for ((i = 1; i < cword; i++)); do
3635
case ${words[i]} in
3736
--help | --version) return ;;
3837
-*) ;;
3938
*)
4039
command=${words[i]}
40+
has_command=yes
4141
break
4242
;;
4343
esac
4444
done
4545

46-
if [[ ! -v command && $cur == -* ]]; then
46+
if [[ ! $has_command && $cur == -* ]]; then
4747
COMPREPLY=($(compgen -W '$(_parse_usage "$1" --help)' -- "$cur"))
4848
[[ ${COMPREPLY-} == *= ]] && compopt -o nospace
4949
return
5050
fi
5151

52-
[[ -v command ]] && _comp_cmd_op__command_options "$1" && return
52+
[[ $has_command ]] && _comp_cmd_op__command_options "$1" && return
5353

54-
if [[ ! -v command || $command == "$prev" ]]; then
54+
if [[ ! $has_command || $command == "$prev" ]]; then
5555
COMPREPLY=($(compgen -W '$(_op_commands "$1" ${command-})' -- "$cur"))
5656
[[ ${COMPREPLY-} ]] && return
5757
fi

completions/_udevadm

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ _comp_cmd_udevadm()
88
local cur prev words cword split comp_args
99
_comp_initialize -s -- "$@" || return
1010

11-
local i udevcmd
12-
unset -v udevcmd # workaround for localvar_inherit
11+
local i udevcmd has_udevcmd=""
1312
for ((i = 1; i < cword; i++)); do
1413
if [[ ${words[i]} != -* ]]; then
1514
udevcmd=${words[i]}
15+
has_udevcmd=yes
1616
break
1717
fi
1818
done
@@ -54,7 +54,7 @@ _comp_cmd_udevadm()
5454

5555
$split && return
5656

57-
if [[ ! -v udevcmd ]]; then
57+
if [[ ! $has_udevcmd ]]; then
5858
case $cur in
5959
-*)
6060
COMPREPLY=($(compgen -W '--help --version --debug' -- "$cur"))

completions/_yum

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,15 @@ _comp_cmd_yum()
4242
local cur prev words cword split comp_args
4343
_comp_initialize -s -- "$@" || return
4444

45-
local special i
46-
unset -v special # workaround for localvar_inherit
45+
local special="" i
4746
for ((i = 1; i < ${#words[@]} - 1; i++)); do
4847
if [[ ${words[i]} == @(install|update|upgrade|remove|erase|deplist|info) ]]; then
4948
special=${words[i]}
5049
break
5150
fi
5251
done
5352

54-
if [[ -v special ]]; then
53+
if [[ $special ]]; then
5554
# TODO: install|update|upgrade should not match *src.rpm
5655
if [[ $cur == @(*/|[.~])* &&
5756
$special == @(deplist|install|update|upgrade) ]]; then

completions/apt-build

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,15 @@ _apt_build()
55
local cur prev words cword comp_args
66
_comp_initialize -- "$@" || return
77

8-
local special i
9-
unset -v special # workaround for localvar_inherit
8+
local special="" i
109
for ((i = 1; i < ${#words[@]} - 1; i++)); do
1110
if [[ ${words[i]} == @(install|remove|source|info|clean) ]]; then
1211
special=${words[i]}
1312
break
1413
fi
1514
done
1615

17-
if [[ -v special ]]; then
16+
if [[ $special ]]; then
1817
case $special in
1918
install | source | info)
2019
COMPREPLY=($(_comp_xfunc apt-cache packages))

completions/apt-cache

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,7 @@ _apt_cache()
2828
local cur prev words cword comp_args
2929
_comp_initialize -- "$@" || return
3030

31-
local special ispecial
32-
unset -v special # workaround for localvar_inherit
31+
local special="" ispecial
3332
if [[ $cur != show ]]; then
3433
for ((ispecial = 1; ispecial < ${#words[@]} - 1; ispecial++)); do
3534
if [[ ${words[ispecial]} == @(add|depends|dotty|madison|policy|rdepends|show?(pkg|src|)) ]]; then
@@ -39,7 +38,7 @@ _apt_cache()
3938
done
4039
fi
4140

42-
if [[ -v special && $ispecial -lt $cword ]]; then
41+
if [[ $special && $ispecial -lt $cword ]]; then
4342
case $special in
4443
add)
4544
_filedir
@@ -85,7 +84,7 @@ _apt_cache()
8584
--full --all-versions --no-all-versions --generate --no-generate
8685
--names-only --all-names --recurse --installed --with-source --help
8786
--version --config-file --option' -- "$cur"))
88-
elif [[ ! -v special ]]; then
87+
elif [[ ! $special ]]; then
8988
COMPREPLY=($(compgen -W 'gencaches showpkg stats showsrc dump
9089
dumpavail unmet show search depends rdepends pkgnames dotty xvcg
9190
policy madison' -- "$cur"))

completions/apt-get

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,15 @@ _apt_get()
2020
local cur prev words cword comp_args package
2121
_comp_initialize -n ':=' -- "$@" || return
2222

23-
local special i
24-
unset -v special # workaround for localvar_inherit
23+
local special="" i
2524
for ((i = 1; i < ${#words[@]} - 1; i++)); do
2625
if [[ ${words[i]} == @(install|remove|auto?(-)remove|purge|source|build-dep|download|changelog) ]]; then
2726
special=${words[i]}
2827
break
2928
fi
3029
done
3130

32-
if [[ -v special ]]; then
31+
if [[ $special ]]; then
3332
case $special in
3433
remove | auto?(-)remove | purge)
3534
_comp_xfunc_apt_get_installed_packages

completions/apt-mark

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,15 @@ _comp_cmd_apt_mark()
55
local cur prev words cword split comp_args
66
_comp_initialize -s -- "$@" || return
77

8-
local special i
9-
unset -v special # workaround for localvar_inherit
8+
local special="" i
109
for ((i = 1; i < ${#words[@]} - 1; i++)); do
1110
if [[ ${words[i]} == @(auto|manual|minimize-manual|showauto|showmanual|hold|unhold|showhold|install|remove|deinstall|purge|showinstall|showremove|showpurge) ]]; then
1211
special=${words[i]}
1312
break
1413
fi
1514
done
1615

17-
if [[ -v special ]]; then
16+
if [[ $special ]]; then
1817
case $special in
1918
auto | manual | unhold)
2019
local -A showcmds=([auto]=manual [manual]=auto [unhold]=hold)

completions/aptitude

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,15 @@ _aptitude()
1818
local cur prev words cword comp_args
1919
_comp_initialize -- "$@" || return
2020

21-
local special i
22-
unset -v special # workaround for localvar_inherit
21+
local special="" i
2322
for ((i = 1; i < ${#words[@]} - 1; i++)); do
2423
if [[ ${words[i]} == @(@(|re)install|@(|un)hold|@(|un)markauto|@(dist|full|safe)-upgrade|download|show|forbid-version|purge|remove|changelog|why@(|-not)|keep@(|-all)|build-dep|@(add|remove)-user-tag|versions) ]]; then
2524
special=${words[i]}
2625
break
2726
fi
2827
done
2928

30-
if [[ -v special ]]; then
29+
if [[ $special ]]; then
3130
case $special in
3231
install | hold | markauto | unmarkauto | dist-upgrade | full-upgrade | \
3332
safe-upgrade | download | show | changelog | why | why-not | build-dep | \

0 commit comments

Comments
 (0)