linux – Bash中的自定义路径完成
发布时间:2020-12-13 23:28:43 所属栏目:Linux 来源:网络整理
导读:我想为自己的文件系统编写一个bash_completion脚本.我有客户端程序,它向一些数据库发送查询. 例: my_prog --ls db_name:/foo/bar/ 此命令写入db_name:/ foo / bar文件夹中的stdout文件列表. 我想为此启用自动完成功能.因此,当我按Tab键时,它会显示选项列表
我想为自己的文件系统编写一个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 } 解决方法
我在尝试类似的东西时找到了这个帖子.
This 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 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |