[英]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 假设我有一个名为
script.py
的程序,它允许使用命令行选项-i
,这需要一个额外的关键字参数,这样我就可以调用
python script.py -i foo
Now assume in my working directory I have a file named tags.txt
which includes a list of strings. 现在假设我的工作目录中有一个名为
tags.txt
的文件,其中包含一个字符串列表。 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
. 现在我希望shell在根据
tags.txt
给出的字符串列表调用脚本时自动完成-i
之后的任何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. 这个想法是在该文件中存储
-i
一些常见输入参数,以减少键入错误并确保更快的输入。 Is that possible with a pure Python solution? 这是纯粹的Python解决方案吗?
In bash the auto completer can run a command or bash function to dynamically generate the auto completer list. 在bash中,auto Completer可以运行命令或bash函数来动态生成自动完成列表。 Let's say your script is called foo.
假设您的脚本名为foo。 One option would be create a script called foo_completer.
一种选择是创建一个名为foo_completer的脚本。 You would register it like this in your .bash_profile:
您可以在.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. 现在,只要您使用foo命令选中自动完成,就会从bash调用foo_completer。 But using the -C isn't the only way to create a dynamic completer.
但是使用-C并不是创建动态完成符的唯一方法。 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. 我见过的最复杂的完成者之一是用python编写的aws cli自动完成器 。
Since you are using python check out the python argcomplete project. 因为你正在使用python检查python argcomplete项目。
Here is another example 这是另一个例子
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. 最接近我能想到的(再次同意一个章节所提出的另一个观点,这更像是一个shell问题/答案而不是Python),而不是tags.txt是一个文件,创建它作为一个目录,sym链接到你想要的文件。 But, to get autocompletion you will need to include the directory name.
但是,要获得自动完成功能,您需要包含目录名称。
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. 虽然,它是更多与命令行相关的问题和问题的解决方案,而不是基于Python的,但是可以通过在命令行和Python脚本上指定
file_name
作为可选参数,使用Python实现此目的的另一种方法将从file_name
参数中指定的文件中读取参数并更新参数变量。 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. 如果未定义
file_name
参数,则期望通过命令行指定其他参数,您可能必须验证是否获得了所有参数的值。
It is recommended to go for command-line (Bash/Shell) oriented solutions like using complete
command or any other similar tool. 建议使用命令行(Bash / Shell)定向解决方案,例如使用
complete
命令或任何其他类似工具。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.