package xxxx
import com.beust.jcommander.JCommander;
import com.beust.jcommander.Parameter;
import java.util.ArrayList;
import java.util.List;
public class JcomEx {
@Parameter
private List<String> parameters = new ArrayList<String>();
@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;
public static void main(String[] args) {
JcomEx jct = new JcomEx();
String[] argv = { "-log", "2", "-groups", "unit" };
JCommander jc = new JCommander(jct, argv);
System.out.println(jct.groups);
System.out.println(jct.debug);
System.out.println(jct.debug);
}
}
输出:
unit
false
false
maven库:
com.beust.jcommander
在命令行上,参数key和value之间默认使用空格,也可以使用其他符号
参考:http://blog.csdn.net/navyhu/article/details/40430267
参数默认用空格分隔,但是我们也可以指定自己的分隔符,比如用“:”, “=”等,java Main -log:3
可以在参数类定义中使用@Parameter annotation指定分隔符
-
- @Parameters(separators = "=")
- public class SeparatorEqual {
- @Parameter(names = "-level")
- private Integer level = 2;
- }