sending command line options to script

Post any questions, ideas, or topics related to Jython and Python scripting.
Post Reply
User avatar
joleenf
Posts: 1123
Joined: Mon Jan 19, 2009 7:16 pm

sending command line options to script

Post by joleenf »

Hi,

What is the best way to read command line options into a script when running from the command line with McIDAS-V. I set up an argument parser so that I would call my script with something like call_imgSetUp.py --time 11:45 at the command line if I were just running in python. However, I need McIDAS-V to execute the McIDAS-V actions. This does not work

$HOME/Applications/McIDAS-V-System/runMcV -pyfile $HOME/mcidas/scripts/call_imgSetUp.py --time 11:45.

Is there anything that would?

Joleen
User avatar
tomw
Posts: 296
Joined: Tue Dec 23, 2008 3:40 pm

Re: sending command line options to script

Post by tomw »

joleenf wrote:$HOME/Applications/McIDAS-V-System/runMcV -pyfile $HOME/mcidas/scripts/call_imgSetUp.py --time 11:45.


This does not work because the Java "main" is what reads all the command line parameters...including the "--time 11:45".

While there are a few options, this one looks easiest to use:

set your parameters into an "environment variable" just before invoking McV, the in your script do:
import os;
a = os.envrion["MYSTUFF"];
and variable "a" will get the String corresponding to the contents of the environment variable MYSTUFF.
User avatar
Jon
Posts: 192
Joined: Fri Jan 09, 2009 8:44 pm
Location: Madison, WI

Passing Command Line Arguments to a Jython Script.

Post by Jon »

This functionality requires the presence of a Jython script ("-script"/"-pyfile"). Any commandline arguments after "-scriptags" will be considered specific to a script and will be ignored by the rest of McIDAS-V. This means that "-scriptargs <jython args>" must be at the end of your command line!

The Jython arguments will then populate the "sys.argv" list just like a normal Python script. This frees you up to use things like optparse or argparse (once we incorporate Jython 2.7).

For example, running the following:

Code: Select all

runMcV -script /tmp/test.py -scriptargs foo --blah=True


Results in the sys.argv for "/tmp/test.py" looking like:

Code: Select all

["/tmp/test.py", "foo", "--blah=True"]
User avatar
joleenf
Posts: 1123
Joined: Mon Jan 19, 2009 7:16 pm

Re: sending command line options to script

Post by joleenf »

Hi Jon,

I know this is basic, but in the case of the McV scripts, is is correct to parse command line arguments such as --blah=True using optparse as opposed to the new argparse which would not be available within Jython?

Joleen
User avatar
Jon
Posts: 192
Joined: Fri Jan 09, 2009 8:44 pm
Location: Madison, WI

Re: sending command line options to script

Post by Jon »

McV is currently using Jython 2.5.2…which means that only optparse is included within the Python standard library. If you are not especially worried about the "portability" of your scripts, you could always download argparse (apparently compatible with Python 2.3+) and treat it like any other Python module.

Again, just be aware that doing so would require the presence of "argparse" for all McV environments running your code! I think just using optparse is a much better idea.

[ if only Jython 2.7 would hurry up and finish up its beta! ]
User avatar
joleenf
Posts: 1123
Joined: Mon Jan 19, 2009 7:16 pm

Re: sending command line options to script

Post by joleenf »

Hi Jon,

I got optparse working, so that will be fine.

Joleen
Post Reply