Skip to content

Commit a590a95

Browse files
committed
common-cli 示例
1 parent a7237d0 commit a590a95

File tree

15 files changed

+1189
-2
lines changed

15 files changed

+1189
-2
lines changed

codes/javalib/javalib-cli/pom.xml

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xmlns="http://maven.apache.org/POM/4.0.0"
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+
7+
<groupId>io.github.dunwu</groupId>
8+
<artifactId>javalib-cli</artifactId>
9+
<version>1.0.0</version>
10+
11+
<properties>
12+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
13+
<java.version>1.8</java.version>
14+
<maven.compiler.source>${java.version}</maven.compiler.source>
15+
<maven.compiler.target>${java.version}</maven.compiler.target>
16+
<dunwu.version>0.4.7</dunwu.version>
17+
<jna.version>5.4.0</jna.version>
18+
</properties>
19+
20+
<dependencies>
21+
<dependency>
22+
<groupId>org.apache.commons</groupId>
23+
<artifactId>commons-lang3</artifactId>
24+
</dependency>
25+
<dependency>
26+
<groupId>commons-cli</groupId>
27+
<artifactId>commons-cli</artifactId>
28+
<version>1.4</version>
29+
</dependency>
30+
<dependency>
31+
<groupId>com.github.oshi</groupId>
32+
<artifactId>oshi-core</artifactId>
33+
<version>4.1.0</version>
34+
<exclusions>
35+
<exclusion>
36+
<groupId>net.java.dev.jna</groupId>
37+
<artifactId>jna</artifactId>
38+
</exclusion>
39+
<exclusion>
40+
<groupId>net.java.dev.jna</groupId>
41+
<artifactId>jna-platform</artifactId>
42+
</exclusion>
43+
</exclusions>
44+
</dependency>
45+
<dependency>
46+
<groupId>net.java.dev.jna</groupId>
47+
<artifactId>jna</artifactId>
48+
<version>${jna.version}</version>
49+
</dependency>
50+
<dependency>
51+
<groupId>net.java.dev.jna</groupId>
52+
<artifactId>jna-platform</artifactId>
53+
<version>${jna.version}</version>
54+
<exclusions>
55+
<exclusion>
56+
<groupId>net.java.dev.jna</groupId>
57+
<artifactId>jna</artifactId>
58+
</exclusion>
59+
</exclusions>
60+
</dependency>
61+
<dependency>
62+
<groupId>junit</groupId>
63+
<artifactId>junit</artifactId>
64+
<version>4.12</version>
65+
<scope>test</scope>
66+
</dependency>
67+
</dependencies>
68+
<dependencyManagement>
69+
<dependencies>
70+
<dependency>
71+
<groupId>io.github.dunwu</groupId>
72+
<artifactId>dunwu-dependencies</artifactId>
73+
<version>${dunwu.version}</version>
74+
<type>pom</type>
75+
<scope>import</scope>
76+
</dependency>
77+
</dependencies>
78+
</dependencyManagement>
79+
</project>
Lines changed: 244 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,244 @@
1+
package io.github.dunwu.javalib;
2+
3+
import io.github.dunwu.javalib.constant.AnsiBgColor;
4+
import io.github.dunwu.javalib.constant.AnsiColor;
5+
import io.github.dunwu.javalib.constant.AnsiSgr;
6+
import io.github.dunwu.javalib.constant.Color;
7+
import org.apache.commons.lang3.StringUtils;
8+
9+
/**
10+
* 以 Ansi 方式在控制台输出(输出彩色字体、粗体、斜体、下划线等)
11+
*
12+
* @author <a href="mailto:forbreak@163.com">Zhang Peng</a>
13+
* @see <a href="https://en.wikipedia.org/wiki/ANSI_escape_code">ANSI escape code</a>
14+
* @since 2019/10/30
15+
*/
16+
public class AnsiSystem {
17+
18+
private String code;
19+
20+
private static final String ENCODE_JOIN = ";";
21+
22+
private static final String ENCODE_START = "\033[";
23+
24+
private static final String ENCODE_END = "m";
25+
26+
private static final String RESET = "\033[0;m";
27+
28+
public static final AnsiSystem RED = new AnsiSystem("\033[;31m");
29+
30+
public static final AnsiSystem GREEN = new AnsiSystem("\033[;32m");
31+
32+
public static final AnsiSystem YELLOW = new AnsiSystem("\033[;33m");
33+
34+
public static final AnsiSystem BLUE = new AnsiSystem("\033[;34m");
35+
36+
public static final AnsiSystem MAGENTA = new AnsiSystem("\033[;35m");
37+
38+
public static final AnsiSystem CYAN = new AnsiSystem("\033[;36m");
39+
40+
public static final AnsiSystem WHITE = new AnsiSystem("\033[;37m");
41+
42+
public AnsiSystem(String code) {
43+
this.code = code;
44+
}
45+
46+
public AnsiSystem(AnsiConfig config) {
47+
this.code = encode(config);
48+
}
49+
50+
public void print(String message) {
51+
System.out.print(code + message + RESET);
52+
}
53+
54+
public void println(String message) {
55+
System.out.println(code + message + RESET);
56+
}
57+
58+
private String encode(AnsiConfig config) {
59+
StringBuilder sb = new StringBuilder();
60+
sb.append(ENCODE_START);
61+
if (config.isBold()) {
62+
sb.append(ENCODE_JOIN).append(AnsiSgr.BOLD.getCode());
63+
}
64+
if (config.isItalic()) {
65+
sb.append(ENCODE_JOIN).append(AnsiSgr.ITALIC.getCode());
66+
}
67+
if (config.isUnderline()) {
68+
sb.append(ENCODE_JOIN).append(AnsiSgr.UNDERLINE.getCode());
69+
}
70+
if (config.isSlowBlink()) {
71+
sb.append(ENCODE_JOIN).append(AnsiSgr.SLOW_BLINK.getCode());
72+
} else {
73+
if (config.isRapidBlink()) {
74+
sb.append(ENCODE_JOIN).append(AnsiSgr.RAPID_BLINK.getCode());
75+
}
76+
}
77+
if (config.isReverseVideo()) {
78+
sb.append(ENCODE_JOIN).append(AnsiSgr.REVERSE_VIDEO.getCode());
79+
}
80+
if (config.isCanceal()) {
81+
sb.append(ENCODE_JOIN).append(AnsiSgr.CONCEAL.getCode());
82+
}
83+
if (config.getColor() != null) {
84+
AnsiColor color = AnsiColor.valueOf(config.getColor().name());
85+
if (StringUtils.isNotBlank(color.getCode())) {
86+
sb.append(ENCODE_JOIN).append(color.getCode());
87+
}
88+
}
89+
if (config.getBgColor() != null) {
90+
AnsiBgColor color = AnsiBgColor.valueOf(config.getBgColor().name());
91+
if (StringUtils.isNotBlank(color.getCode())) {
92+
sb.append(ENCODE_JOIN).append(color.getCode());
93+
}
94+
}
95+
sb.append(ENCODE_END);
96+
return sb.toString();
97+
}
98+
99+
public String getCode() {
100+
return code;
101+
}
102+
103+
public void setCode(String code) {
104+
this.code = code;
105+
}
106+
107+
/**
108+
* Ansi 配置
109+
*/
110+
public static class AnsiConfig {
111+
112+
private boolean bold;
113+
114+
private boolean italic;
115+
116+
private boolean underline;
117+
118+
private boolean slowBlink;
119+
120+
private boolean rapidBlink;
121+
122+
private boolean reverseVideo;
123+
124+
private boolean canceal;
125+
126+
private Color color;
127+
128+
private Color bgColor;
129+
130+
public AnsiConfig() {
131+
this.bold = false;
132+
this.italic = false;
133+
this.underline = false;
134+
this.slowBlink = false;
135+
this.rapidBlink = false;
136+
this.reverseVideo = false;
137+
this.canceal = false;
138+
this.color = Color.DEFAULT;
139+
this.bgColor = Color.DEFAULT;
140+
}
141+
142+
public AnsiConfig(boolean bold, boolean italic, boolean underline, boolean slowBlink, boolean rapidBlink,
143+
boolean reverseVideo, boolean canceal, Color color, Color bgColor) {
144+
this.bold = bold;
145+
this.italic = italic;
146+
this.underline = underline;
147+
this.slowBlink = slowBlink;
148+
this.rapidBlink = rapidBlink;
149+
this.reverseVideo = reverseVideo;
150+
this.canceal = canceal;
151+
this.color = color;
152+
this.bgColor = bgColor;
153+
}
154+
155+
public boolean isBold() {
156+
return bold;
157+
}
158+
159+
public void setBold(boolean bold) {
160+
this.bold = bold;
161+
}
162+
163+
public boolean isItalic() {
164+
return italic;
165+
}
166+
167+
public void setItalic(boolean italic) {
168+
this.italic = italic;
169+
}
170+
171+
public boolean isUnderline() {
172+
return underline;
173+
}
174+
175+
public void setUnderline(boolean underline) {
176+
this.underline = underline;
177+
}
178+
179+
public boolean isSlowBlink() {
180+
return slowBlink;
181+
}
182+
183+
public void setSlowBlink(boolean slowBlink) {
184+
this.slowBlink = slowBlink;
185+
}
186+
187+
public boolean isRapidBlink() {
188+
return rapidBlink;
189+
}
190+
191+
public void setRapidBlink(boolean rapidBlink) {
192+
this.rapidBlink = rapidBlink;
193+
}
194+
195+
public boolean isReverseVideo() {
196+
return reverseVideo;
197+
}
198+
199+
public void setReverseVideo(boolean reverseVideo) {
200+
this.reverseVideo = reverseVideo;
201+
}
202+
203+
public boolean isCanceal() {
204+
return canceal;
205+
}
206+
207+
public void setCanceal(boolean canceal) {
208+
this.canceal = canceal;
209+
}
210+
211+
public Color getColor() {
212+
return color;
213+
}
214+
215+
public void setColor(Color color) {
216+
this.color = color;
217+
}
218+
219+
public Color getBgColor() {
220+
return bgColor;
221+
}
222+
223+
public void setBgColor(Color bgColor) {
224+
this.bgColor = bgColor;
225+
}
226+
227+
@Override
228+
public String toString() {
229+
return "AnsiParam{" +
230+
"bold=" + bold +
231+
", italic=" + italic +
232+
", underline=" + underline +
233+
", slowBlink=" + slowBlink +
234+
", rapidBlink=" + rapidBlink +
235+
", reverseVideo=" + reverseVideo +
236+
", canceal=" + canceal +
237+
", color=" + color +
238+
", bgColor=" + bgColor +
239+
'}';
240+
}
241+
242+
}
243+
244+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package io.github.dunwu.javalib;
2+
3+
import org.apache.commons.cli.ParseException;
4+
5+
import java.util.Date;
6+
import java.util.Properties;
7+
import java.util.Scanner;
8+
9+
/**
10+
* @author <a href="mailto:forbreak@163.com">Zhang Peng</a>
11+
* @since 2019/10/29
12+
*/
13+
public class CliDemo {
14+
15+
public static void main(String[] args) throws ParseException {
16+
17+
while (true) {
18+
Scanner scanner = new Scanner(System.in);
19+
String param = "";
20+
if (scanner.hasNext()) {
21+
param = scanner.next();
22+
}
23+
24+
switch (param) {
25+
case "date":
26+
AnsiSystem.BLUE.println("date = " + new Date());
27+
break;
28+
case "area":
29+
AnsiSystem.BLUE.println("area = " + "China");
30+
break;
31+
case "system":
32+
Properties props = System.getProperties();
33+
System.out.println("Java的运行环境版本:" + props.getProperty("java.version"));
34+
System.out.println("默认的临时文件路径:" + props.getProperty("java.io.tmpdir"));
35+
System.out.println("操作系统的名称:" + props.getProperty("os.name"));
36+
System.out.println("操作系统的构架:" + props.getProperty("os.arch"));
37+
System.out.println("操作系统的版本:" + props.getProperty("os.version"));
38+
System.out.println("用户的账户名称:" + props.getProperty("user.name"));
39+
System.out.println("用户的主目录:" + props.getProperty("user.home"));
40+
System.out.println("用户的当前工作目录:" + props.getProperty("user.dir"));
41+
System.out.println("操作系统:" + props.getProperty("sun.desktop"));
42+
System.out.println("CPU个数:" + Runtime.getRuntime().availableProcessors());
43+
System.out.println("虚拟机内存总量:" + Runtime.getRuntime().totalMemory());
44+
System.out.println("虚拟机空闲内存量:" + Runtime.getRuntime().freeMemory());
45+
System.out.println("虚拟机使用最大内存量:" + Runtime.getRuntime().maxMemory());
46+
break;
47+
case "exit":
48+
return;
49+
default:
50+
System.err.println("invalid param");
51+
break;
52+
}
53+
}
54+
}
55+
56+
}

0 commit comments

Comments
 (0)