NBash
1# serv(8) completion
2
3# This completes on a list of all available services for the
4# 'serv' command, followed by that script's available commands
5#
6_service_list()
7{
8COMPREPLY=( $( serv list-all 2>/dev/null ; echo "list list-all list-startup" ) )
9COMPREPLY=( $( compgen -W '${COMPREPLY[@]}' -- "$cur" ) )
10}
11
12_service_usage_list()
13{
14local USLIST=$(serv ${prev##*/} usage 2>/dev/null | sed -e "y/|/ /" -ne "s/^.*\(u\|U\|msg_u\)sage.*{\(.*\)}.*$/\2/p")
15COMPREPLY=( $( compgen -W '$USLIST' -- "$cur" ) )
16}
17
18
19_serv()
20{
21local cur prev
22
23COMPREPLY=()
24_get_comp_words_by_ref cur prev
25
26# don't complete for things like killall, ssh and mysql if it's
27# the standalone command, rather than the init script
28[[ ${COMP_WORDS[0]} != "serv" ]] && return 0
29
30# don't complete past 2nd token
31[ $COMP_CWORD -gt 2 ] && return 0
32
33if [[ $COMP_CWORD -eq 1 && $prev == "serv" ]]; then
34_service_list
35else
36_service_usage_list
37fi
38
39return 0
40} &&
41complete -F _serv serv
42