I'm looking for a way to redirect output in a groovy script to stderr:
我正在寻找一种方法将groovy脚本中的输出重定向到stderr:
catch(Exception e) {
println "Want this to go to stderr"
}
4 个解决方案
#1
20
Just off the top of my head couldn't you do a bit of self-wiring:
就在我的头顶,你不能做一些自我布线:
def printErr = System.err.&println
printErr("AHHH")
but that is a bit manual
但这有点手册
#2
19
Another quite compact alternative is this:
另一个相当紧凑的替代方案是:
System.err << "Want this to go to stderr"
Or you could add this at the top of your script
或者您可以在脚本的顶部添加它
def err = System.err
...
err << "Want this to go to stderr"
which is what I'm now doing in my groovy shell scripts
这就是我现在在我的groovy shell脚本中所做的事情
#3
16
Groovy has access to the JRE:
Groovy可以访问JRE:
System.err.println "goes to stderr"
Although there may be a more Groovy-fied way...
虽然可能有更多Groovy风格的方式......
#4
1
If you just want something shorter to type, here are two options. First, you can import java.lang.System as anything you like, specifically something shorter like "sys":
如果您只想要更短的类型,这里有两个选项。首先,您可以将java.lang.System导入为您喜欢的任何内容,特别是像“sys”这样的更短的内容:
import java.lang.System as sys
sys.err.println("ERROR Will Robinson")
Second, you can assign the System.err stream to a variable and use that variable from then on as an alias for System.err, like:
其次,您可以将System.err流分配给变量,然后使用该变量作为System.err的别名,如:
err = System.err
err.println("ERROR again Will Robinson")
This has the possible advantage that all the functions of System.err are accessible so you don't have to wire up each one individually (e.g. err.print, err.println, etc.).
这有可能的优点,System.err的所有功能都是可访问的,因此您不必单独连接每个功能(例如err.print,err.println等)。
Hopefully there is a standard Groovy way, because idiosyncratic renaming can be confusing to people who read your code.
希望有一种标准的Groovy方式,因为特殊的重命名可能会让阅读代码的人感到困惑。
#1
20
Just off the top of my head couldn't you do a bit of self-wiring:
就在我的头顶,你不能做一些自我布线:
def printErr = System.err.&println
printErr("AHHH")
but that is a bit manual
但这有点手册
#2
19
Another quite compact alternative is this:
另一个相当紧凑的替代方案是:
System.err << "Want this to go to stderr"
Or you could add this at the top of your script
或者您可以在脚本的顶部添加它
def err = System.err
...
err << "Want this to go to stderr"
which is what I'm now doing in my groovy shell scripts
这就是我现在在我的groovy shell脚本中所做的事情
#3
16
Groovy has access to the JRE:
Groovy可以访问JRE:
System.err.println "goes to stderr"
Although there may be a more Groovy-fied way...
虽然可能有更多Groovy风格的方式......
#4
1
If you just want something shorter to type, here are two options. First, you can import java.lang.System as anything you like, specifically something shorter like "sys":
如果您只想要更短的类型,这里有两个选项。首先,您可以将java.lang.System导入为您喜欢的任何内容,特别是像“sys”这样的更短的内容:
import java.lang.System as sys
sys.err.println("ERROR Will Robinson")
Second, you can assign the System.err stream to a variable and use that variable from then on as an alias for System.err, like:
其次,您可以将System.err流分配给变量,然后使用该变量作为System.err的别名,如:
err = System.err
err.println("ERROR again Will Robinson")
This has the possible advantage that all the functions of System.err are accessible so you don't have to wire up each one individually (e.g. err.print, err.println, etc.).
这有可能的优点,System.err的所有功能都是可访问的,因此您不必单独连接每个功能(例如err.print,err.println等)。
Hopefully there is a standard Groovy way, because idiosyncratic renaming can be confusing to people who read your code.
希望有一种标准的Groovy方式,因为特殊的重命名可能会让阅读代码的人感到困惑。