Repository navigation
Expand file tree
/
Copy pathHelp.java
More file actions
170 lines (145 loc) · 5.28 KB
/
Copy pathHelp.java
File metadata and controls
170 lines (145 loc) · 5.28 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package act.cli.builtin;
import act.app.CliContext;
import act.cli.CliDispatcher;
import act.cli.util.CommandLineParser;
import act.handler.CliHandler;
import act.handler.CliHandlerBase;
import org.osgl.util.C;
import org.osgl.util.S;
import java.util.List;
import static org.osgl.Osgl.T2;
public class Help extends CliHandlerBase {
public static final Help INSTANCE = new Help();
private static int maxWidth = 0;
private Help() {}
@Override
public void handle(CliContext context) {
List<String> args = context.arguments();
String command = null;
if (args.size() > 0) {
command = args.get(0);
if (showHelp(command, context)) {
return;
}
}
CommandLineParser parser = context.commandLine();
boolean sys = parser.getBoolean("-s", "--system");
boolean app = parser.getBoolean("-a", "--app");
if (!sys && !app) {
sys = true;
app = true;
}
CliDispatcher dispatcher = context.app().cliDispatcher();
List<String> sysCommands = dispatcher.commands(true, false);
List<String> appCommands = dispatcher.commands(false, true);
int maxLen;
if (sys && !app) {
maxLen = calMaxCmdLen(sysCommands);
} else if (app && !sys) {
maxLen = calMaxCmdLen(appCommands);
} else {
maxLen = calMaxCmdLen(C.list(sysCommands).append(appCommands));
}
String fmt = "%-" + (maxLen + 4) + "s - %s";
if (sys) {
list(command, "System commands", fmt, sysCommands, dispatcher, context);
}
if (app) {
if (sys) {
context.println("");
}
list(command, "Application commands", fmt, appCommands, dispatcher, context);
}
}
private void list(String search, String label, String fmt, List<String> commands, CliDispatcher dispatcher, CliContext context) {
List<String> lines = C.newList();
boolean noSearch = S.blank(search);
if (noSearch) {
lines.add(label.toUpperCase());
lines.add("");
}
for (String cmd : commands) {
CliHandler handler = dispatcher.handler(cmd);
T2<String, String> commandLine = handler.commandLine(cmd);
if (noSearch || commandLine._1.contains(search)) {
lines.add(S.fmt(fmt, cmd, commandLine._2));
}
}
context.println(S.join("\n", lines));
}
private int calMaxCmdLen(List<String> commands) {
int max = 0;
for (String c : commands) {
max = Math.max(max, c.length());
}
return max;
}
public boolean showHelp(String command, CliContext context) {
CliDispatcher dispatcher = context.app().cliDispatcher();
CliHandler handler = dispatcher.handler(command);
if (null == handler) {
// context.println("Unrecongized command: %s", command);
return false;
}
List<String> lines = C.newList();
List<String> names = dispatcher.names(handler);
T2<String, String> commandLine = handler.commandLine(command);
lines.add("Usage: " + names.get(0));
lines.add(commandLine._2);
String summary = handler.summary();
if (S.notBlank(summary)) {
lines.add("");
lines.add(summary);
}
List<T2<String, String>> options = handler.options();
if (!options.isEmpty()) {
lines.add("");
lines.add("Options:");
int maxLen = 0;
for (T2<String, String> t2: options) {
maxLen = Math.max(maxLen, t2._1.length());
}
String fmt = " %-" + (maxLen + 4) + "s %s";
for (T2<String, String> t2: options) {
lines.add(S.fmt(fmt, t2._1, t2._2));
}
}
if (names.size() > 1) {
lines.add("");
lines.add("Aliases: " + S.join(", ", C.list(names).tail()));
}
List<String> shortCuts = dispatcher.shortCuts(handler);
if (shortCuts.size() > 0) {
lines.add("");
lines.add("Shortcuts: " + S.join(", ", shortCuts));
}
context.println(S.join("\n", lines));
return true;
}
@Override
public T2<String, String> commandLine(String commandName) {
return T2("help [options] [command]", "show help information");
}
@Override
public String summary() {
return "display information about a command if COMMAND is specified. " +
"Otherwise the list of help topics is printed. \n Note when " +
"COMMAND is specified, OPTIONS are ignored";
}
@Override
public List<T2<String, String>> options() {
List<T2<String, String>> retList = C.newList();
retList.add(T2("-s --system", "list system commands"));
retList.add(T2("-a --app", "list application commands"));
return retList;
}
private Object readResolve() {
return INSTANCE;
}
public static void updateMaxWidth(int width) {
maxWidth = Math.max(maxWidth, width);
}
public static void main(String[] args) {
System.out.println(S.fmt("%-18s %s", "acc.create", "create an account"));
}
}