Skip to content

Commit d95da2e

Browse files
committed
[java-matter-controller] Support to print help info for command
1 parent e7a28bf commit d95da2e

File tree

3 files changed

+54
-8
lines changed

3 files changed

+54
-8
lines changed

examples/java-matter-controller/java/src/com/matter/controller/commands/common/Command.java

+4
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ public final int getArgumentsCount() {
7676
return mArgs.size();
7777
}
7878

79+
public final boolean getArgumentIsOptional(int index) {
80+
return mArgs.get(index).isOptional();
81+
}
82+
7983
/**
8084
* @brief Get argument description if it exists
8185
* @return A pointer to an Optional where the argument description will be stored

examples/java-matter-controller/java/src/com/matter/controller/commands/common/CommandManager.java

+43-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.util.Arrays;
2323
import java.util.HashMap;
2424
import java.util.Map;
25+
import java.util.Optional;
2526

2627
public final class CommandManager {
2728
private final ArrayList<Command> mCommandMgr = new ArrayList<Command>();
@@ -96,7 +97,7 @@ public final void run(String[] args) {
9697
command.initArguments(temp.length, temp);
9798
command.run();
9899
} catch (IllegalArgumentException e) {
99-
System.out.println("Arguments init failed with exception: " + e.getMessage());
100+
showCommand(args[0], command);
100101
} catch (Exception e) {
101102
System.out.println("Run command failed with exception: " + e.getMessage());
102103
}
@@ -244,4 +245,45 @@ private void showClusterEvents(
244245
System.out.println(
245246
" +-------------------------------------------------------------------------------------+");
246247
}
248+
249+
private void showCommand(String clusterName, Command command) {
250+
System.out.println("Usage:");
251+
252+
String arguments = command.getName();
253+
String description = "";
254+
255+
int argumentsCount = command.getArgumentsCount();
256+
for (int i = 0; i < argumentsCount; i++) {
257+
String arg = "";
258+
boolean isOptional = command.getArgumentIsOptional(i);
259+
if (isOptional) {
260+
arg += "[--";
261+
}
262+
arg += command.getArgumentName(i);
263+
if (isOptional) {
264+
arg += "]";
265+
}
266+
arguments += " ";
267+
arguments += arg;
268+
269+
Optional<String> argDescription = command.getArgumentDescription(i);
270+
if (argDescription.isPresent()) {
271+
description += "\n";
272+
description += arg;
273+
description += ":\n ";
274+
description += argDescription.get();
275+
description += "\n";
276+
}
277+
}
278+
System.out.format(" java-matter-controller %s %s\n", clusterName, arguments);
279+
280+
Optional<String> helpText = command.getHelpText();
281+
if (helpText.isPresent()) {
282+
System.out.format("\n%s\n", helpText.get());
283+
}
284+
285+
if (!description.isEmpty()) {
286+
System.out.println(description);
287+
}
288+
}
247289
}

examples/java-matter-controller/java/src/com/matter/controller/commands/common/MatterCommand.java

+7-7
Original file line numberDiff line numberDiff line change
@@ -58,38 +58,38 @@ public MatterCommand(
5858
this.mChipDeviceController = controller;
5959

6060
addArgument(
61-
"--paa-trust-store-path",
61+
"paa-trust-store-path",
6262
mPaaTrustStorePath,
6363
"Path to directory holding PAA certificate information. Can be absolute or relative to the current working "
6464
+ "directory.",
6565
true);
6666
addArgument(
67-
"--cd-trust-store-path",
67+
"cd-trust-store-path",
6868
mCDTrustStorePath,
6969
"Path to directory holding CD certificate information. Can be absolute or relative to the current working "
7070
+ "directory.",
7171
true);
7272
addArgument(
73-
"--commissioner-name",
73+
"commissioner-name",
7474
mCommissionerName,
7575
"Name of fabric to use. Valid values are \"alpha\", \"beta\", \"gamma\", and integers greater than or equal to "
7676
+ "4. The default if not specified is \"alpha\".",
7777
true);
7878
addArgument(
79-
"--commissioner-nodeid",
79+
"commissioner-nodeid",
8080
0,
8181
Long.MAX_VALUE,
8282
mCommissionerNodeId,
83-
"The node id to use for chip-tool. If not provided, kTestControllerNodeId (112233, 0x1B669) will be used.",
83+
"The node id to use for java-matter-controller. If not provided, kTestControllerNodeId (112233, 0x1B669) will be used.",
8484
true);
8585
addArgument(
86-
"--use-max-sized-certs",
86+
"use-max-sized-certs",
8787
mUseMaxSizedCerts,
8888
"Maximize the size of operational certificates. If not provided or 0 (\"false\"), normally sized operational "
8989
+ "certificates are generated.",
9090
true);
9191
addArgument(
92-
"--only-allow-trusted-cd-keys",
92+
"only-allow-trusted-cd-keys",
9393
mOnlyAllowTrustedCdKeys,
9494
"Only allow trusted CD verifying keys (disallow test keys). If not provided or 0 (\"false\"), untrusted CD "
9595
+ "verifying keys are allowed. If 1 (\"true\"), test keys are disallowed.",

0 commit comments

Comments
 (0)