【问题标题】:TAB autocomplete python CLITAB 自动完成 python CLI
【发布时间】:2012-11-14 14:48:17
【问题描述】:

我只是想知道是否可以编写一个在 shell 中运行的 Python 脚本,以便在用户点击 Tab 时向他们提供建议?

例如,某些应用程序如何限制它们所支持的文件类型。 我在 optParse 中没有找到任何可以执行此操作的内容?

理想情况下是:

myScript.py [标签] (shell 打印选项列表)

有什么建议吗?具体在OpenSuse和tcsh下使用KDE

非常感谢

【问题讨论】:

  • @KirkStrauser 展示了一种使用 readline here 的方法。
  • 这两种解决方案似乎都需要先执行python文件?有没有办法我可以做:> myScript.py [TAB] 解决方案列表虽然感谢链接。很高兴知道这些。

标签: python shell kde tcsh


【解决方案1】:

这是 shell 的一个特性,而不是被调用的 Python 脚本的特性。有关 shell 完成的更多信息,请参阅 this question on SO。特别是,您正在寻找programmable completion

【讨论】:

  • 是的,我想可能是这样,但只是想检查是否有可能包含所有内容。谢谢!
【解决方案2】:

对于如何为 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 echo –continue –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

【讨论】:

  • 谢谢。我会看看,看看我能不能让它为我工作。干杯
【解决方案3】:

我认为您正在寻找类似optcomplete 的内容。它为 bash 实现了一个完成模块,它将自动完成任何使用 optparse 的 Python 程序的选项。您可以从中获取提示并将其转换为您需要的内容。

【讨论】:

  • 这看起来确实很有用,下面的 compleat 也是如此。我会在家里尝试一下,我有 bash/zsh,但在可预见的未来,工作严格来说是 tcsh。 (这很痛苦,因为 bash/zsh 有更好的社区)
  • 好吧,如果 tcsh 具有可编程完成功能,您也许可以使用 optcomplete 为您完成工作。
【解决方案4】:

compleat 使用简单的用法说明在 bash 中启用制表符补全。 !command 语法应该允许动态过滤文件名。

【讨论】:

  • 我可能会尝试在我可以访问 zsh 和 bash 的家里进行设置。工作严格来说是 tcsh,看起来它依赖于一些 bash 基础
猜你喜欢
  • 2020-11-25
  • 2013-04-04
  • 2010-12-22
  • 2014-05-19
  • 2014-03-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多