Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Line completion with custom commands

my Python program can be launched with a range of different options (or subcommands) like:

$ myProgram doSomething $ myProgram doSomethingElse $ myProgram nowDoSomethingDifferent 

I want it to use auto-completion with so that if i type "myProgram d" it returns "myProgram doSomething" and if i type "myProgram n" it renders "myProgram nowDoSomethingDifferent". This is similar to the average use of the module rlcompleter, but it does not pick possible completion options from the filesystem (or from history) but from a custom set of strings (that correspond to the available options for my program)

Any idea on how to implement this?

I'm aware of the variable PYTHONSTARTUP (that should point to a file I don't know how to write).

As a working example, django-admin (from the django package) has the same exact feature i'm looking for

like image 455
pistacchio Avatar asked Jan 09 '09 09:01

pistacchio


People also ask

How does command line completion work?

Command-line completion allows the user to type the first few characters of a command, program, or filename, and press a completion key (normally Tab ↹ ) to fill in the rest of the item. The user then presses Return or ↵ Enter to run the command or open the file.

How do I create a custom bash completion?

If you want to enable the completion for all users, you can just copy the script under /etc/bash_completion. d/ and it will automatically be loaded by Bash.


1 Answers

Create a file "myprog-completion.bash" and source it in your .bashrc file. Something like this to get you started...

_myProgram() {   cur=${COMP_WORDS[COMP_CWORD]}   case "${cur}" in     d*) use="doSomething" ;;     n*) use="nowDoSomethingElse" ;;   esac   COMPREPLY=( $( compgen -W "$use" -- $cur ) ) } complete -o default -o nospace -F _myProgram  myProgram 
like image 100
richq Avatar answered Sep 21 '22 13:09

richq