-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathNSCmdlineServices.java
More file actions
109 lines (94 loc) · 2.31 KB
/
Copy pathNSCmdlineServices.java
File metadata and controls
109 lines (94 loc) · 2.31 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package com.nullspace.cmdline;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map.Entry;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
/**
* 命令行命令的缓存,收到命令,在此处理
* @author kay.yang
*
*/
public class NSCmdlineServices
{
private static Logger mLogger = LogManager.getLogger(NSCmdlineServices.class);
private HashMap<String, Class<? extends NSCmdlineCommand>> mCommands = new HashMap<>();
private static NSCmdlineServices instance = new NSCmdlineServices();
private NSCmdlineServices()
{
initCommands();
}
public static NSCmdlineServices instance()
{
return instance;
}
private void initCommands()
{
mCommands.put(NSCmdlineType.TOTLE, NSTotalCommandLine.class);
}
public void print() throws InstantiationException, IllegalAccessException
{
for (Entry<String, Class<? extends NSCmdlineCommand>> entry : mCommands.entrySet())
{
String str = entry.getKey();
Class<? extends NSCmdlineCommand> cmdClazz = entry.getValue();
NSCmdlineCommand cmd = cmdClazz.newInstance();
System.out.println("命令行 输入 参数 :" + str + " -- " + cmd.description());
}
}
public void startCmdline()
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String cmd;
while (true)
{
try
{
System.out.print("> ");
cmd = br.readLine();
cmd = cmd == null ? cmd : cmd.trim();
if (cmd == null || cmd.equals(""))
{
continue;
}
String[] temp = cmd.split("-");
String type = temp[0].trim();
type = type.toUpperCase();
if (type.isEmpty() || type.contains(" "))
{
mLogger.info("命令类型只支持单字命令");
continue;
}
executeCmd(type, temp);
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
private void executeCmd(String type, String[] temp)
{
try
{
Class<? extends NSCmdlineCommand> clazz = mCommands.get(type);
if (clazz == null)
{
mLogger.info("命令: " + type + " 不存在");
return;
}
NSCmdlineCommand command = clazz.newInstance();
for (int i = 1; i < temp.length; i++)
{
command.addParameter(temp[i].trim());
}
command.execute();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}