使用optcomplete模块时无法获得bash完成

时间:2012-02-24 21:46:37

标签: python bash-completion

我正在尝试使用 python-optcomplete 包(来自Debian测试的 1.2-11.1 ):

$ cat /etc/bash_completion.d/optcomplete 
_optcomplete()
{
    COMPREPLY=( $( \
    COMP_LINE=$COMP_LINE  COMP_POINT=$COMP_POINT \
    COMP_WORDS="${COMP_WORDS[*]}"  COMP_CWORD=$COMP_CWORD \
    OPTPARSE_AUTO_COMPLETE=1 $1 ) )
}
$ source /etc/bash_completion.d/optcomplete 
$ complete -F _optcomplete optcomplete-test
$ pwd
/tmp/examples
$ ls -l
total 8
-rwxr-xr-x 1 wena wena 3490 Feb 24 23:25 optcomplete-conditional
-rwxr-xr-x 1 wena wena 3521 Feb 24 23:25 optcomplete-simple
$ echo $PATH 
/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games
$ export PATH=/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games:/tmp/examples
$ echo $PATH 
/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games:/tmp/examples
$ optcomplete-simple -[TAB][TAB]

在尝试完成时我什么都没得到......我没有this guy那么幸运。

这是(剪切的)代码as taken from the project website(文件名:“optcomplete-simple”):

#!/usr/bin/env python
import os
import optparse, optcomplete

def main():
    parser = optparse.OptionParser()
    parser.add_option('-s', '--simple', action='store_true',
                      help="Simple really simple option without argument.")
    parser.add_option('-o', '--output', action='store',
                      help="Option that requires an argument.")
    opt = parser.add_option('-p', '--script', action='store',
                            help="Option that takes python scripts args only.")
    opt.completer = optcomplete.RegexCompleter('.*\.py')
    optcomplete.autocomplete(parser, ['.*\.tar.*'])
    opts, args = parser.parse_args()

if __name__ == '__main__':
    main()

1 个答案:

答案 0 :(得分:4)

您将完成脚本绑定到optcomplete-test,但正在使用optcomplete-simple进行测试。请改用:

source /etc/bash_completion.d/optcomplete
complete -F _optcomplete optcomplete-simple

请注意(以防万一)更改$PATH并非绝对必要。您可以通过其绝对路径或./optcomplete-simple运行脚本,并完成工作。此外,将目录添加到当前$PATH的标准方法是:

export PATH=/new/directory:$PATH