Reputation: 354
I am trying to set up Bash tab completion inside of a python script but cannot seem to get the completion to persist once the script is over. What I am trying right now is:
from subprocess import call
options = {'option1' : option1,
'option2' : option2}
all_options = string.join(options.keys())
call('complete -W "%s" -f python_cmd' %all_options, shell=True)
Where python_cmd is a python script that takes as its first argument one of the options. After running this script the options will not complete after the command. This works fine if I call the complete command directly from the command line but I need to be able to call it from python. I am guessing that this has something to do with call() ending with the script. Anyone know how I can make this persist after the script has ended?
Upvotes: 0
Views: 89
Reputation: 354
I have found something that works. I updated my question to better reflect the purpose of what I am trying to do. I want to be able to get options from a dictionary that will be completed on the command line when running a python script that uses the options. The reason for this is that options will be added or changed often and so I didnt want the users to have to either add to their bashrc or resource their bashrc every time they run the setup script for this command. What I found that works is adding only one line to the bashrc with an eval and backticks, so if I have a script that gets the options like in the answer:
## getOptions.py
import sys
options = {'option1' : option1,
'option2' : option2}
all_options = string.join(options.keys())
sys.stdout.write('complete -W "%s" -f cmd' %all_options)
Then I can add the line to the bashrc (or run from a setup script):
eval `getOptions.py` #backticks, not single quotes
This has set the autocomplete correctly and works for environment variables as well. I dont know if this is the best way to do this but it works and any changes that go into the python will automatically get put in the complete command when running the setup script or logging in if its in the bashrc.
Upvotes: 2