I have been coming to stackoverflow for a while now and am very grateful for this incredible resource as it has helped me on countless occasions, but this is my first question so please have mercy on me ;)
I am trying to pass command line options to my python script when running it from the command line with the "open" command. I understand that I can simply use $ python myscript.py [arg1] [arg2] but I am really trying to use the "open" command if possible.
Here is my python script (myscript.py):
#!/usr/local/bin/python
import optparse
import sys
def main():
parser = optparse.OptionParser()
parser.add_option('--arg1', action="store", dest="arg1", type="int", default="0")
parser.add_option('--arg2', action="store", dest="arg2", type="int", default="0")
opts, remainder = parser.parse_args()
print "Options: ", str(opts)
print "Remainders: ", str(remainder)
print "Argv: ", sys.argv[:]
if __name__ == "__main__":
main()
Here is the command I am running in Terminal:
$ open -a terminal myscript.py --args --arg1=5 --arg2=10
And the output from that:
Options: {'arg1': 0, 'arg2': 0}
Remainders: []
Argv: ['/Users/test/Desktop/myscript.py']
So the script is running, but the "--args" are not being passed in and the script is just using the defaults for the arguments! Here is the documentation on the --args option for the open command:
--args All remaining arguments are passed in argv to the application's main() function instead of opened.
I have tried all kinds of things to try and get this to work but I cannot get arguments into my script via "open". Thank you in advance for any guidance or advice on how to get this to work.