首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在不显示此目录的情况下自动完成具有特定目录的文件路径的bash命令行?

如何在不显示此目录的情况下自动完成具有特定目录的文件路径的bash命令行?
EN

Stack Overflow用户
提问于 2017-06-09 08:56:24
回答 2查看 2.6K关注 0票数 4

您可以在这里找到一个相关的问题:如何使用文件路径自动完成bash命令行?

上下文

我正在创建一个shell程序,它是一个命令行工具。我想为这个工具创建我自己的自动完成。

对于选项--单元测试和-t,我想从一个特定的目录自动完成文件路径,我可以运行my_app -目录。

,例如

Run:

代码语言:javascript
复制
user@computer:~$ my_app --install [TAB][TAB]

就可以做到:

代码语言:javascript
复制
Public/          bin/                 Desktop/              
Documents/       Music/               Downloads/
user@computer:~$ my_app --install 

(显示当前目录)

Run:

代码语言:javascript
复制
user@computer:~$ my_app --unit-tests [TAB][TAB]

就可以做到:

代码语言:javascript
复制
folder/              folder2/             folder3/
.hidden_file         file.extension       file2.extension
user@computer:~$ my_app --unit-tests 

(在没有完整目录的情况下显示特定目录的建议)

my_app_autocomplete文件

代码语言:javascript
复制
__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开头添加

代码语言:javascript
复制
_directory=/absolute/path/to/the/directory/  

然后在compgen命令中替换变量。

代码语言:javascript
复制
# 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

代码语言:javascript
复制
user@computer:~$ my_app --unit-tests [TAB][TAB]

代码语言:javascript
复制
user@computer:~$ my_app --unit-tests /absolute/path/to/the/directory/

它将路径添加到目录中。

Run

代码语言:javascript
复制
user@computer:~$ my_app --unit-tests /absolute/path/to/the/directory/file.ext[TAB][TAB]

代码语言:javascript
复制
user@computer:~$ my_app --unit-tests /absolute/path/to/the/directory/

它移除file.ext部件。

问题:

  • 我不想在命令行中添加特定的路径
  • 它删除了我在特定目录之后添加的内容,而不是自动完成它。
EN

回答 2

Stack Overflow用户

发布于 2017-12-13 18:25:39

经过多次尝试和错误之后,我想我已经找到了解决你问题的方法(这也是我的问题):

代码语言:javascript
复制
_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 (而不是以-开头)。

现在,您可以为所需的每个目录声明一个完成函数,如:

代码语言:javascript
复制
_complete_git_home_path() {
  _complete_specific_path "${GIT_HOME}"
}

并将其附加到正确的命令:

代码语言:javascript
复制
complete -F _complete_git_home_path cdrepo lsrepo rmrepo cdwiki pyinst

或者在您自己的完成函数中使用它,为特定的选项(如--unit-test )触发它!

票数 3
EN

Stack Overflow用户

发布于 2021-07-04 19:19:40

@pawamoy答复的改进

打电话时:

代码语言:javascript
复制
_init_completion -s || return

如果_init_completion返回一个非空值,脚本将退出而不执行popd命令,这可能会使您处于调用pushd时指定的目录中(但它甚至会使我的终端崩溃!)我建议改为这样做(请参阅分组命令中的{}解释)

代码语言:javascript
复制
_init_completion -s || { popd > /dev/null 2>&1; return; }

此外,如果您的目标是可移植性,&>重定向是不可移植的,因为它不是官方POSIX规范(见这个答案)的一部分,您应该使用

代码语言:javascript
复制
> /dev/null 2>&1

而不是

代码语言:javascript
复制
&> /dev/null
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44453510

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档