in ratis-shell/src/main/java/org/apache/ratis/shell/cli/AbstractShell.java [59:110]
public int run(String... argv) {
if (argv.length == 0) {
printUsage();
return -1;
}
// Sanity check on the number of arguments
String cmd = argv[0];
Command command = mCommands.get(cmd);
if (command == null) {
// Unknown command (we didn't find the cmd in our dict)
System.err.printf("%s is an unknown command.%n", cmd);
printUsage();
return -1;
}
// Find the inner-most command and its argument line.
CommandLine cmdline;
try {
String[] currArgs = Arrays.copyOf(argv, argv.length);
while (command.hasSubCommand()) {
if (currArgs.length < 2) {
throw new IllegalArgumentException("No sub-command is specified");
}
if (!command.getSubCommands().containsKey(currArgs[1])) {
throw new IllegalArgumentException("Unknown sub-command: " + currArgs[1]);
}
command = command.getSubCommands().get(currArgs[1]);
currArgs = Arrays.copyOfRange(currArgs, 1, currArgs.length);
}
currArgs = Arrays.copyOfRange(currArgs, 1, currArgs.length);
cmdline = command.parseAndValidateArgs(currArgs);
} catch (IllegalArgumentException e) {
// It outputs a prompt message when passing wrong args to CLI
System.out.println(e.getMessage());
System.out.println("Usage: " + command.getUsage());
System.out.println(command.getDescription());
LOG.error("Invalid arguments for command {}:", command.getCommandName(), e);
return -1;
}
// Handle the command
try {
return command.run(cmdline);
} catch (Exception e) {
System.out.println(e.getMessage());
LOG.error("Error running" + Arrays.stream(argv).reduce("", (a, b) -> a + " " + b), e);
return -1;
}
}