Skip to content

Commit 2fa4fa8

Browse files
committed
feat: - [使用 JCommander 解析命令行参数](https://www.wdbyte.com/tool/jcommander/)
1 parent 2f49ced commit 2fa4fa8

24 files changed

Lines changed: 760 additions & 0 deletions

tool-java-jcommander/.gitignore

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
target/
2+
!.mvn/wrapper/maven-wrapper.jar
3+
!**/src/main/**/target/
4+
!**/src/test/**/target/
5+
6+
### IntelliJ IDEA ###
7+
.idea/modules.xml
8+
.idea/jarRepositories.xml
9+
.idea/compiler.xml
10+
.idea/libraries/
11+
*.iws
12+
*.iml
13+
*.ipr
14+
15+
### Eclipse ###
16+
.apt_generated
17+
.classpath
18+
.factorypath
19+
.project
20+
.settings
21+
.springBeans
22+
.sts4-cache
23+
24+
### NetBeans ###
25+
/nbproject/private/
26+
/nbbuild/
27+
/dist/
28+
/nbdist/
29+
/.nb-gradle/
30+
build/
31+
!**/src/main/**/build/
32+
!**/src/test/**/build/
33+
34+
### VS Code ###
35+
.vscode/
36+
37+
### Mac OS ###
38+
.DS_Store

tool-java-jcommander/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
## tool-java-jcommander
2+
3+
### 相关文章
4+
- [使用 JCommander 解析命令行参数](https://www.wdbyte.com/tool/jcommander/)

tool-java-jcommander/pom.xml

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<parent>
7+
<groupId>com.wdbyte</groupId>
8+
<artifactId>parent-modules</artifactId>
9+
<version>1.0.0-SNAPSHOT</version>
10+
</parent>
11+
12+
<artifactId>tool-java-jcommander</artifactId>
13+
14+
<properties>
15+
<maven.compiler.source>1.8</maven.compiler.source>
16+
<maven.compiler.target>1.8</maven.compiler.target>
17+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
18+
</properties>
19+
<dependencies>
20+
<!-- https://mvnrepository.com/artifact/com.beust/jcommander -->
21+
<dependency>
22+
<groupId>com.beust</groupId>
23+
<artifactId>jcommander</artifactId>
24+
<version>1.82</version>
25+
</dependency>
26+
27+
</dependencies>
28+
29+
<build>
30+
<plugins>
31+
<plugin>
32+
<groupId>org.apache.maven.plugins</groupId>
33+
<artifactId>maven-assembly-plugin</artifactId>
34+
<version>3.1.0</version>
35+
<configuration>
36+
<archive>
37+
<manifest>
38+
<mainClass>com.wdbyte.jcommander.GitApp</mainClass>
39+
</manifest>
40+
</archive>
41+
<descriptorRefs>
42+
<!-- 这个jar-with-dependencies是assembly预先写好的一个,组装描述引用 -->
43+
<descriptorRef>jar-with-dependencies</descriptorRef>
44+
</descriptorRefs>
45+
<!--工程名-->
46+
<finalName>git-app</finalName>
47+
</configuration>
48+
<executions>
49+
<execution>
50+
<id>make-assembly</id>
51+
<phase>package</phase>
52+
<goals>
53+
<goal>single</goal>
54+
</goals>
55+
</execution>
56+
</executions>
57+
</plugin>
58+
</plugins>
59+
</build>
60+
61+
</project>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.wdbyte.jcommander;
2+
3+
import java.nio.file.Files;
4+
import java.nio.file.Path;
5+
import java.nio.file.Paths;
6+
7+
import com.beust.jcommander.IStringConverter;
8+
import com.beust.jcommander.ParameterException;
9+
10+
/**
11+
*
12+
* @author niulang
13+
* @date 2023/06/15
14+
*/
15+
public class FilePathConverter implements IStringConverter<Path> {
16+
17+
@Override
18+
public Path convert(String filePath) {
19+
Path path = Paths.get(filePath);
20+
if (Files.exists(path)) {
21+
return path;
22+
}
23+
throw new ParameterException(String.format("文件不存在,path:%s", filePath));
24+
}
25+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package com.wdbyte.jcommander;
2+
3+
import java.nio.file.Path;
4+
5+
import com.beust.jcommander.JCommander;
6+
import com.beust.jcommander.ParameterException;
7+
import org.apache.commons.lang3.StringUtils;
8+
9+
/**
10+
* Git APP
11+
*
12+
* @author niulang
13+
* @date 2023/06/07
14+
*/
15+
public class GitApp {
16+
private static GitCommandOptions commandOptions = new GitCommandOptions();
17+
private static GitCommandCommit commandCommit = new GitCommandCommit();
18+
private static GitCommandAdd commandAdd = new GitCommandAdd();
19+
private static JCommander commander;
20+
21+
static {
22+
commander = JCommander.newBuilder()
23+
.programName("GitApp")
24+
.addObject(commandOptions)
25+
.addCommand(commandAdd)
26+
.addCommand(commandCommit)
27+
.build();
28+
}
29+
30+
public static void main(String[] args) {
31+
if (args.length == 0) {
32+
commander.usage();
33+
return;
34+
}
35+
try {
36+
commander.parse(args);
37+
if (commandOptions.isHelp()) {
38+
commander.usage();
39+
return;
40+
}
41+
if (commandOptions.isVersion()) {
42+
System.out.println("git version 2.24.3 (Apple Git-128)");
43+
}
44+
if (commandOptions.getCloneUrl() != null) {
45+
System.out.printf("开始克隆远程仓库数据:%s%n", commandOptions.getCloneUrl());
46+
return;
47+
}
48+
String parsedCommand = commander.getParsedCommand();
49+
if (GitCommandCommit.COMMAND.equals(parsedCommand)) {
50+
System.out.printf("提交暂存的文件并注释:%s%n", commandCommit.getComment());
51+
return;
52+
}
53+
if (GitCommandAdd.COMMAND.equals(parsedCommand)) {
54+
for (Path file : commandAdd.getFiles()) {
55+
System.out.printf("暂存文件:%s%n", file);
56+
}
57+
return;
58+
}
59+
} catch (ParameterException e) {
60+
System.err.println(e.getMessage());
61+
commander.usage();
62+
}
63+
}
64+
65+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.wdbyte.jcommander;
2+
3+
import java.nio.file.Path;
4+
import java.util.List;
5+
6+
import com.beust.jcommander.Parameter;
7+
import com.beust.jcommander.Parameters;
8+
9+
/**
10+
* git add file1 file2
11+
*
12+
* @author niulang
13+
* @date 2023/06/07
14+
*/
15+
@Parameters(commandDescription = "暂存文件", commandNames = "add", separators = " ")
16+
public class GitCommandAdd {
17+
public static final String COMMAND = "add";
18+
@Parameter(description = "暂存文件列表", converter = FilePathConverter.class)
19+
private List<Path> files;
20+
21+
public List<Path> getFiles() {
22+
return files;
23+
}
24+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.wdbyte.jcommander;
2+
3+
import com.beust.jcommander.Parameter;
4+
import com.beust.jcommander.Parameters;
5+
6+
/**
7+
* git commit -m "desc"
8+
* @author niulang
9+
* @date 2023/06/07
10+
*/
11+
@Parameters(commandDescription = "提交文件", commandNames = "commit")
12+
public class GitCommandCommit {
13+
public static final String COMMAND = "commit";
14+
15+
@Parameter(names = {"-comment", "-m"},
16+
description = "请输入注释",
17+
arity = 1,
18+
required = true)
19+
private String comment;
20+
21+
public String getComment() {
22+
return comment;
23+
}
24+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.wdbyte.jcommander;
2+
3+
import java.net.URL;
4+
5+
import com.beust.jcommander.Parameter;
6+
import com.beust.jcommander.converters.URLConverter;
7+
8+
/**
9+
* @author niulang
10+
* @date 2023/06/07
11+
*/
12+
public class GitCommandOptions {
13+
14+
@Parameter(names = {"help", "-help", "-h"},
15+
description = "查看帮助信息",
16+
help = true)
17+
private boolean help;
18+
19+
@Parameter(names = {"clone"},
20+
description = "克隆远程仓库数据",
21+
arity = 1)
22+
private String cloneUrl;
23+
24+
@Parameter(names = {"version", "-version", "-v"},
25+
description = "显示当前版本号")
26+
private boolean version = false;
27+
28+
public boolean isHelp() {
29+
return help;
30+
}
31+
32+
public boolean isVersion() {
33+
return version;
34+
}
35+
36+
public String getCloneUrl() {
37+
return cloneUrl;
38+
}
39+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.wdbyte.jcommander.v1;
2+
3+
import com.beust.jcommander.JCommander;
4+
5+
/**
6+
* @author niulang
7+
* @date 2023/06/15
8+
*/
9+
public class GitApp {
10+
11+
public static void main(String[] args) {
12+
args = new String[]{"clone","http://www.wdbyte.com/test.git"};
13+
GitCommandOptions gitCommandOptions = new GitCommandOptions();
14+
JCommander commander = JCommander.newBuilder()
15+
.addObject(gitCommandOptions)
16+
.build();
17+
commander.parse(args);
18+
System.out.println("clone " + gitCommandOptions.getCloneUrl());
19+
}
20+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.wdbyte.jcommander.v1;
2+
3+
import com.beust.jcommander.Parameter;
4+
5+
/**
6+
* @author niulang
7+
* @date 2023/06/07
8+
*/
9+
public class GitCommandOptions {
10+
@Parameter(names = {"clone"},
11+
description = "克隆远程仓库数据")
12+
private String cloneUrl;
13+
14+
public String getCloneUrl() {
15+
return cloneUrl;
16+
}
17+
}

0 commit comments

Comments
 (0)