]> git.ipfire.org Git - thirdparty/systemd.git/blob - shell-completion/bash/networkctl
Merge pull request #1359 from jengelh/ue
[thirdparty/systemd.git] / shell-completion / bash / networkctl
1 # networkctl(1) completion -*- shell-script -*-
2 #
3 # This file is part of systemd.
4 #
5 # systemd is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU Lesser General Public License as published by
7 # the Free Software Foundation; either version 2.1 of the License, or
8 # (at your option) any later version.
9 #
10 # systemd is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 # General Public License for more details.
14 #
15 # You should have received a copy of the GNU Lesser General Public License
16 # along with systemd; If not, see <http://www.gnu.org/licenses/>.
17
18 __contains_word () {
19 local w word=$1; shift
20 for w in "$@"; do
21 [[ $w = "$word" ]] && return
22 done
23 return 1
24 }
25
26 __get_links() {
27 networkctl list --no-legend --no-pager --all | { while read -r a b c; do echo " $b"; done; };
28 }
29
30 _networkctl() {
31 local i verb comps
32 local cur=${COMP_WORDS[COMP_CWORD]} prev=${COMP_WORDS[COMP_CWORD-1]}
33 local -A OPTS=(
34 [STANDALONE]='-a --all -h --help --version --no-pager --no-legend'
35 [ARG]=''
36 )
37
38 local -A VERBS=(
39 [STANDALONE]='list lldp'
40 [LINKS]='status'
41 )
42
43 _init_completion || return
44
45 for ((i=0; i < COMP_CWORD; i++)); do
46 if __contains_word "${COMP_WORDS[i]}" ${VERBS[*]} &&
47 ! __contains_word "${COMP_WORDS[i-1]}" ${OPTS[ARG]}; then
48 verb=${COMP_WORDS[i]}
49 break
50 fi
51 done
52
53 if [[ "$cur" = -* ]]; then
54 COMPREPLY=( $(compgen -W '${OPTS[*]}' -- "$cur") )
55 return 0
56 fi
57
58 if [[ -z $verb ]]; then
59 comps=${VERBS[*]}
60 elif __contains_word "$verb" ${VERBS[STANDALONE]}; then
61 comps=''
62 elif __contains_word "$verb" ${VERBS[LINKS]}; then
63 comps=$( __get_links )
64 fi
65
66 COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
67 return 0
68 }
69
70 complete -F _networkctl networkctl