Passing function parameters of various types from batch script to a python function in a python script

Viewed 27

I'm working with a python script (let's call it MyScript.py) containing a function with many parameters of various types, which I'm not allowed to modify:

def MyFunction(str1, int1, str2, str3, int2, bool1):
    #Do something with input

if __name__ == "__main__":
    eval(sys.argv[1])

And I want to call this function from a batch script with a list of parameters. I tried the ways below but it failed:

python -c "import MyScript;MyScript.MyFunction('str1', 'int1', 'str2', 'str3', 'int2', 'bool1')"
python -c "import MyScript;MyScript.MyFunction([str1], [int1], [str2], [str3], [int2], [bool1])"

How should I call this function then? And what's the correct way to format the input parameters to a python script? Thank you!

1 Answers

From the python --help:

-c cmd : program passed in as string (terminates option list)
             application. Typical usage is python3 -X importtime -c 'import asyncio'

So, when you use the -c flag, you are already running within python, so there is no need to use the eval(sys.argv[1]), you just need to call the function with the parameters (their values):

python -c "import MyScript;MyScript.MyFunction('str1', 1, 'str2', 'str3', 2, True)"
Related