-
Notifications
You must be signed in to change notification settings - Fork 120
Expand file tree
/
Copy pathHelp.java
More file actions
217 lines (186 loc) · 6.8 KB
/
Copy pathHelp.java
File metadata and controls
217 lines (186 loc) · 6.8 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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package act.cli.builtin;
/*-
* #%L
* ACT Framework
* %%
* Copyright (C) 2014 - 2017 ActFramework
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/
import act.cli.CliCmdInfo;
import act.cli.CliContext;
import act.cli.CliDispatcher;
import act.cli.util.CommandLineParser;
import act.cli.view.CliView;
import act.handler.CliHandler;
import act.handler.CliHandlerBase;
import act.util.PropertySpec;
import org.fusesource.jansi.Ansi;
import org.osgl.util.C;
import org.osgl.util.S;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.SortedSet;
import static org.osgl.$.T2;
public class Help extends CliHandlerBase {
public static final Help INSTANCE = new Help();
private static int maxWidth = 0;
private PropertySpec.MetaInfo metaInfo;
private Help() {
metaInfo = new PropertySpec.MetaInfo();
metaInfo.onCli("name,shortcut,help");
}
@Override
public void handle(CliContext context) {
List<String> args = context.arguments();
String command;
if (args.size() > 0) {
command = args.get(0);
if (showHelp(command, context)) {
return;
}
}
CommandLineParser parser = context.commandLine();
boolean sys = parser.getBoolean("-s", "--system");
boolean all = parser.getBoolean("-a", "--all");
boolean tableList = parser.getBoolean("-t", "--table");
String q = parser.getString("-q", "--filter");
boolean app = true;
if (all) {
sys = true;
} else if (sys) {
app = false;
}
CliDispatcher dispatcher = context.app().cliDispatcher();
SortedSet<CliCmdInfo> sysCommands = dispatcher.commandInfoList(true, false);
SortedSet<CliCmdInfo> appCommands = dispatcher.commandInfoList(false, true);
int maxLen = -1;
if (!tableList) {
if (sys && !app) {
maxLen = calMaxCmdLen(sysCommands);
} else if (app && !sys) {
maxLen = calMaxCmdLen(appCommands);
} else {
maxLen = calMaxCmdLen(C.list(sysCommands).append(appCommands));
}
}
String fmt = maxLen < 0 ? null : "%-" + (maxLen + 4) + "s - %s";
if (sys) {
list("@|bold System commands|@", sysCommands, context, q, fmt);
}
if (app) {
if (sys) {
context.println("");
}
list("@|bold Application commands|@", appCommands, context, q, fmt);
}
}
private void list(String label, Collection<CliCmdInfo> commands, CliContext context, String q, String fmt) {
List<String> lines = new ArrayList<>();
lines.add(label.toUpperCase());
q = S.string(q).trim();
boolean hasFilter = S.notEmpty(q);
List<CliCmdInfo> toBeDisplayed = new ArrayList<>();
for (CliCmdInfo info : commands) {
String cmd = info.name;
if (hasFilter && !(cmd.toLowerCase().contains(q) || cmd.matches(q))) {
continue;
}
if (null == fmt) {
toBeDisplayed.add(info);
} else {
lines.add(S.fmt(fmt, info.nameAndShortcut(), info.help));
}
}
context.println(Ansi.ansi().render(S.join("\n", lines)).toString());
if (null == fmt) {
// display table list
context.println(CliView.TABLE.render(toBeDisplayed, metaInfo, context));
}
}
private int calMaxCmdLen(Collection<CliCmdInfo> commands) {
int max = 0;
for (CliCmdInfo info : commands) {
max = Math.max(max, info.nameAndShortcutLen());
}
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 = new ArrayList<>();
List<String> names = dispatcher.names(handler);
T2<String, String> commandLine = handler.commandLine();
lines.add("@|bold 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("@|bold 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("@|bold Aliases|@: " + S.join(", ", C.list(names).tail()));
}
List<String> shortCuts = dispatcher.shortCuts(handler);
if (shortCuts.size() > 0) {
lines.add("");
lines.add("@|bold Shortcuts|@: " + S.join(", ", shortCuts));
}
context.println(Ansi.ansi().render(S.join("\n", lines)).toString());
return true;
}
@Override
public T2<String, String> commandLine() {
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 = new ArrayList<>();
retList.add(T2("-s --system", "list system commands"));
retList.add(T2("-a --app", "list application commands"));
retList.add(T2("-t --table", "show command list in table"));
return retList;
}
private Object readResolve() {
return INSTANCE;
}
public static void updateMaxWidth(int width) {
maxWidth = Math.max(maxWidth, width);
}
}