[英]Creating a makefile that would create an executable which would run a python script with unlimited amount of arguments
I have a recieved a coding assignment in which I have to create a "simulator", and I can choose whatever language I want.我收到了一个编码任务,我必须在其中创建一个“模拟器”,我可以选择我想要的任何语言。 Naturally, I choosed python.
我自然选择了python。
My problem isn't related to the implementation of the simulator itself, so let's say for simplicity that my python script is called main.py, and all it does is prints all the arguments it receives when I call it in linux shell, something like this:我的问题与模拟器本身的实现无关,所以为了简单起见,我的 python 脚本称为 main.py,它所做的只是打印当我在 linux shell 中调用它时收到的所有 arguments,类似这个:
import sys
for arg in sys.argv:
print(arg)
The problem is, I have to supply a Makefile which will build a simulator executable so that the following syntax will work in the linux shell:问题是,我必须提供一个 Makefile,它将构建一个模拟器可执行文件,以便以下语法在 linux shell 中工作:
make //makes or builds the simulator executable
./simulator arg1 arg2 arg3 arg4 //runs main.py, can receive unlimited amount of args, lets say 4
arg1 arg2 arg3 arg4 //output
./simulator arg1 //this time only 1 argument is provided, should still work
arg1 //output
The tests for the assigment are automated, so this syntax has to work.分配的测试是自动的,所以这个语法必须有效。 There are no workarounds, I can't call my script with its original name or use "python" keyword.
没有解决方法,我不能用它的原始名称调用我的脚本或使用“python”关键字。 It has to be done as shown above.
它必须如上所示完成。 So far, I managed to solve the problem only in case I don't have arguments at all:
到目前为止,只有在我根本没有 arguments 的情况下,我才设法解决问题:
#############makefile#############
simulator: main.py
@touch simulator
@chmod 777 simulator
@echo python main.py >> ./simulator
Of course this solution is not going to work in cases where I have arguments.. Esepecially because there is no way for me to know up front with how many arguments "simulator" is going to be called.当然,这个解决方案在我有 arguments 的情况下不起作用。特别是因为我无法预先知道将调用多少个 arguments“模拟器”。
I tried using symlinks, didn't work very well.我尝试使用符号链接,但效果不佳。
I feel like I'm missing something rather simple, and I would love to get some help from you guys.我觉得我错过了一些相当简单的东西,我很想得到你们的帮助。 Thanks in advance!
提前致谢!
You need to learn about one of two things: either shebang ( #!
) features which you can learn about here and will let you write your script so it's directly runnable from the command line and doesn't require you to run python main.py
.您需要了解以下两件事之一:shebang (
#!
) 功能,您可以在此处了解这些功能,并允许您编写脚本,以便它可以直接从命令行运行,并且不需要您运行python main.py
. Then your makefile can just be:那么你的 makefile 可以是:
simulator: main.py
cp main.py simulator
Or else you have to learn how shell scripts handle command line arguments, which you can learn about here (and many other places).否则你必须学习 shell 脚本如何处理命令行 arguments,你可以在这里(以及许多其他地方)了解它。 But this is much trickier for a number of reasons especially to write in a makefile recipe where you have to escape
$
etc.但由于多种原因,这要复杂得多,尤其是在 makefile 食谱中,你必须转义
$
等。
Thanks to MadScientist's insight, this solution worked for me:感谢 MadScientist 的洞察力,这个解决方案对我有用:
simulator: main.py
@touch simulator
@echo 'python main.py' "\$$@" >> ./simulator
@chmod u+x simulator
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.