使用参数从终端运行Jython脚本 [英] Running Jython script from terminal with parameter
问题描述
我想从命令行p.e.调用Jython脚本.
$ /Applications/Fiji.app/Contents/MacOS/ImageJ-macosx --headless little_jython_script.py
I want to invoke Jython scripts from command line, p.e.
$ /Applications/Fiji.app/Contents/MacOS/ImageJ-macosx --headless little_jython_script.py
我知道Python的(因此是Jython的)通过以下方式获取参数的功能
I know about Python's (and therefore Jython's) capability to take parameters by
import sys
params = sys.argv[1:]
,然后用类似的方法调用脚本
$ /Applications/Fiji.app/Contents/MacOS/ImageJ-macosx --headless jython_script_with_params.py param1 param2 param3
.
and then calling the script with something like
$ /Applications/Fiji.app/Contents/MacOS/ImageJ-macosx --headless jython_script_with_params.py param1 param2 param3
.
但是,根据ImageJ网页 http://imagej.net/Script_parameters ,这也是可能的在Jython中编码参数的用法,类似于该网站上的Greeting.py示例
However, according to to the ImageJ webpage http://imagej.net/Script_parameters it is also possible to code the use of parameters in Jython similar to the Greeting.py example from that website
# @String name
# A Jython script with parameters.
# It is the duty of the scripting framework to harvest
# the 'name' parameter from the user, and then display
# the 'greeting' output parameter, based on its type.
print "Hello, " + name + "!"
问题是:如何在命令行调用$/Applications/Fiji.app/Contents/MacOS/ImageJ-macosx --headless Helloworld.py
中指定参数name
?
The questions is: How do I specify the parameter name
in a command line call $/Applications/Fiji.app/Contents/MacOS/ImageJ-macosx --headless Helloworld.py
?
推荐答案
可用参数的方式取决于调用命令,区别在于Jython方式中的附加标志--ij2
和--run
. sys.argv
或# @String
等都可以工作,但不能同时工作
The way parameters are available depends on the calling command, where the difference is the additional flags --ij2
and --run
in the Jython way. Either sys.argv
or # @String
etc. work, but not both at the same time
1. sys.argv的经典Python方式
$/Applications/Fiji.app/Contents/MacOS/ImageJ-macosx --headless JythonScript.py param1 param2
使用sys.argv
,以经典的python方式获取JythonScript.py
的参数,即
harvests the parameters for JythonScript.py
in the classical python way with sys.argv
, i.e.
# @String param1 ### Does NOT work
import sys
program_name = sys.argv[0]
paramvalue1 = sys.argv[1]
paramvalue2 = sys.argv[2]
2. Jython特定于#@String等的方式.
$/Applications/Fiji.app/Contents/MacOS/ImageJ-macosx --ij2 --headless --run JythonScript_2.py 'param1=value, param2=value'
以Jython方式获取参数
takes parameters in the Jython way
# @String param1
# @Long param2
### See http://imagej.net/Script_parameters#Parameter_types
### for a complete list of parameter types
import sys
check = sys.argv
#here check is a length 1 list containing en empty string: check ==['']
请注意两个逗号分隔的param=value
对周围的引号.单引号和双引号均适用.当仅存在一个参数时,可以将其省略.对于字符串参数,请确保将其括在其他类型的引号中,或者在字符串为纯字母数字形式(例如)时省略引号
$/Applications/Fiji.app/Contents/MacOS/ImageJ-macosx --ij2 --headless --run JythonScript_3.py 'stringparam1="string with ',' and space ", stringparam2=abc123'
Note the quotes around the two comma-separated param=value
pairs. Both single and double quotes work. They can be omitted when only 1 parameter is present. For string parameters make sure to enclose them into the other kind of quotes, or omit the quotes when the string is purely alphanumeric like
$/Applications/Fiji.app/Contents/MacOS/ImageJ-macosx --ij2 --headless --run JythonScript_3.py 'stringparam1="string with ',' and space ", stringparam2=abc123'
这篇关于使用参数从终端运行Jython脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!