7

Tab を押したときにユーザーに提案を提供できるシェルで実行される Python スクリプトを作成できるかどうか疑問に思っていました。

たとえば、特定のアプリケーションが、サポート対象によって提案されるファイル タイプをどのように制限できるかなどです。これを行う optParse には何も見つかりませんでしたか?

理想的には次のようになります。

myScript.py [TAB] (シェルはオプションのリストを出力します)

助言がありますか?具体的には、OpenSuse および tcsh で KDE を使用する

とても有難い

4

4 に答える 4

4

これはシェルの機能であり、呼び出される Python スクリプトの機能ではありません。シェル補完の詳細については、SO に関するこの質問を参照してください。特に、プログラム可能な補完を探しています。

于 2012-11-27T06:53:14.680 に答える
2

optcompleteのようなものを探していると思います。を使用するPythonプログラムのオプションをオートコンプリートするbashの補完モジュールを実装しますoptparse。そこからヒントを得て、必要なものに変換することができます。

于 2012-11-27T06:59:12.867 に答える
1

gitでこれを行う方法についてはかなり良い説明があります。私はそれをインラインで含めていますが、クレジットは元の作者にあります

# 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
于 2012-11-27T06:57:02.790 に答える
1

compleatは、簡単な使用法の説明を使用して、bashでタブ補完を有効にします。!command構文では、ファイル名を動的にフィルタリングできる必要があります。

于 2012-11-27T07:05:03.020 に答える