I have a function:
def foo(a=0, b=0, c=0, val=0, someotherval=0):
print val + someotherval
This function is called inside a file bar.py
. When I run bar.py
from the console, I want to pass the arguments to the function as a string:
>>python bar.py "val=3"
So the function foo
interprets it as:
foo(val=3)
I have attempted to use the exec
command. In my bar.py
file:
import sys
cmdlinearg = sys.argv[1] # capturing commandline argument
foo(exec(cmdlinearg))
But I get a syntax error.
I understand that I can just pass the argument values themselves, but with a function with many arguments, I do not want the end user to enter 0s for unneeded arguments:
>>python bar.py "0" "0" "0" "3"
Is there a way to accomplish this?