I want to write a bash_completion script for my own file system. I have a client program that sends queries to some DB.
Example:
my_prog --ls db_name:/foo/bar/
this command writes to the list of stdout files in the folder db_name:/foo/bar.
I want to enable autocomplete for this. So when I click the tab, a list of options is displayed.
my_prog --ls db_name:/foo/bar/<tab>
but in this case, when I click the tab and there is a single parameter, it replaces the current path entered, so I get the following:
$ my_prog --ls db_name:/foo/bar/<tab>
$ my_prog --ls file
But I want the match to be added to the end of the path entered.
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
}
source
share