1# pyang(1) completion           -*- shell-script -*-
2
3have pyang &&
4_pyang()
5{
6    local cur prev words cword plugin hello pl_opts wind=1
7
8    local formats="hypertree dsdl depend sample-xml-skeleton omni yin
9        tree jstree capability yang xsd uml jtox jsonxsl xmi name"
10
11    local opts_global="
12        -h --help
13        -v --version
14        -V --verbose
15        -e --list-errors
16        --print-error-code
17        -W -E
18        --ignore-error
19        --ignore-errors
20        --canonical
21        --max-line-length
22        --max-identifier-length
23        -f --format
24        -o --output
25        -F --features
26        --deviation-module
27        -p --path
28        --plugindir
29        --strict
30        --lax-xpath-checks
31        --trim-yin
32        -L --hello
33        --keep-comments
34        --check-update-from
35        -P --check-update-from-path
36        --ietf
37        --lint
38        --lint-ensure-hyphenated-names"
39
40    local opts_capability="--capability-entity"
41
42    local opts_depend="
43        --depend-target
44        --depend-no-submodules
45        --depend-from-submodules
46        --depend-recurse
47        --depend-extension
48        --depend-include-path
49        --depend-ignore-module"
50
51    local opts_dsdl="
52        --dsdl-no-documentation
53        --dsdl-no-dublin-core
54        --dsdl-record-defs"
55
56    local opts_hypertree="
57        --hypertree-help
58        --hypertree-path"
59
60    local opts_jstree="--jstree-no-path"
61
62    local opts_omni="--omni-path"
63
64    local opts_sample_xml_skeleton="
65        --sample-xml-skeleton-doctype
66        --sample-xml-skeleton-defaults
67        --sample-xml-skeleton-annotations
68        --sample-xml-skeleton-path"
69
70    local opts_tree="
71        --tree-help
72        --tree-depth
73        --tree-line-length
74        --tree-path
75        --tree-print-groupings
76        --tree-print-yang-data
77        --tree-module-name-prefix"
78
79    local opts_uml="
80        --uml-classes-only
81        --uml-split-pages
82        --uml-output-directory
83        --uml-title
84        --uml-header
85        --uml-footer
86        --uml-long-identifiers
87        --uml-inline-groupings
88        --uml-inline-augments
89        --uml-description
90        --uml-no
91        --uml-truncate
92        --uml-max-enums
93        --uml-filter
94        --uml-filter-file"
95
96    local opts_xmi="--xmi-path --xmi-no-assoc-name"
97
98    local opts_yang="
99        --yang-canonical
100        --yang-remove-unused-imports"
101
102    local opts_yin="
103        --yin-canonical
104        --yin-pretty-strings"
105
106    COMPREPLY=()
107    _get_comp_words_by_ref cur prev cword words
108
109    while [[ $wind -lt $cword ]]; do
110        case ${words[$wind]} in
111            -f|--format)
112                plugin=${words[$wind+1]}
113                ;;
114            -L|--hello)
115                hello=yes
116                ;;
117        esac
118        ((wind++))
119    done
120
121    case $prev in
122        -o|--output)
123            case $plugin in
124                dsdl)
125                    _filedir 'dsdl'
126                    ;;
127                jsonxsl)
128                    _filedir '@(xsl|xslt)'
129                    ;;
130                jstree)
131                    _filedir 'js'
132                    ;;
133                jtox)
134                    _filedir 'jtox'
135                    ;;
136                sample-xml-skeleton|hypertree)
137                    _filedir 'xml'
138                    ;;
139                omni)
140                    _filedir 'graffle'
141                    ;;
142                uml)
143                    _filedir 'uml'
144                    ;;
145                xmi)
146                    _filedir 'xmi'
147                    ;;
148                yang)
149                    _filedir 'yang'
150                    ;;
151                yin)
152                    _filedir 'yin'
153                    ;;
154                *)
155                    _filedir
156                    ;;
157            esac
158            return 0
159            ;;
160        -W)
161            COMPREPLY=($(compgen -W '$(pyang --list-errors 2>&1 | sed -nEe \
162                "s/^(Warning|Minor Error):[[:space:]]*([A-Za-z0-9_]*).*/\2/p") \
163                error none' -- "$cur"))
164            return 0
165            ;;
166        -E)
167            COMPREPLY=($(compgen -W '$(pyang --list-errors 2>&1 | \
168                sed -nEe "s/^Warning:[[:space:]]*([A-Za-z0-9_]*).*/\1/p")' \
169                -- "$cur"))
170            return 0
171            ;;
172        -f|--format)
173            COMPREPLY=($(compgen -W '$formats' -- "$cur"))
174            return 0
175            ;;
176    esac
177
178    if [[ $cur == -* ]]; then
179        pl_opts="opts_${plugin//-/_}"
180        COMPREPLY=($(compgen -W '$opts_global ${!pl_opts}' -- "$cur"))
181        return 0
182    fi
183
184    if [[ -z $hello ]]; then
185        _filedir '@(yang|yin)'
186    else
187        _filedir 'xml'
188    fi
189} &&
190complete -F _pyang pyang
191
192have yang2dsdl &&
193_yang2dsdl()
194{
195    local cur prev words cword mode opts wind=1
196
197    local targets="
198        data config get-reply get-config-reply edit-config
199        rpc rpc-reply notification"
200
201    COMPREPLY=()
202    _get_comp_words_by_ref cur prev cword words
203
204    case $prev in
205        -t)
206            COMPREPLY=($(compgen -W '$targets' -- "$cur"))
207            return 0
208            ;;
209        -v)
210            _filedir 'xml'
211            return 0
212            ;;
213        -d)
214            _filedir -d
215            return 0
216            ;;
217    esac
218
219    while [[ $wind -lt $cword ]]; do
220        case ${words[$wind]} in
221            -s)
222                mode=schemas	# use pre-generated schemas
223                break
224                ;;
225            -L)
226                mode=hello
227                ;;
228        esac
229        ((wind++))
230    done
231
232    if [[ $cur == -* ]]; then
233        opts=(-t -d -b -j -v)
234        [[ -z $mode ]] && opts+=(-L)
235        [[ $prev == "yang2dsdl" ]] && opts+=(-h)
236        COMPREPLY=($(compgen -W '${opts[*]}' -- "$cur"))
237        return 0
238    fi
239
240    case $mode in
241        hello)
242            _filedir 'xml'
243            ;;
244        schemas)
245            ;;
246        *)
247            _filedir '@(yang|yin)'
248            ;;
249    esac
250} &&
251complete -F _yang2dsdl yang2dsdl
252
253have json2xml &&
254_json2xml()
255{
256    local cur prev words cword opts jtox wind=1
257
258    local targets="data config"
259
260    COMPREPLY=()
261    _get_comp_words_by_ref cur prev cword words
262
263    case $prev in
264        -t|--target)
265            COMPREPLY=($(compgen -W '$targets' -- "$cur"))
266            return 0
267            ;;
268        -o|--output)
269            _filedir 'xml'
270            return 0
271            ;;
272    esac
273
274    while [[ $wind -lt $cword ]]; do
275        if [[ ${words[$wind]} =~ .*\.jtox$ ]]; then
276            jtox=yes
277            break
278        fi
279        ((wind++))
280    done
281
282    if [[ $cur == -* ]]; then
283        opts=(-t --target -o --output)
284        [[ $prev == "json2xml" ]] && opts+=(-h --help)
285        COMPREPLY=($(compgen -W '${opts[*]}' -- "$cur"))
286        return 0
287    fi
288
289    if [[ -z $jtox ]]; then
290        _filedir 'jtox'
291    else
292        _filedir 'json'
293    fi
294} &&
295complete -F _json2xml json2xml
296