shell-completion: support --json option for hostnamectl

This commit is contained in:
Yu Watanabe
2021-05-14 09:18:29 +09:00
parent bfc2b05e59
commit f8cd3f610f
2 changed files with 37 additions and 2 deletions
+29 -2
View File
@@ -25,11 +25,38 @@ __contains_word () {
done
}
__get_machines() {
local a b
machinectl list --full --no-legend --no-pager 2>/dev/null |
{ while read a b; do echo " $a"; done; };
}
_hostnamectl() {
local i verb comps
local cur=${COMP_WORDS[COMP_CWORD]} prev=${COMP_WORDS[COMP_CWORD-1]}
local OPTS='-h --help --version --transient --static --pretty
--no-ask-password -H --host -M --machine'
local -A OPTS=(
[STANDALONE]='-h --help --version --transient --static --pretty --no-ask-password'
[ARG]='-H --host -M --machine --json'
)
if __contains_word "$prev" ${OPTS[ARG]} ${OPTS[ARGUNKNOWN]}; then
case $prev in
--host|-H)
comps=$(compgen -A hostname)
;;
--machine|-M)
comps=$( __get_machines )
;;
--json)
comps=$( hostnamectl --json=help 2>/dev/null )
;;
*)
return 0
;;
esac
COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
return 0
fi
if [[ $cur = -* ]]; then
COMPREPLY=( $(compgen -W '${OPTS[*]}' -- "$cur") )