使用来自 Groovy 的 jCommander 解析命令行(示例代码失败)
Parsing command line with jCommander from Groovy (sample code fails)
我正在尝试使用 jCommander 来解析 Groovy 中的命令行参数。
(MacBook (El Capitan), Groovy 2.4.11, jCommander-1.49 )
jCommander.org 上提供的示例(参见 http://jcommander.org/#_groovy)为:
import com.beust.jcommander.Parameter;
public class Args {
@Parameter
private List<String> parameters = new ArrayList<>();
@Parameter(names = { "-log", "-verbose" }, description = "Level of verbosity")
private Integer verbose = 1;
@Parameter(names = "-groups", description = "Comma-separated list of group names to be run")
private String groups;
@Parameter(names = "-debug", description = "Debug mode")
private boolean debug = false;
}
但是当我尝试 运行 该代码时 (groovy jCommanderSample.groovy) 我得到:
lexu@mbp:~/.../Groovy/CliParameters $ groovy jCommanderSample.groovy
Caught: groovy.lang.MissingMethodException: No signature of method: static com.beust.jcommander.JCommander.newBuilder() is applicable for argument types: () values: []
groovy.lang.MissingMethodException: No signature of method: static com.beust.jcommander.JCommander.newBuilder() is applicable for argument types: () values: []
at jCommanderSample$_run_closure1.doCall(jCommanderSample.groovy:9)
at jCommanderSample.run(jCommanderSample.groovy:8)
我错过了什么?
您可能注意到 groovy
中的列表定义有所不同。
在java中,使用了{ ..}
。在 groovy 中如下:
def list = [1, 2]
更改自:
@Parameter(names = { "-log", "-verbose" }, description = "Level of verbosity")
收件人:
@Parameter(names = ["-log", "-verbose" ] , description = "Level of verbosity")
这是我复制到我的代码片段集合中的解决方案:
示例 Groovy jCommander 脚本
#!/usr/bin/env groovy
import com.beust.jcommander.*
class Simple {
static class Args {
@Parameter
private List<String> aParamList = [];
@Parameter(names = [ "--quiet", "-q" ], description = "quiet mode, no output to STDOUT")
private Boolean aQuiet = false;
@Parameter(names = [ "--logging", "-l" ], description = "logging verbosity (0-4)")
private Integer aLogLevel = 1;
@Parameter(names = [ "--group", "-g" ], description = "group name, default is 'none'")
private String aGroupName = 'none';
}
public static main(String[] pArgs) {
Boolean vQuiet ;
Integer vLogLevel ;
String vGroupName ;
List<String> vParamList;
new Args().with {
try {
new JCommander(it, pArgs)
vQuiet = it.aQuiet;
vLogLevel = it.aLogLevel;
vGroupName = it.aGroupName;
vParamList = it.aParamList;
}
catch(com.beust.jcommander.ParameterException e) {
println "Fatal Error parsing arguments: $e.message"
System.exit(-1)
}
}
println "unbound parameters: " + vParamList;
println " LogLevel: $vLogLevel";
println " Silent Mode: $vQuiet";
println " GroupName: $vGroupName";
}
}
我正在尝试使用 jCommander 来解析 Groovy 中的命令行参数。
(MacBook (El Capitan), Groovy 2.4.11, jCommander-1.49 )
jCommander.org 上提供的示例(参见 http://jcommander.org/#_groovy)为:
import com.beust.jcommander.Parameter;
public class Args {
@Parameter
private List<String> parameters = new ArrayList<>();
@Parameter(names = { "-log", "-verbose" }, description = "Level of verbosity")
private Integer verbose = 1;
@Parameter(names = "-groups", description = "Comma-separated list of group names to be run")
private String groups;
@Parameter(names = "-debug", description = "Debug mode")
private boolean debug = false;
}
但是当我尝试 运行 该代码时 (groovy jCommanderSample.groovy) 我得到:
lexu@mbp:~/.../Groovy/CliParameters $ groovy jCommanderSample.groovy
Caught: groovy.lang.MissingMethodException: No signature of method: static com.beust.jcommander.JCommander.newBuilder() is applicable for argument types: () values: []
groovy.lang.MissingMethodException: No signature of method: static com.beust.jcommander.JCommander.newBuilder() is applicable for argument types: () values: []
at jCommanderSample$_run_closure1.doCall(jCommanderSample.groovy:9)
at jCommanderSample.run(jCommanderSample.groovy:8)
我错过了什么?
您可能注意到 groovy
中的列表定义有所不同。
在java中,使用了{ ..}
。在 groovy 中如下:
def list = [1, 2]
更改自:
@Parameter(names = { "-log", "-verbose" }, description = "Level of verbosity")
收件人:
@Parameter(names = ["-log", "-verbose" ] , description = "Level of verbosity")
这是我复制到我的代码片段集合中的解决方案:
示例 Groovy jCommander 脚本
#!/usr/bin/env groovy
import com.beust.jcommander.*
class Simple {
static class Args {
@Parameter
private List<String> aParamList = [];
@Parameter(names = [ "--quiet", "-q" ], description = "quiet mode, no output to STDOUT")
private Boolean aQuiet = false;
@Parameter(names = [ "--logging", "-l" ], description = "logging verbosity (0-4)")
private Integer aLogLevel = 1;
@Parameter(names = [ "--group", "-g" ], description = "group name, default is 'none'")
private String aGroupName = 'none';
}
public static main(String[] pArgs) {
Boolean vQuiet ;
Integer vLogLevel ;
String vGroupName ;
List<String> vParamList;
new Args().with {
try {
new JCommander(it, pArgs)
vQuiet = it.aQuiet;
vLogLevel = it.aLogLevel;
vGroupName = it.aGroupName;
vParamList = it.aParamList;
}
catch(com.beust.jcommander.ParameterException e) {
println "Fatal Error parsing arguments: $e.message"
System.exit(-1)
}
}
println "unbound parameters: " + vParamList;
println " LogLevel: $vLogLevel";
println " Silent Mode: $vQuiet";
println " GroupName: $vGroupName";
}
}