python-swiftclient/tools/swift.bash_completion
Matthew Oliver 45ed21c6c4 Add bash_completion to swiftclient
This patch basically follows the bash completion
model that other OpenStack clients use. It creates
a new command to swiftclient called `bash_completion`.

The `bash_completion` command by default will print
all base flags and exsiting commands. If you pass
it a command, it'll print out all base flags and
any flags that command accepts. So as you type out
your swift command and auto-complete, only the current
available flags are offered to you.

This is used by the swift.bash_completion script to
allow swift commands to be bash completed.

To make it work, place the swift.bash_completion file
into /etc/bash_completion.d and source it:

  cp tools/swift.bash_completion /etc/bash_completion.d/swift
  source /etc/bash_completion.d/swift

Because swiftclient itself is creating this flag/command output
it should automatically add anything we add to the swiftclient
CLI.

Change-Id: I5609a19018269762b4640403daae5827bb9ad724
2018-07-13 11:24:24 -07:00

33 lines
774 B
Plaintext

declare -a _swift_opts # lazy init
_swift_get_current_opt()
{
local opt
for opt in ${_swift_opts[@]} ; do
if [[ $(echo ${COMP_WORDS[*]} |grep -c " $opt\$") > 0 ]] || [[ $(echo ${COMP_WORDS[*]} |grep -c " $opt ") > 0 ]] ; then
echo $opt
return 0
fi
done
echo ""
return 0
}
_swift()
{
local opt cur prev sflags
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"
if [ "x$_swift_opts" == "x" ] ; then
_swift_opts=(`swift bash_completion "$sbc" | sed -e "s/-[-A-Za-z0-9_]*//g" -e "s/ */ /g"`)
fi
opt="$(_swift_get_current_opt)"
COMPREPLY=($(compgen -W "$(swift bash_completion $opt)" -- ${cur}))
return 0
}
complete -F _swift swift