Salome HOME
Add completion mechanism
[tools/sat.git] / complete_sat.sh
1 #!/bin/bash
2 #  Copyright (C) 2010-2012  CEA/DEN
3 #
4 #  This library is free software; you can redistribute it and/or
5 #  modify it under the terms of the GNU Lesser General Public
6 #  License as published by the Free Software Foundation; either
7 #  version 2.1 of the License.
8 #
9 #  This library is distributed in the hope that it will be useful,
10 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
11 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 #  Lesser General Public License for more details.
13 #
14 #  You should have received a copy of the GNU Lesser General Public
15 #  License along with this library; if not, write to the Free Software
16 #  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17
18 # Completion Function for salomeTools (sat)
19
20 export SAT_PATH=$(cd `dirname "${BASH_SOURCE}"` && pwd)
21
22 _show_applications()
23 {
24     local opts2=$(for x in `$SAT_PATH/sat config -nl`
25         do
26             echo ${x}
27         done)
28
29     # additional options for command working without products
30     case "${command}" in
31         config)
32             opts2=$(echo --list --value --edit --info $opts2)
33             ;;
34         log)
35             opts2=$(echo --clean --full --last --terminal $opts2)
36             ;;
37     esac
38
39     COMPREPLY=( $(compgen -W "${opts2}" -- ${cur}) )
40 }
41
42 _salomeTools_complete()
43 {
44     if [[ "${SAT_PATH}x" == "x" ]]
45     then
46         return 0
47     fi
48
49     local cur opts args command
50     COMPREPLY=()
51     argc="${COMP_CWORD}"
52     cur="${COMP_WORDS[COMP_CWORD]}"
53     
54     # second argument => show available APPLICATION
55     if [[ ${argc} > 1 ]]
56     then
57         command="${COMP_WORDS[1]}"
58     fi
59
60     if [[ ${argc} > 1 ]]
61     then
62         if [[ ${command%%-*} == "" ]]
63         then
64             command="${COMP_WORDS[2]}"
65             argc="$((( argc - 1)))"
66         fi
67     fi
68
69     # first argument => show available commands
70     if [[ ${argc} == 1 ]]
71     then
72         opts="config log testcommand --help"
73         COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
74         return 0
75     fi
76
77     if [[ ${argc} == 2 ]]
78     then
79         # get list of APPLICATIONS
80         _show_applications
81         return 0
82     fi
83     
84     # option depending on command
85     local prev="${COMP_WORDS[COMP_CWORD-1]}"
86     
87     if [[ ${prev} == "--value" || ${prev} == "-v" ]]
88     then
89         if [[ ${argc} == 4 ]]
90         then
91             # with application
92             opts=$(for x in `$SAT_PATH/sat config ${COMP_WORDS[COMP_CWORD-2]} -s ${COMP_WORDS[COMP_CWORD]}`
93                 do echo ${x} ; done)
94             COMPREPLY=( $(compgen -W "${opts}" -S "." -- ${cur}) )
95         else
96             # without application
97             opts=$(for x in `$SAT_PATH/sat config -s ${COMP_WORDS[COMP_CWORD]}`
98                 do echo ${x} ; done)
99             COMPREPLY=( $(compgen -W "${opts}" -S "." -- ${cur}) )
100         fi
101         
102         return 0
103     fi
104
105     # show list of softwares
106     if [[ ${prev} == "--module" || ${prev} == "-m" ]]
107     then
108         prod="${COMP_WORDS[2]}"
109         if [[ ${command} != "test" ]]
110         then
111             opts=$(for x in `$SAT_PATH/sat config $prod -nv PRODUCT.softwares`
112                 do echo ${x}; done)
113
114                COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
115             return 0
116            fi
117     fi
118
119     # show argument for each command
120     case "${command}" in
121         config)
122             opts="--value --list --copy --edit --no_label"
123             COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
124             return 0        
125             ;;
126         log)
127             opts="--clean --last --terminal --last"
128             COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
129             return 0
130             ;;
131         *) return 0 ;;
132     esac
133     
134 }
135
136 # activation de l'auto-completion pour la commande sat
137 complete -F _salomeTools_complete sat
138 complete -F _salomeTools_complete ./sat
139