Python 类似于git的test.py自动完成<;选项卡>;
当我用tab发布git时,它可以用一个列表自动完成,我想写一个test.py,当我键入test.py后跟tab时,它可以用test.py中定义的给定列表自动完成,这可能吗Python 类似于git的test.py自动完成<;选项卡>;,python,bash,Python,Bash,当我用tab发布git时,它可以用一个列表自动完成,我想写一个test.py,当我键入test.py后跟tab时,它可以用test.py中定义的给定列表自动完成,这可能吗 $ git [tab] add branch column fetch help mv reflog revert
$ git [tab]
add branch column fetch help mv reflog revert stash
am bundle commit filter-branch imap-send name-rev relink rm status
annotate checkout config format-patch init notes remote send-email submodule
apply cherry credential fsck instaweb p4 repack shortlog subtree
archive cherry-pick describe gc log pull replace show tag
bisect clean diff get-tar-commit-id merge push request-pull show-branch whatchanged
blame clone difftool grep mergetool rebase reset stage
您正在寻找的方法是:
readline.set\u completer
。此方法与bashshell的readline
交互。它很容易实现。示例:这不是git二进制文件本身的一个特性,它是一个bash
完成“hack”,因此与Python本身无关,但既然您已经将其标记为这样,那么让我们添加一点扭曲。假设我们创建了一个脚本,该脚本知道其可接受的参数-test.py
:
#!/usr/bin/env python
import sys
# let's define some sample functions to be called on passed arguments
def f1():
print("F1 called!")
def f2():
print("F2 called!")
def f3():
print("F3 called!")
def f_invalid(): # a simple invalid placeholder function
print("Invalid command!")
def f_list(): # a function to list all valid arguments
print(" ".join(sorted(arguments.keys())))
if __name__ == "__main__": # make sure we're running this as a script
arguments = { # a simple argument map, use argparse or similar in a real world use
"arg1": f1,
"arg2": f2,
"arg3": f3,
"list_arguments": f_list
}
if len(sys.argv) > 1:
for arg in sys.argv[1:]: # loop through all arguments
arguments.get(arg, f_invalid)() # call the mapped or invalid function
else:
print("At least one argument required!")
注意:确保向脚本(chmod+x test.py
)添加一个可执行标志,使其shebang用于执行,而不是作为Python解释器的参数提供
除了所有样板之外,重要的参数是list\u arguments
——它列出了此脚本的所有可用参数,我们将在bash
完成脚本中使用此输出来指示bash
如何自动完成。为此,创建另一个脚本,我们称之为测试完成。bash
:
#!/usr/bin/env bash
SCRIPT_NAME=test.py
SCRIPT_PATH=/path/to/your/script
_complete_script()
{
local cursor options
options=$(${SCRIPT_PATH}/${SCRIPT_NAME} list_arguments)
cursor="${COMP_WORDS[COMP_CWORD]}"
COMPREPLY=( $(compgen -W "${options}" -- ${cursor}) )
return 0
}
complete -F _complete_script ${SCRIPT_NAME}
它所做的基本上是在complete
中添加\u complete\u脚本
函数,以便在调用test.py
完成时调用该函数。\u complete\u脚本
函数本身首先调用test.py
上的list\u arguments
来检索其可接受的参数,然后使用compgen
为complete
创建所需的结构,以便能够将其打印出来
要进行测试,您只需将此脚本的源代码设置为:
source test-completion.bash
然后,您的bash将表现为:
$ ./test.py [tab]
arg1 arg2 arg3 list_arguments
$./test.py[选项卡]
arg1 arg2 arg3列出参数
更重要的是,它完全可以从Python脚本中控制-在list\u arguments
命令中打印为列表的内容将显示为自动完成帮助
要使更改永久化,您只需将源代码
行添加到.bashrc
,或者如果您想要更结构化的解决方案,您可以遵循操作系统的指导原则。例如,本页介绍了几种方法。当然,这假设您的系统上确实安装并启用了bash autocomplete,但是如果您不这样做,git
autocomplete将无法工作
说到git的自动完成,你可以通过检查来了解它是如何实现的——警告一句,这不是给胆小鬼的。如果你想在你的Python IDE中实现它,那么不,我相信不是。最接近的方法是使用git python库,它将为您提供支持的命令,这些命令相当于执行那些git命令。我很确定您可以执行自定义的
complete
脚本,但这肯定是您编写过的最痛苦的事情。我知道这不是一回事,但是你看到模块了吗?它有类似的功能。我不知道这个模块。它似乎功能强大且有用。这不是OP想要的——这只有在运行Python脚本时才会起作用,但它不会在命令行本身上提供任何类型的自动完成。这是我们在Python环境中可以做的最接近的操作(以免bash本身在Python中实现)。您提到的解决方法虽然正确,但考虑到要修改的文件数量,有点复杂。相反,启动python控制台并利用readline
。