【问题标题】:Running Jython script from terminal with parameter从带有参数的终端运行 Jython 脚本
【发布时间】:2016-12-16 16:59:59
【问题描述】:

我想从命令行调用 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

【问题讨论】:

    标签: python jython imagej


    【解决方案1】:

    参数可用的方式取决于调用命令,区别在于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'

    【讨论】:

      猜你喜欢
      • 2020-01-02
      • 2020-12-18
      • 1970-01-01
      • 2015-11-20
      • 1970-01-01
      • 2012-08-21
      • 2014-03-18
      • 2020-11-27
      相关资源
      最近更新 更多