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.

My problem isn't related to the implementation of the simulator itself, so let's say for simplicity that my python script is called, and all it does is prints all the arguments it receives when I call it in linux shell, something like this:

import sys

for arg in sys.argv:

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:

make                                //makes or builds the simulator executable
./simulator arg1 arg2 arg3 arg4     //runs, 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. 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:


    @touch simulator
    @chmod 777 simulator 
    @echo python >> ./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.

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!

2 answers

  • answered 2020-12-02 00:50 MadScientist

    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 Then your makefile can just be:

            cp simulator

    Or else you have to learn how shell scripts handle command line arguments, which you can learn about here (and many other places). But this is much trickier for a number of reasons especially to write in a makefile recipe where you have to escape $ etc.

  • answered 2020-12-02 20:29 Benny Nazimov

    Thanks to MadScientist's insight, this solution worked for me:

        @touch simulator
        @echo 'python' "\$$@" >> ./simulator
        @chmod u+x simulator