TAB自动补全Python命令行界面

9
我想知道是否有可能编写一个在shell中运行的Python脚本,当用户按Tab键时,可以为他们提供建议?
例如,某些应用程序如何通过支持的文件类型来限制建议。我在optParse中没有发现任何这样的内容。
理想情况下是:
myScript.py [TAB] (shell打印选项列表)
有什么建议吗?具体使用OpenSuse和tcsh下的KDE。
非常感谢

看一下 cmd 模块。具体来说:http://docs.python.org/2/library/cmd.html#cmd.Cmd.completedefault - mgilson
@KirkStrauser在这里展示了使用 readline 的方法:[https://dev59.com/lUXRa4cB1Zd3GeqPsYa4#209503]。 - unutbu
看起来这两个解决方案都需要先执行Python文件?我能不能这样做:
myScript.py [TAB] 解决方案列表
不过还是谢谢你提供的链接,很有用。
- Dhruv Govil
4个回答

5

这是shell的一个功能,与被调用的Python脚本无关。有关shell自动补全的更多信息,请参见此SO上的问题。特别是,您需要寻找可编程完成


是的,我也想到可能是这样,但只是想确认是否有可能将其全部包含在内。谢谢! - Dhruv Govil

2
我想你正在寻找类似于optcomplete的东西。它为Bash实现了一个补全模块,可以自动补全任何使用optparse的Python程序的选项。你可以从中获取灵感,并将其转换为你需要的内容。

那看起来确实很有用,下面的compleat也是如此。我会在家里尝试它,因为我有bash/zsh,但工作上只能使用tcsh,这是一个痛苦的事情,因为bash/zsh拥有更好的社区支持。 - Dhruv Govil
如果tcsh具有可编程完成功能,您可能可以调整optcomplete以为您完成工作。 - Noufal Ibrahim

1

有一个非常好的关于如何在Git中进行此操作的解释。我将其嵌入式包含在此处,但是原作者为 original author

# Source this script in tcsh to setup shell completions
# for git. Completions are activated by typing or Control-D
# in the shell after entering a partial command.
#
# Usage:
# source git-completion.tcsh (e.g. in ~/.cshrc)
#
# Supported completions:
# git (lists git commands)
# git help (lists git commands)
# git branch (lists branch names)
# git checkout (lists branch names)
# git log (lists the most commonly used flags)
# git remote (lists git remote commands)
# git remote add|prune|rm|show|update
# (lists git remote names)
# In addition to entering where shown, you can enter it after
# typing part of the word, e.g. git branch bug to auto-complete
# branches starting with “bug”.
#
# Author: David Adler, David Aguilar

if ( -x /usr/bin/git) then
# Git is installed so define tcsh completions for it.

# List of known git subcommands
# This is a hard-coded list to avoid calling ‘git help’ at startup.
set __git_cmd_names = (add bisect blame branch checkout clone commit config \
diff diff-files difftool fetch grep gui init log merge mv pull push \
rebase reset rm show shortlog stash status tag)

alias __git_aliases ‘git config –get-regexp “alias.*” | sed -n “s,alias\.\([^ ]*\).*,\1,p”‘
alias __git_branches ‘git for-each-ref –format=”%(refname)” refs/heads refs/remotes | sed -e s,refs/remotes/,, | sed -e s,refs/heads/,,’
alias __git_origin_branches ‘git for-each-ref –format=”%(refname)” refs/remotes/origin | grep -v HEAD | sed -e s,refs/remotes/origin/,,’

# Define the completions (see the tcsh man page).
complete git \
‘p/1/`__git_aliases | xargs echo $__git_cmd_names`/’ \
“n/help/($__git_cmd_names)/” \
‘n/branch/`__git_branches | xargs echo -m -d`/’ \
‘n/config/(–global –get-regexp –list)/’ \
‘n/diff/`__git_branches | xargs echo –check –staged –stat — *`/’ \
‘n/difftool/`__git_branches | xargs echo –no-prompt –staged — *`/’ \
‘n/fetch/`git remote`/’ \
‘n/merge/`__git_branches`/’ \
‘n/log/`__git_branches | xargs echo — –name-only –name-status –reverse –committer= –no-color –relative –ignore-space-change –ignore-space-at-eol –format=medium –format=full –format=fuller`/’ \
‘n/stash/(apply list save pop clear)/’ \
‘n/push/`git remote`/’ \
‘N/push/`__git_origin_branches`/’ \
‘n/pull/`git remote | xargs echo –rebase`/’ \
‘n/–rebase/`git remote`/’ \
‘N/–rebase/`__git_origin_branches`/’ \
‘N/pull/`__git_origin_branches`/’ \
‘n/rebase/`__git_branches | xargs echocontinue –abort –onto –skip –interactive`/’ \
‘N/rebase/`__git_branches`/’ \
‘n/remote/(show add rm prune update)/’ \
‘N/remote/`git remote`/’ \
‘n/checkout/`__git_branches | xargs echo -b –`/’ \
‘N/-b/`__git_branches`/’
endif

谢谢。我会仔细看一下,看看能否让它为我工作。祝好! - Dhruv Govil

1

compleat 可以通过简单的使用说明在 bash 中启用制表符自动完成。!command 语法应该允许动态过滤文件名。


我可能会在家里尝试设置它,因为我可以访问zsh和bash。但是工作严格使用tcsh,而且看起来它依赖于一些bash的基础。 - Dhruv Govil

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接