chrisdutz commented on code in PR #13194:
URL: https://github.com/apache/iotdb/pull/13194#discussion_r1728923991


##########
iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/ServerCommandLine.java:
##########
@@ -18,50 +18,97 @@
  */
 package org.apache.iotdb.commons;
 
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
+import org.apache.iotdb.commons.exception.IoTDBException;
+
+import org.apache.commons.cli.CommandLine;
+import org.apache.commons.cli.CommandLineParser;
+import org.apache.commons.cli.DefaultParser;
+import org.apache.commons.cli.HelpFormatter;
+import org.apache.commons.cli.Option;
+import org.apache.commons.cli.OptionGroup;
+import org.apache.commons.cli.Options;
+import org.apache.commons.cli.ParseException;
+
+import java.io.PrintWriter;
 
 public abstract class ServerCommandLine {
-  private static final Logger LOG = 
LoggerFactory.getLogger(ServerCommandLine.class);
 
-  /**
-   * Implementing subclasses should return a usage string to print out
-   *
-   * @return usage
-   */
-  protected abstract String getUsage();
+  private static final Option OPTION_START =
+      Option.builder("s").longOpt("start").desc("start a new node").build();
+  private static final Option OPTION_REMOVE =
+      Option.builder("r")
+          .longOpt("remove")
+          .desc(
+              "remove a node (with the given nodeId or the node started on the 
current machine, if omitted)")
+          .hasArg()
+          .type(Number.class)
+          .argName("nodeId")
+          .optionalArg(true)
+          .build();
 
-  /**
-   * run command
-   *
-   * @param args system args
-   * @return return 0 if exec success
-   */
-  protected abstract int run(String[] args) throws Exception;
+  private final String cliName;
+  private final PrintWriter output;
+  private final Options options;
 
-  protected void usage(String message) {
-    if (message != null) {
-      System.err.println(message);
-      System.err.println();
-    }
+  public ServerCommandLine(String cliName) {
+    this(cliName, new PrintWriter(System.out));
+  }
 
-    System.err.println(getUsage());
+  public ServerCommandLine(String cliName, PrintWriter output) {
+    this.cliName = cliName;
+    this.output = output;
+    OptionGroup commands = new OptionGroup();
+    commands.addOption(OPTION_START);
+    commands.addOption(OPTION_REMOVE);
+    // Require one option of the group.
+    commands.setRequired(true);
+    options = new Options();
+    options.addOptionGroup(commands);
   }
 
-  /**
-   * Parse and run the given command line.
-   *
-   * @param args system args
-   */
-  public void doMain(String[] args) {
+  public int run(String[] args) {
+    CommandLineParser parser = new DefaultParser();
     try {
-      int result = run(args);
-      if (result != 0) {
-        System.exit(result);
+      CommandLine cmd = parser.parse(options, args);
+      // When starting there is no additional argument.
+      if (cmd.hasOption(OPTION_START)) {
+        start();
       }
-    } catch (Exception e) {
-      LOG.error("Failed to execute system command", e);
-      System.exit(-1);
+      // As we only support start and remove and one has to be selected,
+      // no need to check if OPTION_REMOVE is set.
+      else {
+        Number nodeId = (Number) cmd.getParsedOptionValue(OPTION_REMOVE);
+        if (nodeId != null) {
+          remove(nodeId.longValue());
+        } else {
+          remove(null);
+        }
+      }

Review Comment:
   There is no "no option" ... that's why I even left a comment in the code.
   
   The "command" OptionGroup is mandatory, so one of both has to be selected 
and not selecting an option results in the usage being printed. This is tested 
by the ServerCommandLineTest.testNoArgs test.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to