TAB autocomplete python CLI

I'm just wondering if it is possible to write a Python script that runs in a shell that can provide suggestions to the user when they press Tab?

for example, how certain applications can limit the file types offered by what they support. I did not find anything in optParse that does this?

Ideally, this would be:

myScript.py [TAB] (the shell prints a list of options)

Any suggestions? In particular, using KDE under OpenSuse and tcsh

Very much appreciated

+4
source share
4 answers

This is a shell property, not a Python script call. See this question on SO for more information on shell completion. In particular, you are looking for programmable termination .

+3
source

There's a pretty good explanation on how to do this for git. I include it in the string, but the credit belongs to the 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 (eg 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, eg 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 -es,refs/remotes/,, | sed -es,refs/heads/,,' alias __git_origin_branches 'git for-each-ref –format="%(refname)" refs/remotes/origin | grep -v HEAD | sed -es,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 
+1
source

I think you are looking for something like optcomplete . It implements a completion module for bash that will autocomplete parameters for any Python program that uses optparse . You could get advice from this and convert them into what you need.

+1
source

compleat allows you to tab in bash with a simple usage description. The !command syntax should allow dynamic filtering of file names.

+1
source

All Articles