[英]Custom path completion in Bash
我想为自己的文件系统编写一个bash_completion脚本。 我有客户端程序,它向一些数据库发送查询。
例:
my_prog --ls db_name:/foo/bar/
此命令写入db_name:/foo/bar
文件夹中的stdout文件列表。
我想为此启用自动完成功能。 因此,当我按Tab键时,它会显示选项列表。
my_prog --ls db_name:/foo/bar/<tab>
但在这种情况下,当我按Tab键并且有单个选项时它会替换当前输入的路径,所以我得到了这个:
$ my_prog --ls db_name:/foo/bar/<tab>
$ my_prog --ls file
但我希望将匹配添加到输入路径的末尾。
这是我的完成功能:
__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
}
我在尝试类似的东西时找到了这个帖子。 这个 stackexchange帖子帮助我将自动完成功能放在一起。 它不像“正常”自动完成那样显示完整路径,但您可能会发现它很有用。
_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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.