用Jcommander打造自己的Java的命令行工具
# 前言
有时候我们用Java开发了一个小工具,希望通过命令行(CLI)或者图形界面直接调用。命令行相较于图形界面,实现迅速,交互更接近于程序员人群,而Jcommander就是Java的这样一款工具。
# 示例
# 依赖
<dependency>
<groupId>com.beust</groupId>
<artifactId>jcommander</artifactId>
<version>1.78</version>
</dependency>
1
2
3
4
5
2
3
4
5
# 例子
package com.unclezs.jcommander;
import com.beust.jcommander.JCommander;
import com.beust.jcommander.Parameter;
import com.beust.jcommander.UnixStyleUsageFormatter;
import java.nio.file.Files;
import java.nio.file.Paths;
/**
* 用法示例
*
* @author blog.unclezs.com
* @date 2020/12/1 12:15 上午
*/
public class Main {
@Parameter(names = {"--path", "-p"}, description = "文件路径", order = 1)
private String path;
@Parameter(names = {"--version", "-v"}, description = "版本", order = 2, arity = 0)
private boolean version;
@Parameter(names = {"-h"}, description = "帮助", order = 3, arity = 0)
private boolean usage;
@Parameter(names = "--help", help = true)
private boolean help;
public static void main(String[] args) {
Main pathUtil = new Main();
JCommander jCommander = JCommander.newBuilder()
.programName("pathUtil")
.addObject(pathUtil)
.build();
jCommander.setUsageFormatter(new UnixStyleUsageFormatter(jCommander));
jCommander.parse(args);
if (pathUtil.version) {
System.out.println("pathUtil version 6.6.6");
return;
}
if (pathUtil.path != null) {
System.out.println("path exist? " + Files.exists(Paths.get(pathUtil.path)));
return;
}
if (pathUtil.usage) {
jCommander.usage();
return;
}
if (pathUtil.help) {
System.out.println("help invoke");
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
运行后的效果图
# 了解更多
在 GitHub 编辑此页 (opens new window)
上次更新: 2024/02/25, 12:11:11