I want to write a bash_completion script for my own file system. I have client program which sends queries to some DB.
example:
my_prog --ls db_name:/foo/bar/
this command writes to stdout list of files in db_name:/foo/bar
folder.
I want to enable autocompletion for this. So when i press tab it shows list of options.
my_prog --ls db_name:/foo/bar/<tab>
but in this case when I press tab and there is single option it replaces the current entered path, so I'm getting this:
$ my_prog --ls db_name:/foo/bar/<tab>
$ my_prog --ls file
But I want the match to be added to end of entered path.
Here is my completion function:
__complete_path()
{
COMPREPLY=()
if [[ ${1} == "" ]]
then
COMPREPLY=( "/" )
compopt -o nospace
return
fi
base=${1##*/}
dir=${1%/*}
options="my_prog --ls ${db}:${dir}"
COMPREPLY=( $(compgen -W "${options}" -- ${base} ) )
compopt -o nospace
}
I found this thread while trying something similar. This stackexchange post helped me in putting the autocomplete function below together. It isn't quite as the "normal" autocompletion as the full paths are shown, but otherwise you may find it useful.
_complete_func()
local cur prev opts
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"
if [ $COMP_CWORD -eq 1 ]; then
opts="some options for the program"
COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
elif [ $COMP_CWORD -ge 2 ]; then
local files=("${cur}"*)
COMPREPLY=( "${files[@]}")
fi
}
complete -o nospace -F complete_func command_to_autocomplete
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.