运行 来自带有参数的终端的 Jython 脚本

Running Jython script from terminal with parameter

我想从命令行调用 Jython 脚本,p.e。 $ /Applications/Fiji.app/Contents/MacOS/ImageJ-macosx --headless little_jython_script.py

我知道 Python(以及 Jython)通过

获取参数的能力
import sys
params = sys.argv[1:]

然后用类似的东西调用脚本 $ /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 示例

# @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

参数可用的方式取决于调用命令,不同之处在于Jython方式中的附加标志--ij2--runsys.argv# @String 等都有效,但不能同时有效

1.使用 sys.argv

的经典 Python 方式

$/Applications/Fiji.app/Contents/MacOS/ImageJ-macosx --headless JythonScript.py param1 param2

sys.argv 的经典 python 方式收集 JythonScript.py 的参数,即

# @String param1     ### Does NOT work

import sys
program_name = sys.argv[0]
paramvalue1  = sys.argv[1]
paramvalue2  = sys.argv[2]

2。使用 # @String 等的 Jython 特定方法

$/Applications/Fiji.app/Contents/MacOS/ImageJ-macosx --ij2 --headless --run JythonScript_2.py 'param1=value, param2=value'

以 Jython 方式获取参数

# @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 对周围的引号。单引号和双引号都有效。当仅存在 1 个参数时,可以省略它们。对于字符串参数,请确保将它们括在其他类型的引号中,或者当字符串是纯字母数字时省略引号,例如 $/Applications/Fiji.app/Contents/MacOS/ImageJ-macosx --ij2 --headless --run JythonScript_3.py 'stringparam1="string with ',' and space ", stringparam2=abc123'