Skip to content

Commit 2a34827

Browse files
authored
Merge pull request #4606 from bladehan1/feature/help
feat(command): show help message
2 parents 58f5e0b + f827359 commit 2a34827

3 files changed

Lines changed: 126 additions & 19 deletions

File tree

common/src/main/java/org/tron/common/parameter/CommonParameter.java

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -25,57 +25,65 @@ public class CommonParameter {
2525
public static CommonParameter PARAMETER = new CommonParameter();
2626
@Setter
2727
public static boolean ENERGY_LIMIT_HARD_FORK = false;
28-
@Parameter(names = {"-c", "--config"}, description = "Config File")
28+
@Parameter(names = {"-c", "--config"}, description = "Config file (default:config.conf)")
2929
public String shellConfFileName = "";
3030
@Getter
31-
@Parameter(names = {"-d", "--output-directory"}, description = "Directory")
31+
@Parameter(names = {"-d", "--output-directory"},
32+
description = "Data directory for the databases (default:output-directory)")
3233
public String outputDirectory = "output-directory";
3334
@Getter
34-
@Parameter(names = {"--log-config"})
35+
@Parameter(names = {"--log-config"}, description = "Logback config file")
3536
public String logbackPath = "";
3637
@Getter
37-
@Parameter(names = {"-h", "--help"}, help = true, description = "HELP message")
38+
@Parameter(names = {"-h", "--help"}, help = true, description = "Show help message")
3839
public boolean help = false;
3940
@Getter
4041
@Setter
41-
@Parameter(names = {"-w", "--witness"})
42+
@Parameter(names = {"-w", "--witness"}, description = "Is witness node")
4243
public boolean witness = false;
4344
@Getter
4445
@Setter
45-
@Parameter(names = {"--support-constant"})
46+
@Parameter(names = {"--support-constant"}, description = "Support constant calling for TVM. " +
47+
"(defalut: false)")
4648
public boolean supportConstant = false;
4749
@Getter
4850
@Setter
49-
@Parameter(names = {"--max-energy-limit-for-constant"})
51+
@Parameter(names = {"--max-energy-limit-for-constant"}, description = "Max energy limit for " +
52+
"constant calling. (default: 100,000,000)")
5053
public long maxEnergyLimitForConstant = 100_000_000L;
5154
@Getter
5255
@Setter
53-
@Parameter(names = {"--lru-cache-size"})
56+
@Parameter(names = {"--lru-cache-size"}, description = "Max LRU size for caching bytecode and " +
57+
"result of JUMPDEST analysis. (default: 500)")
5458
public int lruCacheSize = 500;
5559
@Getter
5660
@Setter
57-
@Parameter(names = {"--debug"})
61+
@Parameter(names = {"--debug"}, description = "Switch for TVM debug mode. In debug model, TVM " +
62+
"will not check for timeout. (default: false)")
5863
public boolean debug = false;
5964
@Getter
6065
@Setter
61-
@Parameter(names = {"--min-time-ratio"})
66+
@Parameter(names = {"--min-time-ratio"}, description = "Maximum CPU tolerance when executing " +
67+
"non-timeout transactions while synchronizing blocks. (default: 5.0)")
6268
public double minTimeRatio = 0.0;
6369
@Getter
6470
@Setter
65-
@Parameter(names = {"--max-time-ratio"})
71+
@Parameter(names = {"--max-time-ratio"}, description = "Maximum CPU tolerance when executing " +
72+
"timeout transactions while synchronizing blocks. (default: 0.0)")
6673
public double maxTimeRatio = calcMaxTimeRatio();
6774
@Getter
6875
@Setter
6976
@Parameter(names = {"--long-running-time"})
7077
public int longRunningTime = 10;
7178
@Getter
7279
@Setter
73-
@Parameter(names = {"--max-connect-number"})
80+
@Parameter(names = {"--max-connect-number"}, description = "Http server max connect number " +
81+
"(default:50)")
7482
public int maxHttpConnectNumber = 50;
7583
@Getter
7684
@Parameter(description = "--seed-nodes")
7785
public List<String> seedNodes = new ArrayList<>();
78-
@Parameter(names = {"-p", "--private-key"}, description = "private-key")
86+
@Parameter(names = {"-p", "--private-key"}, description = "Witness private key")
7987
public String privateKey = "";
8088
@Parameter(names = {"--witness-address"}, description = "witness-address")
8189
public String witnessAddress = "";
@@ -92,8 +100,8 @@ public class CommonParameter {
92100
"--storage-db-synchronous"},
93101
description = "Storage db is synchronous or not.(true or false)")
94102
public String storageDbSynchronous = "";
95-
@Parameter(names = {"--contract-parse-enable"},
96-
description = "enable contract parses in java-tron or not.(true or false)")
103+
@Parameter(names = {"--contract-parse-enable"}, description = "Switch for contract parses in " +
104+
"java-tron. (default: true)")
97105
public String contractParseEnable = "";
98106
@Parameter(names = {"--storage-index-directory"},
99107
description = "Storage index directory")
@@ -162,7 +170,8 @@ public class CommonParameter {
162170
public long nodeP2pPingInterval;
163171
@Getter
164172
@Setter
165-
@Parameter(names = {"--save-internaltx"})
173+
@Parameter(names = {"--save-internaltx"}, description = "Save internal transactions generated " +
174+
"during TVM execution, such as create, call, suicide and so on. (default: false)")
166175
public boolean saveInternalTx;
167176
@Getter
168177
@Setter
@@ -342,12 +351,12 @@ public class CommonParameter {
342351
public long blockNumForEnergyLimit;
343352
@Getter
344353
@Setter
345-
@Parameter(names = {"--es"})
354+
@Parameter(names = {"--es"}, description = "Start event subscribe server")
346355
public boolean eventSubscribe = false;
347356
@Getter
348357
@Setter
349358
public long trxExpirationTimeInMilliseconds; // (ms)
350-
@Parameter(names = {"-v", "--version"}, description = "output code version", help = true)
359+
@Parameter(names = {"-v", "--version"}, description = "Output code version", help = true)
351360
public boolean version;
352361
@Getter
353362
@Setter

framework/src/main/java/org/tron/core/config/args/Args.java

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
import static org.tron.core.config.Parameter.ChainConstant.MAX_ACTIVE_WITNESS_NUM;
88

99
import com.beust.jcommander.JCommander;
10+
import com.beust.jcommander.ParameterDescription;
11+
import com.google.common.base.Strings;
1012
import com.typesafe.config.Config;
1113
import com.typesafe.config.ConfigObject;
1214
import io.grpc.internal.GrpcUtil;
@@ -22,9 +24,13 @@
2224
import java.nio.file.Paths;
2325
import java.text.ParseException;
2426
import java.util.ArrayList;
27+
import java.util.Arrays;
2528
import java.util.Collections;
29+
import java.util.HashMap;
2630
import java.util.HashSet;
31+
import java.util.LinkedHashMap;
2732
import java.util.List;
33+
import java.util.Map;
2834
import java.util.Objects;
2935
import java.util.Optional;
3036
import java.util.Properties;
@@ -230,6 +236,95 @@ private static void printVersion() {
230236
JCommander.getConsole().println("Code : " + Version.VERSION_CODE);
231237
}
232238

239+
public static void printHelp(JCommander jCommander) {
240+
List<ParameterDescription> parameterDescriptionList = jCommander.getParameters();
241+
Map<String, ParameterDescription> stringParameterDescriptionMap = new HashMap<>();
242+
for (ParameterDescription parameterDescription : parameterDescriptionList) {
243+
String parameterName = parameterDescription.getParameterized().getName();
244+
stringParameterDescriptionMap.put(parameterName, parameterDescription);
245+
}
246+
247+
StringBuilder helpStr = new StringBuilder();
248+
helpStr.append("Name:\n\tFullNode - the java-tron command line interface\n");
249+
String programName = Strings.isNullOrEmpty(jCommander.getProgramName()) ? "FullNode.jar" :
250+
jCommander.getProgramName();
251+
helpStr.append(String.format("%nUsage: java -jar %s [options] [seedNode <seedNode> ...]%n",
252+
programName));
253+
helpStr.append(String.format("%nVERSION: %n%s-%s%n", Version.getVersion(),
254+
getCommitIdAbbrev()));
255+
256+
Map<String, String[]> groupOptionListMap = Args.getOptionGroup();
257+
for (Map.Entry<String, String[]> entry : groupOptionListMap.entrySet()) {
258+
String group = entry.getKey();
259+
helpStr.append(String.format("%n%s OPTIONS:%n", group.toUpperCase()));
260+
int optionMaxLength = Arrays.stream(entry.getValue()).mapToInt(p -> {
261+
ParameterDescription tmpParameterDescription = stringParameterDescriptionMap.get(p);
262+
if (tmpParameterDescription == null) {
263+
return 1;
264+
}
265+
return tmpParameterDescription.getNames().length();
266+
}).max().orElse(1);
267+
268+
for (String option : groupOptionListMap.get(group)) {
269+
ParameterDescription parameterDescription = stringParameterDescriptionMap.get(option);
270+
if (parameterDescription == null) {
271+
logger.warn("Miss option:{}", option);
272+
continue;
273+
}
274+
String tmpOptionDesc = String.format("%s\t\t\t%s%n",
275+
Strings.padEnd(parameterDescription.getNames(), optionMaxLength, ' '),
276+
upperFirst(parameterDescription.getDescription()));
277+
helpStr.append(tmpOptionDesc);
278+
}
279+
}
280+
JCommander.getConsole().println(helpStr.toString());
281+
}
282+
283+
public static String upperFirst(String name) {
284+
if (name.length() <= 1) {
285+
return name;
286+
}
287+
name = name.substring(0, 1).toUpperCase() + name.substring(1);
288+
return name;
289+
}
290+
291+
private static String getCommitIdAbbrev() {
292+
Properties properties = new Properties();
293+
try {
294+
InputStream in = Thread.currentThread()
295+
.getContextClassLoader().getResourceAsStream("git.properties");
296+
properties.load(in);
297+
} catch (IOException e) {
298+
logger.warn("Load resource failed,git.properties {}", e.getMessage());
299+
}
300+
return properties.getProperty("git.commit.id.abbrev");
301+
}
302+
303+
private static Map<String, String[]> getOptionGroup() {
304+
String[] tronOption = new String[] {"version", "help", "shellConfFileName", "logbackPath",
305+
"eventSubscribe"};
306+
String[] dbOption = new String[] {"outputDirectory"};
307+
String[] witnessOption = new String[] {"witness", "privateKey"};
308+
String[] vmOption = new String[] {"debug"};
309+
310+
Map<String, String[]> optionGroupMap = new LinkedHashMap<>();
311+
optionGroupMap.put("tron", tronOption);
312+
optionGroupMap.put("db", dbOption);
313+
optionGroupMap.put("witness", witnessOption);
314+
optionGroupMap.put("virtual machine", vmOption);
315+
316+
for (String[] optionList : optionGroupMap.values()) {
317+
for (String option : optionList) {
318+
try {
319+
CommonParameter.class.getField(option);
320+
} catch (NoSuchFieldException e) {
321+
logger.warn("NoSuchFieldException:{},{}", option, e.getMessage());
322+
}
323+
}
324+
}
325+
return optionGroupMap;
326+
}
327+
233328
/**
234329
* set parameters.
235330
*/

framework/src/main/java/org/tron/program/FullNode.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import ch.qos.logback.classic.LoggerContext;
44
import ch.qos.logback.classic.joran.JoranConfigurator;
5+
import com.beust.jcommander.JCommander;
56
import java.io.File;
67
import lombok.extern.slf4j.Slf4j;
78
import org.slf4j.LoggerFactory;
@@ -58,7 +59,9 @@ public static void main(String[] args) {
5859
load(parameter.getLogbackPath());
5960

6061
if (parameter.isHelp()) {
61-
logger.info("Here is the help message.");
62+
JCommander jCommander = JCommander.newBuilder().addObject(Args.PARAMETER).build();
63+
jCommander.parse(args);
64+
Args.printHelp(jCommander);
6265
return;
6366
}
6467

0 commit comments

Comments
 (0)