0

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?

3
  • 3
    You can't. The shell session you ran it in is gone when call finishes. Commented Sep 1, 2015 at 1:21
  • Also, this sounds a lot like an XY problem. What are you actually trying to accomplish? Why would you use Python to start a shell to run a command when the shell itself is very specifically made for doing that? Obviously, if you want real persistence, write out those commands to the end of your .bashrc or similar. Commented Sep 1, 2015 at 10:16
  • The reason for doing this is that the command being run is a python script where all of the options come from a python dictionary. This way whenever new options are added they will be put into the complete command when running a setup script automatically instead of the user having to add them manually. Commented Sep 1, 2015 at 11:21

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

If anyone has a nicer answer I would love to hear it!
Rather than calling complete -W and passing it a wordlist, maybe it would be easier to call complete -C (directly from bashc or in a script sourced from there) and giving it a way to invoke your script to get completions? That way they will always be up to date.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.