0

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.

2 Answers2

1

Your syntax is fine; open is passing the extra arguments to Terminal, but Terminal is ignoring them. For example, this just opens a shell, and doesn't do any echoing:

open -a terminal /bin/bash --args -c 'echo foo bar'

You need to do something different if you want to run a command with arguments in a new Terminal window. Check out the answers to this question for ideas.

Community
  • 1
  • 1
Mark Reed
  • 91,912
  • 16
  • 138
  • 175
  • Thanks so much for the quick response Mark, I can't believe I didn't already come across that thread in my research. I'll study up on that. Thanks again, I really appreciate the help! – ghost-pilot Dec 19 '15 at 17:36
0

Although I still am unclear how to properly use the --args option for the open command, I have found a suitable work around.

echo python /Users/test/Desktop/myscript.py --arg1=5 --arg2=10 > myscript.command; chmod +x myscript.command; open myscript.command

And the output from that:

Options:  {'arg1': 5, 'arg2': 10}
Remainders:  []
Argv:  ['/Users/test/Desktop/opt.py', '--arg1=5', '--arg2=10']

The primary reason I originally wanted to use the open command was to open a new terminal window to run myscript with arguments. This work around does the same thing, but just creates a terminal shell script, makes it a UNIX executable, an uses "open" without --args to run it with python. I would still like to understand how to use --args with open, and also why its not possible to do this without creating a whole other shell script on the fly but I am just happy that its figured out ;) Big thanks to Mark Reed for pointing me in the right direction! I hope this helps somebody else out someday too.