Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Autocomplete command line arguments from a list in a file

Assume I have a program called script.py and it allows for the command line option -i which requires an additional keyword argument, such that I call

python script.py -i foo

Now assume in my working directory I have a file named tags.txt which includes a list of strings. Now I want the shell to autocomplete whatever comes after -i when calling the script according to the list of strings given in tags.txt. The idea is the store some common input arguments for -i in that file in order to reduce typing mistakes and ensure faster input. Is that possible with a pure Python solution?

like image 438
marc Avatar asked Jul 21 '14 11:07

marc


1 Answers

In bash the auto completer can run a command or bash function to dynamically generate the auto completer list. Let's say your script is called foo. One option would be create a script called foo_completer. You would register it like this in your .bash_profile:

complete -C foo_completer foo

Now anytime you tab for autocomplete with the foo command the foo_completer will be called from bash. But using the -C isn't the only way to create a dynamic completer. Here are few links to help you get started.

One of the most complex completer I've seen is the aws cli autocompleter which is written in python.

Since you are using python check out the python argcomplete project.

Here is another example

like image 193
WaltDe Avatar answered Sep 28 '22 09:09

WaltDe