简体   繁体   English

创建一个 makefile,它将创建一个可执行文件,该可执行文件将运行一个 python 脚本,其数量不受限制 arguments

[英]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.

相关问题 Apache可以运行不可执行的python脚本吗? - Can apache run a python script which is NOT executable? 简单的python else函数将不会运行,这将导致脚本结束 - simple python else function won't run which would result in the end of of script 使用多个参数从python脚本运行可执行文件 - Run windows executable from python script with multiple arguments 如何创建Flask WebApp,以输入关键字并从已编写的python脚本输出数据? - How would I create a Flask WebApp which inputs keywords and outputs data from a python script which is already written? 使用pyInstaller 2.0创建具有特定参数的python脚本的可执行文件 - Use pyInstaller 2.0 to create executable of python script with specific arguments 我无法直接运行的python脚本的原因是什么? - What would be the cause of a python script that I cannot run directly? 如何在python中创建将存储树的类 - how to create a class in python which would store a tree 在第一次显示输出后,我如何让这个程序无限次地再次接受输入? - how would I get this program to take input again for an unlimited amount of times after the output is displayed the first time? 我想使我的文件成为可执行文件,但是除非使用python,否则它将无法运行? - I would like to make my file an executable however it wont run unless its using python? 为什么这个Python脚本会崩溃? - Why would this Python script crash?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM