您可以在这里找到一个相关的问题:如何使用文件路径自动完成bash命令行?
上下文
我正在创建一个shell程序,它是一个命令行工具。我想为这个工具创建我自己的自动完成。
对于选项--单元测试和-t,我想从一个特定的目录自动完成文件路径,我可以运行my_app -目录。
,例如
Run:
user@computer:~$ my_app --install [TAB][TAB]就可以做到:
Public/ bin/ Desktop/
Documents/ Music/ Downloads/
user@computer:~$ my_app --install (显示当前目录)
Run:
user@computer:~$ my_app --unit-tests [TAB][TAB]就可以做到:
folder/ folder2/ folder3/
.hidden_file file.extension file2.extension
user@computer:~$ my_app --unit-tests (在没有完整目录的情况下显示特定目录的建议)
my_app_autocomplete文件
__my_app_autocomplete()
{
local cur prev opts
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"
opts="--help -h --install -i --run -r --rebuild -rb --show-running-containers -ps --stop -s --remove -rm --logs -l --bash -b --sass -css --unit-tests -t"
containers="nginx php mysql mongo node"
sass="watch"
# By default, autocomplete with options
if [[ ${prev} == my_app ]] ; then
COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
return 0
fi
# By default, autocomplete with options
if [[ ${cur} == -* ]] ; then
COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
return 0
fi
# For --install and -i options, autocomplete with folder
if [ ${prev} == --install ] || [ ${prev} == -i ] ; then
COMPREPLY=( $(compgen -d -- ${cur}) )
return 0
fi
# For --stop --remove --logs and --bash, autocomplete with containers
if [ ${prev} == --stop ] || [ ${prev} == -s ] || [ ${prev} == --remove ] || [ ${prev} == -rm ] || [ ${prev} == --logs ] || [ ${prev} == -l ] || [ ${prev} == --bash ] || [ ${prev} == -b ] ; then
COMPREPLY=( $(compgen -W "${containers}" -- ${cur}) )
return 0
fi
# For --sass and -css, complete with sass options
if [ ${prev} == --sass ] || [ ${prev} == -css ] ; then
COMPREPLY=( $(compgen -W "${sass}" -- ${cur}) )
return 0
fi
# For --unit-tests and -t, complete from a specific folder
if [ ${prev} == --unit-tests ] || [ ${prev} == -t ] ; then
COMPREPLY=( $(compgen -d -- ${cur}) )
return 0
fi
}
complete -o filenames -F __my_app_autocomplete my_app问题
我找不到办法去做。你有什么想法吗?
调查
使用包含特定目录的变量
@D‘’Arcy建议
在my_app_autocomplete开头添加
_directory=/absolute/path/to/the/directory/ 然后在compgen命令中替换变量。
# For --unit-tests and -t, complete with relative to my_app folder paths
if [ ${prev} == --unit-tests ] || [ ${prev} == -t ] ; then
COMPREPLY=( $(compgen -d -- "${_directory}") )
return 0
fi行为:
Run
user@computer:~$ my_app --unit-tests [TAB][TAB]做
user@computer:~$ my_app --unit-tests /absolute/path/to/the/directory/它将路径添加到目录中。
Run
user@computer:~$ my_app --unit-tests /absolute/path/to/the/directory/file.ext[TAB][TAB]做
user@computer:~$ my_app --unit-tests /absolute/path/to/the/directory/它移除file.ext部件。
问题:
发布于 2017-12-13 18:25:39
经过多次尝试和错误之后,我想我已经找到了解决你问题的方法(这也是我的问题):
_complete_specific_path() {
# declare variables
local _item _COMPREPLY _old_pwd
# if we already are in the completed directory, skip this part
if [ "${PWD}" != "$1" ]; then
_old_pwd="${PWD}"
# magic here: go the specific directory!
pushd "$1" &>/dev/null || return
# init completion and run _filedir inside specific directory
_init_completion -s || return
_filedir
# iterate on original replies
for _item in "${COMPREPLY[@]}"; do
# this check seems complicated, but it handles the case
# where you have files/dirs of the same name
# in the current directory and in the completed one:
# we want only one "/" appended
if [ -d "${_item}" ] && [[ "${_item}" != */ ]] && [ ! -d "${_old_pwd}/${_item}" ]; then
# append a slash if directory
_COMPREPLY+=("${_item}/")
else
_COMPREPLY+=("${_item}")
fi
done
# popd as early as possible
popd &>/dev/null
# if only one reply and it is a directory, don't append a space
# (don't know why we must check for length == 2 though)
if [ ${#_COMPREPLY[@]} -eq 2 ]; then
if [[ "${_COMPREPLY}" == */ ]]; then
compopt -o nospace
fi
fi
# set the values in the right COMPREPLY variable
COMPREPLY=( "${_COMPREPLY[@]}" )
# clean up
unset _COMPREPLY
unset _item
else
# we already are in the completed directory, easy
_init_completion -s || return
_filedir
fi
}通过查看cat是如何自动完成的,我找到了这个解决方案。它使用_longopt函数,该函数反过来对非选项的参数使用_filedir (而不是以-开头)。
现在,您可以为所需的每个目录声明一个完成函数,如:
_complete_git_home_path() {
_complete_specific_path "${GIT_HOME}"
}并将其附加到正确的命令:
complete -F _complete_git_home_path cdrepo lsrepo rmrepo cdwiki pyinst或者在您自己的完成函数中使用它,为特定的选项(如--unit-test )触发它!
发布于 2021-07-04 19:19:40
对@pawamoy答复的改进
打电话时:
_init_completion -s || return如果_init_completion返回一个非空值,脚本将退出而不执行popd命令,这可能会使您处于调用pushd时指定的目录中(但它甚至会使我的终端崩溃!)我建议改为这样做(请参阅分组命令中的{}解释)
_init_completion -s || { popd > /dev/null 2>&1; return; }此外,如果您的目标是可移植性,&>重定向是不可移植的,因为它不是官方POSIX规范(见这个答案)的一部分,您应该使用
> /dev/null 2>&1而不是
&> /dev/nullhttps://stackoverflow.com/questions/44453510
复制相似问题