Skip to content

Commit

Permalink
[java-matter-controller] Support to print help info for command (#23801)
Browse files Browse the repository at this point in the history
  • Loading branch information
yufengwangca authored and pull[bot] committed Oct 5, 2023
1 parent 4488e23 commit 8277001
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ public final int getArgumentsCount() {
return mArgs.size();
}

public final boolean getArgumentIsOptional(int index) {
return mArgs.get(index).isOptional();
}

/**
* @brief Get argument description if it exists
* @return A pointer to an Optional where the argument description will be stored
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

public final class CommandManager {
private final ArrayList<Command> mCommandMgr = new ArrayList<Command>();
Expand Down Expand Up @@ -96,7 +97,7 @@ public final void run(String[] args) {
command.initArguments(temp.length, temp);
command.run();
} catch (IllegalArgumentException e) {
System.out.println("Arguments init failed with exception: " + e.getMessage());
showCommand(args[0], command);
} catch (Exception e) {
System.out.println("Run command failed with exception: " + e.getMessage());
}
Expand Down Expand Up @@ -244,4 +245,45 @@ private void showClusterEvents(
System.out.println(
" +-------------------------------------------------------------------------------------+");
}

private void showCommand(String clusterName, Command command) {
System.out.println("Usage:");

String arguments = command.getName();
String description = "";

int argumentsCount = command.getArgumentsCount();
for (int i = 0; i < argumentsCount; i++) {
String arg = "";
boolean isOptional = command.getArgumentIsOptional(i);
if (isOptional) {
arg += "[--";
}
arg += command.getArgumentName(i);
if (isOptional) {
arg += "]";
}
arguments += " ";
arguments += arg;

Optional<String> argDescription = command.getArgumentDescription(i);
if (argDescription.isPresent()) {
description += "\n";
description += arg;
description += ":\n ";
description += argDescription.get();
description += "\n";
}
}
System.out.format(" java-matter-controller %s %s\n", clusterName, arguments);

Optional<String> helpText = command.getHelpText();
if (helpText.isPresent()) {
System.out.format("\n%s\n", helpText.get());
}

if (!description.isEmpty()) {
System.out.println(description);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,38 +58,38 @@ public MatterCommand(
this.mChipDeviceController = controller;

addArgument(
"--paa-trust-store-path",
"paa-trust-store-path",
mPaaTrustStorePath,
"Path to directory holding PAA certificate information. Can be absolute or relative to the current working "
+ "directory.",
true);
addArgument(
"--cd-trust-store-path",
"cd-trust-store-path",
mCDTrustStorePath,
"Path to directory holding CD certificate information. Can be absolute or relative to the current working "
+ "directory.",
true);
addArgument(
"--commissioner-name",
"commissioner-name",
mCommissionerName,
"Name of fabric to use. Valid values are \"alpha\", \"beta\", \"gamma\", and integers greater than or equal to "
+ "4. The default if not specified is \"alpha\".",
true);
addArgument(
"--commissioner-nodeid",
"commissioner-nodeid",
0,
Long.MAX_VALUE,
mCommissionerNodeId,
"The node id to use for chip-tool. If not provided, kTestControllerNodeId (112233, 0x1B669) will be used.",
"The node id to use for java-matter-controller. If not provided, kTestControllerNodeId (112233, 0x1B669) will be used.",
true);
addArgument(
"--use-max-sized-certs",
"use-max-sized-certs",
mUseMaxSizedCerts,
"Maximize the size of operational certificates. If not provided or 0 (\"false\"), normally sized operational "
+ "certificates are generated.",
true);
addArgument(
"--only-allow-trusted-cd-keys",
"only-allow-trusted-cd-keys",
mOnlyAllowTrustedCdKeys,
"Only allow trusted CD verifying keys (disallow test keys). If not provided or 0 (\"false\"), untrusted CD "
+ "verifying keys are allowed. If 1 (\"true\"), test keys are disallowed.",
Expand Down

0 comments on commit 8277001

Please sign in to comment.