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
sending command line options to script
Re: sending command line options to script
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.
Passing Command Line Arguments to a Jython Script.
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:
Results in the sys.argv for "/tmp/test.py" looking like:
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"]
Re: sending command line options to script
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
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
Re: sending command line options to script
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! ]
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! ]
Re: sending command line options to script
Hi Jon,
I got optparse working, so that will be fine.
Joleen
I got optparse working, so that will be fine.
Joleen