#!/bin/bash have xmlstarlet && _xmlstarlet() { local cur prev base baseopts formatopts opts helpopts validateopts canonicopts editopts elementopts transformopts encodingopts COMPREPLY=() cur="${COMP_WORDS[COMP_CWORD]}" prev="${COMP_WORDS[COMP_CWORD-1]}" base="${COMP_WORDS[1]}" helpopts="--help" baseopts="ed edit sel select tr transform val validate fo format el elements c14n canonic ls list esc escape unesc unescape pyx xmln p2x depyx --version" canonicopts="--with-comments --without-comments --exc-with-comments --exc-without-comments" editopts="-P --pf -S --ps -O --omit-decl -N" selectopts="-C --comp -R --root -T --text -I --indent -D --xml-decl -B --noblanks -E --encode -N --net -t --template" elementopts="-a -v -u -d" formatopts="-n --noindent -t --indent-tab -s --indent-spaces -o --omit-decl -R --recover -D --dropdtd -C --nocdata -N --nsclean -e --encode -H --html -h --help" transformopts="--omit-decl -E --embed --show-ext --val --net --xinclude --maxdepth --html --catalogs" validateopts="-w --well-formed -d --dtd -s --xsd -E --embed -r --relaxng -e --err -b --list-bad -g --list-good -q --quiet" encodingopts="UTF-8 ISO-8859-1 ISO-8859-15 CP1252" opts=() if [ ${COMP_CWORD} -eq 1 ]; then COMPREPLY=($(compgen -W "${baseopts} ${helpopts}" -- ${cur})) return 0 elif [ ${COMP_CWORD} -eq 2 ]; then # # Complete the arguments to some of the basic commands. # case "${base}" in c14n|canonic) opts="${canonicopts} ${helpopts}" ;; ed|edit) opts="${editopts} ${helpopts}" ;; sel|select) opts="${selectopts} ${helpopts}" ;; el|elements) opts="${elementopts} ${helpopts}" if [ ! "${cur:0:1}" = "-" ]; then COMPREPLY=( $(compgen -f "${cur}") ) return 0 fi ;; fo|format) opts="${formatopts} ${helpopts}" ;; val|validate) opts="${validateopts} ${helpopts}" if [ ! "${cur:0:1}" = "-" ]; then COMPREPLY=( $(compgen -f "${cur}") ) return 0 fi ;; tr|transform) opts="${transformopts} ${helpopts}" if [ ! "${cur:0:1}" = "-" ]; then COMPREPLY=( $(compgen -f "${cur}") ) return 0 fi ;; *) COMPREPLY=( ${helpopts} $(compgen -f "${cur}") ) return 0 ;; esac COMPREPLY=($(compgen -W "${opts}" -- ${cur})) return 0 elif [ ${COMP_CWORD} -eq 3 ]; then case "${base}" in ed|edit) if [ "${cur:0:1}" = "-" ]; then ## xmlstarlet edit {} opts="-d --delete -i --insert -a --append -s --subnode -m --move -r --rename -u --update" COMPREPLY=($(compgen -W "${opts}" -- ${cur})) return 0 fi ;; sel|select) case "${prev}" in "-E"|"--encode") opts=${encodingopts} ;; "-t"|"--template") opts="-c --copy-of -v --value-of -o --output -n --nl -f --inp-name -m --match -i --if -e --elem -a --attr -b --break -s --sort" ;; *) COMPREPLY=( $(compgen -f "${cur}") ) return 0 ;; esac COMPREPLY=($(compgen -W "${opts}" -- ${cur})) return 0 ;; el|elements) if [ "${cur:0:1}" = "-" ]; then ## xmlstarlet elements [options] opts="-a -v -u -d" COMPREPLY=($(compgen -W "${opts}" -- ${cur})) return 0 fi ;; fo|format) if [ "${prev}" = "-e" -o "${prev}" = "--encode" ]; then opts=${encodingopts} COMPREPLY=($(compgen -W "${opts}" -- ${cur})) return 0 fi ;; esac fi COMPREPLY=( $(compgen -f "${cur}") ) return 0 } [ "$have" ] && complete -F _xmlstarlet xmlstarlet