]> git.ipfire.org Git - thirdparty/systemd.git/blob - shell-completion/bash/loginctl
bash-completion: loginctl: suggest argument for --output option
[thirdparty/systemd.git] / shell-completion / bash / loginctl
1 # loginctl(1) completion -*- shell-script -*-
2 # SPDX-License-Identifier: LGPL-2.1+
3 #
4 # This file is part of systemd.
5 #
6 # Copyright © 2010 Ran Benita
7 #
8 # systemd is free software; you can redistribute it and/or modify it
9 # under the terms of the GNU Lesser General Public License as published by
10 # the Free Software Foundation; either version 2.1 of the License, or
11 # (at your option) any later version.
12 #
13 # systemd is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 # General Public License for more details.
17 #
18 # You should have received a copy of the GNU Lesser General Public License
19 # along with systemd; If not, see <http://www.gnu.org/licenses/>.
20
21 __contains_word () {
22 local w word=$1; shift
23 for w in "$@"; do
24 [[ $w = "$word" ]] && return
25 done
26 }
27
28 __get_all_sessions () { loginctl --no-legend list-sessions | { while read -r a b; do printf "%s\n" "$a"; done; } ; }
29 __get_all_users () { loginctl --no-legend list-users | { while read -r a b; do printf "%s\n" "$b"; done; } ; }
30 __get_all_seats () { loginctl --no-legend list-seats | { while read -r a b; do printf "%s\n" "$a"; done; } ; }
31
32 _loginctl () {
33 local cur=${COMP_WORDS[COMP_CWORD]} prev=${COMP_WORDS[COMP_CWORD-1]}
34 local i verb comps
35
36 local -A OPTS=(
37 [STANDALONE]='--all -a --help -h --no-pager --version
38 --no-legend --no-ask-password -l --full --value'
39 [ARG]='--host -H --kill-who --property -p --signal -s -M --machine
40 -n --lines -o --output'
41 )
42
43 if __contains_word "$prev" ${OPTS[ARG]}; then
44 case $prev in
45 --signal|-s)
46 _signals
47 return
48 ;;
49 --kill-who)
50 comps='all leader'
51 ;;
52 --host|-H)
53 comps=$(compgen -A hostname)
54 ;;
55 --property|-p)
56 comps=''
57 ;;
58 --output|-o)
59 comps=$( loginctl --output=help 2>/dev/null )
60 ;;
61 esac
62 COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
63 return 0
64 fi
65
66
67 if [[ "$cur" = -* ]]; then
68 COMPREPLY=( $(compgen -W '${OPTS[*]}' -- "$cur") )
69 return 0
70 fi
71
72 local -A VERBS=(
73 [SESSIONS]='session-status show-session activate lock-session unlock-session terminate-session kill-session'
74 [USERS]='user-status show-user enable-linger disable-linger terminate-user kill-user'
75 [SEATS]='seat-status show-seat terminate-seat'
76 [STANDALONE]='list-sessions lock-sessions unlock-sessions list-users list-seats flush-devices'
77 [ATTACH]='attach'
78 )
79
80 for ((i=0; i < COMP_CWORD; i++)); do
81 if __contains_word "${COMP_WORDS[i]}" ${VERBS[*]} &&
82 ! __contains_word "${COMP_WORDS[i-1]}" ${OPTS[ARG]}; then
83 verb=${COMP_WORDS[i]}
84 break
85 fi
86 done
87
88 if [[ -z $verb ]]; then
89 comps="${VERBS[*]}"
90
91 elif __contains_word "$verb" ${VERBS[SESSIONS]}; then
92 comps=$( __get_all_sessions )
93
94 elif __contains_word "$verb" ${VERBS[USERS]}; then
95 comps=$( __get_all_users )
96
97 elif __contains_word "$verb" ${VERBS[SEATS]}; then
98 comps=$( __get_all_seats )
99
100 elif __contains_word "$verb" ${VERBS[STANDALONE]}; then
101 comps=''
102
103 elif __contains_word "$verb" ${VERBS[ATTACH]}; then
104 if [[ $prev = $verb ]]; then
105 comps=$( __get_all_seats )
106 else
107 comps=$(compgen -A file -- "$cur" )
108 compopt -o filenames
109 fi
110 fi
111
112 COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
113 return 0
114 }
115
116 complete -F _loginctl loginctl