Generalization of recursive function
[apex.git] / build / bash_completion_apex
1 # bash/zsh completion support for OPNFV Apex
2 ##############################################################################
3 # Copyright (c) 2016 Dan Radez (Red Hat) and others.
4 #
5 # All rights reserved. This program and the accompanying materials
6 # are made available under the terms of the Apache License, Version 2.0
7 # which accompanies this distribution, and is available at
8 # http://www.apache.org/licenses/LICENSE-2.0
9 ##############################################################################
10
11 # Pieces of this script are derived from the git bash completion script
12
13 ___main () {
14     local cur prev opts
15     COMPREPLY=()
16     cur="${COMP_WORDS[COMP_CWORD]}"
17     prev="${COMP_WORDS[COMP_CWORD-1]}"
18     opts=" -h $(${COMP_WORDS[0]} -h | grep -Eo '^   [^ ]+')"
19     if [[ ! $opts =~ $prev ]]; then
20         COMPREPLY=($(compgen -W "${opts}" -- ${cur}))
21     fi
22 }
23
24 # these functions are setup like this in the thought that
25 # deploy and util will eventually diverge from each other
26 # for now they can use the same main logic so it's just
27 # abstracted to another function
28 __deploy_main () {
29     ___main
30 }
31
32
33 __util_main () {
34     ___main
35 }
36
37
38 __apex_func_wrap () {
39     local cur words cword prev
40     _get_comp_words_by_ref -n =: cur words cword prev
41     $1
42 }
43
44 # Setup function for bash completion
45 __apex_complete () {
46     local wrapper="__apex_wrap${2}"
47     eval "$wrapper () { __apex_func_wrap $2 ; }"
48     complete -o bashdefault -o default -o nospace -F $wrapper $1 2>/dev/null \
49         || complete -o default -o nospace -F $wrapper $1
50 }
51
52 # run completion setup
53 __apex_complete ./deploy.py __deploy_main
54 __apex_complete opnfv-deploy __deploy_main
55 __apex_complete ./util.sh __util_main
56 __apex_complete opnfv-util __util_main