Salome HOME
add completion for application and template commands
[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 applications
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         jobs)
38             opts2=$(echo --name --only_jobs --list --completion --test_connection --input_boards --publish $opts2)
39             ;;
40         shell)
41             opts2=$(echo --command --sat $opts2)
42             ;;
43         job)
44             opts2=$(echo --jobs_config --name $opts2)
45             ;;
46         test)
47             opts2=$(echo --base --display --grid --launcher --session $opts2)
48             ;;
49         package)
50             opts2=$(echo --name --project --salometools $opts2)
51             ;;
52         find_duplicates)
53             opts2=$(echo --path --exclude-file --exclude-extension --exclude-path $opts2)
54             ;;
55         template)
56             opts2=$(echo --name --template --target --param --info $opts2)
57             ;;
58     esac
59
60     COMPREPLY=( $(compgen -W "${opts2}" -- ${cur}) )
61 }
62
63 _show_products()
64 {
65     if [[ $appli != $prev ]]
66     then
67         opts=$(for x in `$SAT_PATH/sat -s config $appli -nv APPLICATION.products`
68             do echo ${x}; done)
69
70         COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
71    fi
72 }
73
74 _salomeTools_complete()
75 {
76     if [[ "${SAT_PATH}x" == "x" ]]
77     then
78         return 0
79     fi
80
81     local cur opts args command
82     COMPREPLY=()
83     argc="${COMP_CWORD}"
84     cur="${COMP_WORDS[COMP_CWORD]}"
85     
86     # second argument => show available APPLICATION
87     if [[ ${argc} > 1 ]]
88     then
89         command="${COMP_WORDS[1]}"
90     fi
91
92     if [[ ${argc} > 1 ]]
93     then
94         if [[ ${command%%-*} == "" ]]
95         then
96             command="${COMP_WORDS[2]}"
97             argc="$((( argc - 1)))"
98         fi
99     fi
100
101     # first argument => show available commands
102     if [[ ${argc} == 1 ]]
103     then
104         opts="config log testcommand source patch prepare environ clean configure make makeinstall compile launcher run jobs job shell test package generate find_duplicates application template --help"
105         COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
106         return 0
107     fi
108
109     if [[ ${argc} == 2 ]]
110     then
111         # get list of APPLICATIONS
112         _show_applications
113         return 0
114     fi
115     
116     # option depending on command
117     local prev="${COMP_WORDS[COMP_CWORD-1]}"
118     
119     if [[ ${prev} == "--value" || ${prev} == "-v" ]]
120     then
121         if [[ ${argc} == 4 ]]
122         then
123             # with application
124             opts=$(for x in `$SAT_PATH/sat config ${COMP_WORDS[COMP_CWORD-2]} -s ${COMP_WORDS[COMP_CWORD]}`
125                 do echo ${x} ; done)
126             COMPREPLY=( $(compgen -W "${opts}" -S "." -- ${cur}) )
127         else
128             # without application
129             opts=$(for x in `$SAT_PATH/sat config -s ${COMP_WORDS[COMP_CWORD]}`
130                 do echo ${x} ; done)
131             COMPREPLY=( $(compgen -W "${opts}" -S "." -- ${cur}) )
132         fi
133         
134         return 0
135     fi
136       
137     # show list of products
138     if [[ ${prev} == "--product" || ${prev} == "-p" ]]
139     then
140         appli="${COMP_WORDS[2]}"
141         if [[ ${command} != "source" ]]
142         then
143             opts=$(for x in `$SAT_PATH/sat config $appli -nv APPLICATION.products`
144                 do echo ${x}; done)
145
146                COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
147             return 0
148         fi
149     fi
150
151     # show argument for each command
152     case "${command}" in
153         config)
154             opts="--value --list --copy --edit --no_label --info"
155             COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
156             return 0        
157             ;;
158         log)
159             opts="--clean --last --terminal --last"
160             COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
161             return 0
162             ;;
163         clean)
164             opts="--products --sources --build --install --all --sources_without_dev"
165             COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
166             return 0
167             ;;
168         source)
169             opts="--products"
170             COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
171             return 0
172             ;;
173         patch)
174             opts="--products"
175             COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
176             return 0
177             ;;
178         prepare)
179             opts="--products --force --force_patch"
180             COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
181             return 0
182             ;;
183         environ)
184             opts="--products --shell --prefix --target"
185             COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
186             return 0
187             ;;
188         configure)
189             opts="--products --option"
190             COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
191             return 0
192             ;;
193         make)
194             opts="--products --option"
195             COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
196             return 0
197             ;;
198         makeinstall)
199             opts="--products"
200             COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
201             return 0
202             ;;
203         compile)
204             opts="--products --with_fathers --with_children --clean_all --make_flags --show --stop_first_fail"
205             COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
206             return 0
207             ;;
208         launcher)
209             opts="--name --catalog --gencat"
210             COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
211             return 0
212             ;;
213         jobs)
214             opts="--name --only_jobs --list --completion --test_connection --input_boards --publish"
215             COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
216             return 0
217             ;;
218         shell)
219             opts="--command"
220             COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
221             return 0
222             ;;
223         job)
224             opts="--jobs_config --name"
225             COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
226             return 0
227             ;;
228         test)
229             opts="--base --launcher --grid --session --display"
230             COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
231             return 0
232             ;;
233         package)
234             opts="--name --binaries --sources --project --salometools --with_vcs"
235             COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
236             return 0
237             ;;
238         generate)
239             opts="--products --yacsgen"
240             COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
241             return 0
242             ;;
243         find_duplicates)
244             opts="--path --sources --exclude-file --exclude-extension --exclude-path"
245             COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
246             return 0
247             ;;
248         application)
249             opts="--name --catalog --target --gencat --module"
250             COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
251             return 0
252             ;;
253         template)
254             opts="--name --template --target --param --info"
255             COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
256             return 0
257             ;;
258         *) return 0 ;;
259     esac
260     
261 }
262
263 # activation of auto-completion for the sat command
264 complete -F _salomeTools_complete sat
265 complete -F _salomeTools_complete ./sat
266