6

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?

tripleee
  • 175,061
  • 34
  • 275
  • 318
marc
  • 81
  • 1
  • 5
  • What shell are you using? What is your OS? –  Jul 21 '14 at 11:31
  • This appears to be a shell programming question, not a Python programming question. You need to specify which shell(s) you want this for and explain how a "pure Python" solution would work, or change this requirement. – tripleee Jul 21 '14 at 11:32
  • I am using a bash shell under MAC OS X. I am working on a sort of file managing software package which is to be implemented in python. – marc Jul 21 '14 at 11:40
  • 4
    You are calling your script from `bash`, so any argument completions must be handled by `bash` prior to running Python. Unless you are using Python for your command shell, there is no pure Python solution. – chepner Jul 21 '14 at 13:13
  • 3
    The answers to this question details a couple of different ways to provide tab completion of arguments -- http://stackoverflow.com/questions/427472/line-completion-with-custom-commands – Dunes Jul 21 '14 at 22:15
  • 1
    @shuttle87 That bounty doesn't make sense until the question is clarified. As it is, the answer is just "No, you can't do this purely in Python". No amount of attention on the question will help clarify it until the author does it. – Benjamin W. Aug 12 '19 at 15:55

3 Answers3

7

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

WaltDe
  • 1,715
  • 8
  • 17
1

Closest I can think of (again to concur with another point made by one the chaps, this is more a shell question/answer than Python), Instead of tags.txt being a file, create it as a directory, with sym links to the files you want. But, to get autocompletion you will need to include the directory name.

TenG
  • 3,843
  • 2
  • 25
  • 42
  • -1 That's not necessary or a particularly useful workaround. The autocompletion feature of Bash is eminently customizable. Write your own completion code like in the linked question in @Dunes' comment. – tripleee Jul 22 '14 at 05:36
0

Although, it is more command-line related question and the solution to the problem and less Python-based but there could be one more approach to achieve this using Python by specifying the file_name as an optional argument on the command-line and your Python script will read the arguments from the file specified in the file_name argument and update the argument variables. If the file_name argument is not defined, it will be expecting other arguments to be specified via the command-line and you may have to validate whether you got the values for all the arguments or not.

It is recommended to go for command-line (Bash/Shell) oriented solutions like using complete command or any other similar tool.

Abdullah Khawer
  • 4,461
  • 4
  • 29
  • 66