You are viewing a plain text version of this content. The canonical link for it is here.
Posted to jira@kafka.apache.org by GitBox <gi...@apache.org> on 2023/01/19 12:46:10 UTC

[GitHub] [kafka] fvaleri opened a new pull request, #13131: KAFKA-14628: Move CommandLineUtils and CommandDefaultOptions shared classes

fvaleri opened a new pull request, #13131:
URL: https://github.com/apache/kafka/pull/13131

   These classes are required by most commands, so they must be migrated first.
   


-- 
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: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [kafka] clolov commented on a diff in pull request #13131: KAFKA-14628: Move CommandLineUtils and CommandDefaultOptions shared classes

Posted by "clolov (via GitHub)" <gi...@apache.org>.
clolov commented on code in PR #13131:
URL: https://github.com/apache/kafka/pull/13131#discussion_r1083909966


##########
core/src/main/scala/kafka/tools/ConsoleConsumer.scala:
##########
@@ -352,9 +353,11 @@ object ConsoleConsumer extends Logging {
     } else if (options.has(offsetOpt))
       CommandLineUtils.printUsageAndDie(parser, "The partition is required when offset is specified.")
 
-    def invalidOffset(offset: String): Nothing =
+    def invalidOffset(offset: String): Nothing = {
       CommandLineUtils.printUsageAndDie(parser, s"The provided offset value '$offset' is incorrect. Valid values are " +
         "'earliest', 'latest', or a non-negative long.")
+      Exit.exit(1)

Review Comment:
   A similar question about using `ToolsUtils.printUsageAndDie(...)` here as well.



-- 
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: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [kafka] fvaleri commented on a diff in pull request #13131: KAFKA-14628: Move CommandLineUtils and CommandDefaultOptions shared classes

Posted by "fvaleri (via GitHub)" <gi...@apache.org>.
fvaleri commented on code in PR #13131:
URL: https://github.com/apache/kafka/pull/13131#discussion_r1083709872


##########
core/src/main/scala/kafka/utils/ToolsUtils.scala:
##########
@@ -64,4 +65,18 @@ object ToolsUtils {
         println(s"%-${maxLengthOfDisplayName}s : $specifier".format(metricName, value))
     }
   }
+
+  /**
+   * This is a simple wrapper around `CommandLineUtils.printUsageAndDie`.
+   * It is needed for tools migration (KAFKA-14525), as there is no Java equivalent for return type `Nothing`.
+   * Can be removed once [[kafka.admin.ConsumerGroupCommand]], [[kafka.tools.ConsoleConsumer]]
+   * and [[kafka.tools.ConsoleProducer]] are migrated.
+   *
+   * @param parser Command line options parser.
+   * @param message Error message.
+   */
+  def printUsageAndDie(parser: OptionParser, message: String): Nothing = {

Review Comment:
   In the original code we always call exit, as the method name implies, so I think it's fine.



##########
core/src/main/scala/kafka/admin/ConsumerGroupCommand.scala:
##########
@@ -801,15 +801,21 @@ object ConsumerGroupCommand extends Logging {
         partitionsToReset.map { topicPartition =>
           logStartOffsets.get(topicPartition) match {
             case Some(LogOffsetResult.LogOffset(offset)) => (topicPartition, new OffsetAndMetadata(offset))
-            case _ => CommandLineUtils.printUsageAndDie(opts.parser, s"Error getting starting offset of topic partition: $topicPartition")
+            case _ => {
+              CommandLineUtils.printUsageAndDie(opts.parser, s"Error getting starting offset of topic partition: $topicPartition")
+              Exit.exit(1)

Review Comment:
   Yes, there were some some replacements that I missed. Fixed.



-- 
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: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [kafka] fvaleri commented on a diff in pull request #13131: KAFKA-14628: Move CommandLineUtils and CommandDefaultOptions shared classes

Posted by GitBox <gi...@apache.org>.
fvaleri commented on code in PR #13131:
URL: https://github.com/apache/kafka/pull/13131#discussion_r1082712140


##########
server-common/src/main/java/org/apache/kafka/server/util/CommandLineUtils.java:
##########
@@ -0,0 +1,201 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.kafka.server.util;
+
+import joptsimple.OptionParser;
+import joptsimple.OptionSet;
+import joptsimple.OptionSpec;
+import org.apache.kafka.common.utils.AppInfoParser;
+import org.apache.kafka.common.utils.Exit;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Optional;
+import java.util.Properties;
+import java.util.Set;
+
+/**
+ * Helper functions for dealing with command line utilities.
+ */
+public class CommandLineUtils {
+    /**
+     * Check if there are no options or `--help` option from command line.
+     *
+     * @param commandOpts Acceptable options for a command
+     * @return true on matching the help check condition
+     */
+    public static boolean isPrintHelpNeeded(CommandDefaultOptions commandOpts) {
+        return commandOpts.args.length == 0 || commandOpts.options.has(commandOpts.helpOpt);
+    }
+
+    /**
+     * Check if there is `--version` option from command line.
+     *
+     * @param commandOpts Acceptable options for a command
+     * @return true on matching the help check condition
+     */
+    public static boolean isPrintVersionNeeded(CommandDefaultOptions commandOpts) {
+        return commandOpts.options.has(commandOpts.versionOpt);
+    }
+
+    /**
+     * Check and print help message if there is no options or `--help` option
+     * from command line, if `--version` is specified on the command line
+     * print version information and exit.
+     * NOTE: The function name is not strictly speaking correct anymore
+     * as it also checks whether the version needs to be printed, but
+     * refactoring this would have meant changing all command line tools
+     * and unnecessarily increased the blast radius of this change.
+     *
+     * @param commandOpts Acceptable options for a command
+     * @param message     Message to display on successful check
+     */
+    public static void printHelpAndExitIfNeeded(CommandDefaultOptions commandOpts, String message) {
+        if (isPrintHelpNeeded(commandOpts)) {
+            printUsageAndDie(commandOpts.parser, message);
+        }
+        if (isPrintVersionNeeded(commandOpts)) {
+            printVersionAndDie();
+        }
+    }
+
+    /**
+     * Check that all the listed options are present.
+     */
+    public static void checkRequiredArgs(OptionParser parser, OptionSet options, OptionSpec<?>... requiredList) {
+        for (OptionSpec<?> arg : requiredList) {
+            if (!options.has(arg)) {
+                printUsageAndDie(parser, String.format("Missing required argument \"%s\"", arg));
+            }
+        }
+    }
+
+    /**
+     * Check that none of the listed options are present.
+     */
+    public static void checkInvalidArgs(OptionParser parser,
+                                        OptionSet options,
+                                        OptionSpec<?> usedOption,
+                                        OptionSpec<?>... invalidOptions) {
+        if (options.has(usedOption)) {
+            for (OptionSpec<?> arg : invalidOptions) {
+                if (options.has(arg)) {
+                    printUsageAndDie(parser, String.format("Option \"%s\" can't be used with option \"%s\"", usedOption, arg));
+                }
+            }
+        }
+    }
+
+    /**
+     * Check that none of the listed options are present.
+     */
+    public static void checkInvalidArgs(OptionParser parser,
+                                        OptionSet options,
+                                        OptionSpec<?> usedOption,
+                                        Set<OptionSpec<?>> invalidOptions) {
+        OptionSpec<?>[] array = new OptionSpec<?>[invalidOptions.size()];
+        invalidOptions.toArray(array);
+        checkInvalidArgs(parser, options, usedOption, array);
+    }
+
+    /**
+     * Check that none of the listed options are present with the combination of used options.
+     */
+    public static void checkInvalidArgsSet(OptionParser parser,
+                                           OptionSet options,
+                                           Set<OptionSpec<?>> usedOptions,
+                                           Set<OptionSpec<?>> invalidOptions,
+                                           Optional<String> trailingAdditionalMessage) {
+        if (usedOptions.stream().filter(options::has).count() == usedOptions.size()) {
+            for (OptionSpec<?> arg : invalidOptions) {
+                if (options.has(arg)) {
+                    printUsageAndDie(parser, String.format("Option combination \"%s\" can't be used with option \"%s\"%s",
+                            usedOptions, arg, trailingAdditionalMessage.orElse("")));
+                }
+            }
+        }
+    }
+
+    public static void printUsageAndDie(OptionParser parser, String message) {
+        System.err.println(message);
+        try {
+            parser.printHelpOn(System.err);
+        } catch (IOException e) {
+            throw new RuntimeException(e);
+        }
+        Exit.exit(1, message);

Review Comment:
   Internally? WDYM?
   
   There are some places, where I call this method and then call `Exit.exit(1)` (the Scala version). This is because I need `Nothing` as return value and there is no Java equivalent. It's an ugly hack, but it will naturally go away with the migration of these commands.
   



-- 
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: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [kafka] clolov commented on a diff in pull request #13131: KAFKA-14628: Move CommandLineUtils and CommandDefaultOptions shared classes

Posted by "clolov (via GitHub)" <gi...@apache.org>.
clolov commented on code in PR #13131:
URL: https://github.com/apache/kafka/pull/13131#discussion_r1083920980


##########
server-common/src/main/java/org/apache/kafka/server/util/CommandLineUtils.java:
##########
@@ -0,0 +1,201 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.kafka.server.util;
+
+import joptsimple.OptionParser;
+import joptsimple.OptionSet;
+import joptsimple.OptionSpec;
+import org.apache.kafka.common.utils.AppInfoParser;
+import org.apache.kafka.common.utils.Exit;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Optional;
+import java.util.Properties;
+import java.util.Set;
+
+/**
+ * Helper functions for dealing with command line utilities.
+ */
+public class CommandLineUtils {
+    /**
+     * Check if there are no options or `--help` option from command line.
+     *
+     * @param commandOpts Acceptable options for a command
+     * @return true on matching the help check condition
+     */
+    public static boolean isPrintHelpNeeded(CommandDefaultOptions commandOpts) {
+        return commandOpts.args.length == 0 || commandOpts.options.has(commandOpts.helpOpt);
+    }
+
+    /**
+     * Check if there is `--version` option from command line.
+     *
+     * @param commandOpts Acceptable options for a command
+     * @return true on matching the help check condition
+     */
+    public static boolean isPrintVersionNeeded(CommandDefaultOptions commandOpts) {
+        return commandOpts.options.has(commandOpts.versionOpt);
+    }
+
+    /**
+     * Check and print help message if there is no options or `--help` option
+     * from command line, if `--version` is specified on the command line
+     * print version information and exit.
+     * NOTE: The function name is not strictly speaking correct anymore
+     * as it also checks whether the version needs to be printed, but
+     * refactoring this would have meant changing all command line tools
+     * and unnecessarily increased the blast radius of this change.
+     *
+     * @param commandOpts Acceptable options for a command
+     * @param message     Message to display on successful check
+     */
+    public static void printHelpAndExitIfNeeded(CommandDefaultOptions commandOpts, String message) {
+        if (isPrintHelpNeeded(commandOpts)) {
+            printUsageAndDie(commandOpts.parser, message);
+        }
+        if (isPrintVersionNeeded(commandOpts)) {
+            printVersionAndDie();
+        }
+    }
+
+    /**
+     * Check that all the listed options are present.
+     */
+    public static void checkRequiredArgs(OptionParser parser, OptionSet options, OptionSpec<?>... requiredList) {
+        for (OptionSpec<?> arg : requiredList) {
+            if (!options.has(arg)) {
+                printUsageAndDie(parser, String.format("Missing required argument \"%s\"", arg));
+            }
+        }
+    }
+
+    /**
+     * Check that none of the listed options are present.
+     */
+    public static void checkInvalidArgs(OptionParser parser,
+                                        OptionSet options,
+                                        OptionSpec<?> usedOption,
+                                        OptionSpec<?>... invalidOptions) {
+        if (options.has(usedOption)) {
+            for (OptionSpec<?> arg : invalidOptions) {
+                if (options.has(arg)) {
+                    printUsageAndDie(parser, String.format("Option \"%s\" can't be used with option \"%s\"", usedOption, arg));
+                }
+            }
+        }
+    }
+
+    /**
+     * Check that none of the listed options are present.
+     */
+    public static void checkInvalidArgs(OptionParser parser,
+                                        OptionSet options,
+                                        OptionSpec<?> usedOption,
+                                        Set<OptionSpec<?>> invalidOptions) {
+        OptionSpec<?>[] array = new OptionSpec<?>[invalidOptions.size()];
+        invalidOptions.toArray(array);
+        checkInvalidArgs(parser, options, usedOption, array);
+    }
+
+    /**
+     * Check that none of the listed options are present with the combination of used options.
+     */
+    public static void checkInvalidArgsSet(OptionParser parser,
+                                           OptionSet options,
+                                           Set<OptionSpec<?>> usedOptions,
+                                           Set<OptionSpec<?>> invalidOptions,
+                                           Optional<String> trailingAdditionalMessage) {
+        if (usedOptions.stream().filter(options::has).count() == usedOptions.size()) {
+            for (OptionSpec<?> arg : invalidOptions) {
+                if (options.has(arg)) {
+                    printUsageAndDie(parser, String.format("Option combination \"%s\" can't be used with option \"%s\"%s",
+                            usedOptions, arg, trailingAdditionalMessage.orElse("")));
+                }
+            }
+        }
+    }
+
+    public static void printUsageAndDie(OptionParser parser, String message) {
+        System.err.println(message);
+        try {
+            parser.printHelpOn(System.err);
+        } catch (IOException e) {
+            throw new RuntimeException(e);
+        }
+        Exit.exit(1, message);

Review Comment:
   Actually, sorry, I still don't get it and I should have explained my question better. What I meant to ask is whether we can put all calls to `Exit.exit(1)` only in `CommandLineUtils` functions where we expect a termination of the program and not make explicit reference to it in other commands? For example, here https://github.com/apache/kafka/blob/0803659dc5b5e0aeae82b29e6ba4d07cb855bc1c/core/src/main/scala/kafka/admin/ZkSecurityMigrator.scala#L74 we have pushed Exit.exit(1) down into `CommandLineUtils`. And here https://github.com/apache/kafka/blob/0803659dc5b5e0aeae82b29e6ba4d07cb855bc1c/core/src/main/scala/kafka/admin/ZkSecurityMigrator.scala#L103-L104 we have not.



-- 
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: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [kafka] fvaleri commented on a diff in pull request #13131: KAFKA-14628: Move CommandLineUtils and CommandDefaultOptions shared classes

Posted by "fvaleri (via GitHub)" <gi...@apache.org>.
fvaleri commented on code in PR #13131:
URL: https://github.com/apache/kafka/pull/13131#discussion_r1084030517


##########
core/src/main/scala/kafka/admin/ZkSecurityMigrator.scala:
##########
@@ -100,6 +101,7 @@ object ZkSecurityMigrator extends Logging {
         false
       case _ =>
         CommandLineUtils.printUsageAndDie(opts.parser, usageMessage)
+        Exit.exit(1)

Review Comment:
   Sure. Thanks.



-- 
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: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [kafka] fvaleri commented on a diff in pull request #13131: KAFKA-14628: Move CommandLineUtils and CommandDefaultOptions shared classes

Posted by "fvaleri (via GitHub)" <gi...@apache.org>.
fvaleri commented on code in PR #13131:
URL: https://github.com/apache/kafka/pull/13131#discussion_r1083318714


##########
core/src/main/scala/kafka/admin/ConfigCommand.scala:
##########
@@ -771,8 +772,8 @@ object ConfigCommand extends Logging {
       .withRequiredArg
       .describedAs("command config property file")
       .ofType(classOf[String])
-    val alterOpt = parser.accepts("alter", "Alter the configuration for the entity.")
-    val describeOpt = parser.accepts("describe", "List configs for the given entity.")
+    val alterOpt: OptionSpec[String] = parser.accepts("alter", "Alter the configuration for the entity.").withOptionalArg()
+    val describeOpt: OptionSpec[String] = parser.accepts("describe", "List configs for the given entity.").withOptionalArg()

Review Comment:
   Correct, it is a left over from a previous attempt. Reverting.



-- 
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: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [kafka] fvaleri commented on pull request #13131: KAFKA-14628: Move CommandLineUtils and CommandDefaultOptions shared classes

Posted by "fvaleri (via GitHub)" <gi...@apache.org>.
fvaleri commented on PR #13131:
URL: https://github.com/apache/kafka/pull/13131#issuecomment-1402347644

   > we have pushed Exit.exit(1) down into CommandLineUtils.
   
   @clolov it wasn't pushed down, that's the original logic. The code you are referring to in ZkSecurityMigrator.scala:103 is outdated. Now we are using ToolsUtils.printUsageAndDie in a few places for the reason expressed in the method comment. Please, pull latest changes and let me know.
   


-- 
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: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [kafka] fvaleri commented on a diff in pull request #13131: KAFKA-14628: Move CommandLineUtils and CommandDefaultOptions shared classes

Posted by "fvaleri (via GitHub)" <gi...@apache.org>.
fvaleri commented on code in PR #13131:
URL: https://github.com/apache/kafka/pull/13131#discussion_r1085675650


##########
server-common/src/main/java/org/apache/kafka/server/util/CommandLineUtils.java:
##########
@@ -0,0 +1,201 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.kafka.server.util;
+
+import joptsimple.OptionParser;
+import joptsimple.OptionSet;
+import joptsimple.OptionSpec;
+import org.apache.kafka.common.utils.AppInfoParser;
+import org.apache.kafka.common.utils.Exit;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Optional;
+import java.util.Properties;
+import java.util.Set;
+
+/**
+ * Helper functions for dealing with command line utilities.
+ */
+public class CommandLineUtils {
+    /**
+     * Check if there are no options or `--help` option from command line.
+     *
+     * @param commandOpts Acceptable options for a command
+     * @return true on matching the help check condition
+     */
+    public static boolean isPrintHelpNeeded(CommandDefaultOptions commandOpts) {
+        return commandOpts.args.length == 0 || commandOpts.options.has(commandOpts.helpOpt);
+    }
+
+    /**
+     * Check if there is `--version` option from command line.
+     *
+     * @param commandOpts Acceptable options for a command
+     * @return true on matching the help check condition
+     */
+    public static boolean isPrintVersionNeeded(CommandDefaultOptions commandOpts) {
+        return commandOpts.options.has(commandOpts.versionOpt);
+    }
+
+    /**
+     * Check and print help message if there is no options or `--help` option
+     * from command line, if `--version` is specified on the command line
+     * print version information and exit.
+     * NOTE: The function name is not strictly speaking correct anymore

Review Comment:
   What about maybePrintHelpOrVersion? I think the exit part is kind of implicit when you ask for help or version.



-- 
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: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [kafka] clolov commented on a diff in pull request #13131: KAFKA-14628: Move CommandLineUtils and CommandDefaultOptions shared classes

Posted by "clolov (via GitHub)" <gi...@apache.org>.
clolov commented on code in PR #13131:
URL: https://github.com/apache/kafka/pull/13131#discussion_r1083920980


##########
server-common/src/main/java/org/apache/kafka/server/util/CommandLineUtils.java:
##########
@@ -0,0 +1,201 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.kafka.server.util;
+
+import joptsimple.OptionParser;
+import joptsimple.OptionSet;
+import joptsimple.OptionSpec;
+import org.apache.kafka.common.utils.AppInfoParser;
+import org.apache.kafka.common.utils.Exit;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Optional;
+import java.util.Properties;
+import java.util.Set;
+
+/**
+ * Helper functions for dealing with command line utilities.
+ */
+public class CommandLineUtils {
+    /**
+     * Check if there are no options or `--help` option from command line.
+     *
+     * @param commandOpts Acceptable options for a command
+     * @return true on matching the help check condition
+     */
+    public static boolean isPrintHelpNeeded(CommandDefaultOptions commandOpts) {
+        return commandOpts.args.length == 0 || commandOpts.options.has(commandOpts.helpOpt);
+    }
+
+    /**
+     * Check if there is `--version` option from command line.
+     *
+     * @param commandOpts Acceptable options for a command
+     * @return true on matching the help check condition
+     */
+    public static boolean isPrintVersionNeeded(CommandDefaultOptions commandOpts) {
+        return commandOpts.options.has(commandOpts.versionOpt);
+    }
+
+    /**
+     * Check and print help message if there is no options or `--help` option
+     * from command line, if `--version` is specified on the command line
+     * print version information and exit.
+     * NOTE: The function name is not strictly speaking correct anymore
+     * as it also checks whether the version needs to be printed, but
+     * refactoring this would have meant changing all command line tools
+     * and unnecessarily increased the blast radius of this change.
+     *
+     * @param commandOpts Acceptable options for a command
+     * @param message     Message to display on successful check
+     */
+    public static void printHelpAndExitIfNeeded(CommandDefaultOptions commandOpts, String message) {
+        if (isPrintHelpNeeded(commandOpts)) {
+            printUsageAndDie(commandOpts.parser, message);
+        }
+        if (isPrintVersionNeeded(commandOpts)) {
+            printVersionAndDie();
+        }
+    }
+
+    /**
+     * Check that all the listed options are present.
+     */
+    public static void checkRequiredArgs(OptionParser parser, OptionSet options, OptionSpec<?>... requiredList) {
+        for (OptionSpec<?> arg : requiredList) {
+            if (!options.has(arg)) {
+                printUsageAndDie(parser, String.format("Missing required argument \"%s\"", arg));
+            }
+        }
+    }
+
+    /**
+     * Check that none of the listed options are present.
+     */
+    public static void checkInvalidArgs(OptionParser parser,
+                                        OptionSet options,
+                                        OptionSpec<?> usedOption,
+                                        OptionSpec<?>... invalidOptions) {
+        if (options.has(usedOption)) {
+            for (OptionSpec<?> arg : invalidOptions) {
+                if (options.has(arg)) {
+                    printUsageAndDie(parser, String.format("Option \"%s\" can't be used with option \"%s\"", usedOption, arg));
+                }
+            }
+        }
+    }
+
+    /**
+     * Check that none of the listed options are present.
+     */
+    public static void checkInvalidArgs(OptionParser parser,
+                                        OptionSet options,
+                                        OptionSpec<?> usedOption,
+                                        Set<OptionSpec<?>> invalidOptions) {
+        OptionSpec<?>[] array = new OptionSpec<?>[invalidOptions.size()];
+        invalidOptions.toArray(array);
+        checkInvalidArgs(parser, options, usedOption, array);
+    }
+
+    /**
+     * Check that none of the listed options are present with the combination of used options.
+     */
+    public static void checkInvalidArgsSet(OptionParser parser,
+                                           OptionSet options,
+                                           Set<OptionSpec<?>> usedOptions,
+                                           Set<OptionSpec<?>> invalidOptions,
+                                           Optional<String> trailingAdditionalMessage) {
+        if (usedOptions.stream().filter(options::has).count() == usedOptions.size()) {
+            for (OptionSpec<?> arg : invalidOptions) {
+                if (options.has(arg)) {
+                    printUsageAndDie(parser, String.format("Option combination \"%s\" can't be used with option \"%s\"%s",
+                            usedOptions, arg, trailingAdditionalMessage.orElse("")));
+                }
+            }
+        }
+    }
+
+    public static void printUsageAndDie(OptionParser parser, String message) {
+        System.err.println(message);
+        try {
+            parser.printHelpOn(System.err);
+        } catch (IOException e) {
+            throw new RuntimeException(e);
+        }
+        Exit.exit(1, message);

Review Comment:
   Actually, sorry, I still don't get it and I should have explained my question better. What I meant to ask is whether we can put all calls to `Exit.exit(1)` only in `CommandLineUtils` functions where we expect a termination of the program and not make explicit reference to it in other commands? For example, here https://github.com/apache/kafka/blob/0803659dc5b5e0aeae82b29e6ba4d07cb855bc1c/core/src/main/scala/kafka/admin/ZkSecurityMigrator.scala#L74 we have pushed `Exit.exit(1)` down into `CommandLineUtils`. And here https://github.com/apache/kafka/blob/0803659dc5b5e0aeae82b29e6ba4d07cb855bc1c/core/src/main/scala/kafka/admin/ZkSecurityMigrator.scala#L103-L104 we have not.



-- 
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: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [kafka] mimaison merged pull request #13131: KAFKA-14628: Move CommandLineUtils and CommandDefaultOptions shared classes

Posted by "mimaison (via GitHub)" <gi...@apache.org>.
mimaison merged PR #13131:
URL: https://github.com/apache/kafka/pull/13131


-- 
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: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [kafka] vamossagar12 commented on a diff in pull request #13131: KAFKA-14628: Move CommandLineUtils and CommandDefaultOptions shared classes

Posted by "vamossagar12 (via GitHub)" <gi...@apache.org>.
vamossagar12 commented on code in PR #13131:
URL: https://github.com/apache/kafka/pull/13131#discussion_r1083413458


##########
core/src/main/scala/kafka/utils/ToolsUtils.scala:
##########
@@ -64,4 +65,18 @@ object ToolsUtils {
         println(s"%-${maxLengthOfDisplayName}s : $specifier".format(metricName, value))
     }
   }
+
+  /**
+   * This is a simple wrapper around `CommandLineUtils.printUsageAndDie`.
+   * It is needed for tools migration (KAFKA-14525), as there is no Java equivalent for return type `Nothing`.
+   * Can be removed once [[kafka.admin.ConsumerGroupCommand]], [[kafka.tools.ConsoleConsumer]]
+   * and [[kafka.tools.ConsoleProducer]] are migrated.
+   *
+   * @param parser Command line options parser.
+   * @param message Error message.
+   */
+  def printUsageAndDie(parser: OptionParser, message: String): Nothing = {

Review Comment:
   I am thinking if we can add another parameter to this to say if we want to do an Exit(1) from this method or not. I see some places where Exit(1) is not being invoked from the scala layer. 



##########
core/src/main/scala/kafka/admin/ConsumerGroupCommand.scala:
##########
@@ -801,15 +801,21 @@ object ConsumerGroupCommand extends Logging {
         partitionsToReset.map { topicPartition =>
           logStartOffsets.get(topicPartition) match {
             case Some(LogOffsetResult.LogOffset(offset)) => (topicPartition, new OffsetAndMetadata(offset))
-            case _ => CommandLineUtils.printUsageAndDie(opts.parser, s"Error getting starting offset of topic partition: $topicPartition")
+            case _ => {
+              CommandLineUtils.printUsageAndDie(opts.parser, s"Error getting starting offset of topic partition: $topicPartition")
+              Exit.exit(1)

Review Comment:
   Thanks @fvaleri . Wondering if these and other similar lines can be changed to use ToolsUtils. printUsageAndDie()? 



-- 
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: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [kafka] ijuma commented on pull request #13131: KAFKA-14628: Move CommandLineUtils and CommandDefaultOptions shared classes

Posted by "ijuma (via GitHub)" <gi...@apache.org>.
ijuma commented on PR #13131:
URL: https://github.com/apache/kafka/pull/13131#issuecomment-1409209835

   Got it, so it's useful for `main` methods too - not just CLI tools. Then it's fine for it to be in `server-common`.


-- 
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: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [kafka] clolov commented on a diff in pull request #13131: KAFKA-14628: Move CommandLineUtils and CommandDefaultOptions shared classes

Posted by GitBox <gi...@apache.org>.
clolov commented on code in PR #13131:
URL: https://github.com/apache/kafka/pull/13131#discussion_r1082663006


##########
server-common/src/main/java/org/apache/kafka/server/util/CommandLineUtils.java:
##########
@@ -0,0 +1,201 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.kafka.server.util;
+
+import joptsimple.OptionParser;
+import joptsimple.OptionSet;
+import joptsimple.OptionSpec;
+import org.apache.kafka.common.utils.AppInfoParser;
+import org.apache.kafka.common.utils.Exit;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Optional;
+import java.util.Properties;
+import java.util.Set;
+
+/**
+ * Helper functions for dealing with command line utilities.
+ */
+public class CommandLineUtils {
+    /**
+     * Check if there are no options or `--help` option from command line.
+     *
+     * @param commandOpts Acceptable options for a command
+     * @return true on matching the help check condition
+     */
+    public static boolean isPrintHelpNeeded(CommandDefaultOptions commandOpts) {
+        return commandOpts.args.length == 0 || commandOpts.options.has(commandOpts.helpOpt);
+    }
+
+    /**
+     * Check if there is `--version` option from command line.
+     *
+     * @param commandOpts Acceptable options for a command
+     * @return true on matching the help check condition
+     */
+    public static boolean isPrintVersionNeeded(CommandDefaultOptions commandOpts) {
+        return commandOpts.options.has(commandOpts.versionOpt);
+    }
+
+    /**
+     * Check and print help message if there is no options or `--help` option
+     * from command line, if `--version` is specified on the command line
+     * print version information and exit.
+     * NOTE: The function name is not strictly speaking correct anymore
+     * as it also checks whether the version needs to be printed, but
+     * refactoring this would have meant changing all command line tools
+     * and unnecessarily increased the blast radius of this change.
+     *
+     * @param commandOpts Acceptable options for a command
+     * @param message     Message to display on successful check
+     */
+    public static void printHelpAndExitIfNeeded(CommandDefaultOptions commandOpts, String message) {
+        if (isPrintHelpNeeded(commandOpts)) {
+            printUsageAndDie(commandOpts.parser, message);
+        }
+        if (isPrintVersionNeeded(commandOpts)) {
+            printVersionAndDie();
+        }
+    }
+
+    /**
+     * Check that all the listed options are present.
+     */
+    public static void checkRequiredArgs(OptionParser parser, OptionSet options, OptionSpec<?>... requiredList) {
+        for (OptionSpec<?> arg : requiredList) {
+            if (!options.has(arg)) {
+                printUsageAndDie(parser, String.format("Missing required argument \"%s\"", arg));
+            }
+        }
+    }
+
+    /**
+     * Check that none of the listed options are present.
+     */
+    public static void checkInvalidArgs(OptionParser parser,
+                                        OptionSet options,
+                                        OptionSpec<?> usedOption,
+                                        OptionSpec<?>... invalidOptions) {
+        if (options.has(usedOption)) {
+            for (OptionSpec<?> arg : invalidOptions) {
+                if (options.has(arg)) {
+                    printUsageAndDie(parser, String.format("Option \"%s\" can't be used with option \"%s\"", usedOption, arg));
+                }
+            }
+        }
+    }
+
+    /**
+     * Check that none of the listed options are present.
+     */
+    public static void checkInvalidArgs(OptionParser parser,
+                                        OptionSet options,
+                                        OptionSpec<?> usedOption,
+                                        Set<OptionSpec<?>> invalidOptions) {
+        OptionSpec<?>[] array = new OptionSpec<?>[invalidOptions.size()];
+        invalidOptions.toArray(array);
+        checkInvalidArgs(parser, options, usedOption, array);
+    }
+
+    /**
+     * Check that none of the listed options are present with the combination of used options.
+     */
+    public static void checkInvalidArgsSet(OptionParser parser,
+                                           OptionSet options,
+                                           Set<OptionSpec<?>> usedOptions,
+                                           Set<OptionSpec<?>> invalidOptions,
+                                           Optional<String> trailingAdditionalMessage) {
+        if (usedOptions.stream().filter(options::has).count() == usedOptions.size()) {
+            for (OptionSpec<?> arg : invalidOptions) {
+                if (options.has(arg)) {
+                    printUsageAndDie(parser, String.format("Option combination \"%s\" can't be used with option \"%s\"%s",
+                            usedOptions, arg, trailingAdditionalMessage.orElse("")));
+                }
+            }
+        }
+    }
+
+    public static void printUsageAndDie(OptionParser parser, String message) {
+        System.err.println(message);
+        try {
+            parser.printHelpOn(System.err);
+        } catch (IOException e) {
+            throw new RuntimeException(e);
+        }
+        Exit.exit(1, message);

Review Comment:
   Do we need the `Exit.exit(1)` introduced elsewhere in this pull request if this method already uses it internally?



-- 
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: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [kafka] mimaison commented on a diff in pull request #13131: KAFKA-14628: Move CommandLineUtils and CommandDefaultOptions shared classes

Posted by "mimaison (via GitHub)" <gi...@apache.org>.
mimaison commented on code in PR #13131:
URL: https://github.com/apache/kafka/pull/13131#discussion_r1085116578


##########
server-common/src/main/java/org/apache/kafka/server/util/CommandLineUtils.java:
##########
@@ -0,0 +1,201 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.kafka.server.util;
+
+import joptsimple.OptionParser;
+import joptsimple.OptionSet;
+import joptsimple.OptionSpec;
+import org.apache.kafka.common.utils.AppInfoParser;
+import org.apache.kafka.common.utils.Exit;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Optional;
+import java.util.Properties;
+import java.util.Set;
+
+/**
+ * Helper functions for dealing with command line utilities.
+ */
+public class CommandLineUtils {
+    /**
+     * Check if there are no options or `--help` option from command line.
+     *
+     * @param commandOpts Acceptable options for a command
+     * @return true on matching the help check condition
+     */
+    public static boolean isPrintHelpNeeded(CommandDefaultOptions commandOpts) {
+        return commandOpts.args.length == 0 || commandOpts.options.has(commandOpts.helpOpt);
+    }
+
+    /**
+     * Check if there is `--version` option from command line.
+     *
+     * @param commandOpts Acceptable options for a command
+     * @return true on matching the help check condition
+     */
+    public static boolean isPrintVersionNeeded(CommandDefaultOptions commandOpts) {
+        return commandOpts.options.has(commandOpts.versionOpt);
+    }
+
+    /**
+     * Check and print help message if there is no options or `--help` option
+     * from command line, if `--version` is specified on the command line
+     * print version information and exit.
+     * NOTE: The function name is not strictly speaking correct anymore

Review Comment:
   Should we take this opportunity to update this name?



##########
server-common/src/main/java/org/apache/kafka/server/util/CommandLineUtils.java:
##########
@@ -0,0 +1,201 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.kafka.server.util;
+
+import joptsimple.OptionParser;
+import joptsimple.OptionSet;
+import joptsimple.OptionSpec;
+import org.apache.kafka.common.utils.AppInfoParser;
+import org.apache.kafka.common.utils.Exit;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Optional;
+import java.util.Properties;
+import java.util.Set;
+
+/**
+ * Helper functions for dealing with command line utilities.
+ */
+public class CommandLineUtils {
+    /**
+     * Check if there are no options or `--help` option from command line.
+     *
+     * @param commandOpts Acceptable options for a command
+     * @return true on matching the help check condition
+     */
+    public static boolean isPrintHelpNeeded(CommandDefaultOptions commandOpts) {
+        return commandOpts.args.length == 0 || commandOpts.options.has(commandOpts.helpOpt);
+    }
+
+    /**
+     * Check if there is `--version` option from command line.
+     *
+     * @param commandOpts Acceptable options for a command
+     * @return true on matching the help check condition
+     */
+    public static boolean isPrintVersionNeeded(CommandDefaultOptions commandOpts) {
+        return commandOpts.options.has(commandOpts.versionOpt);
+    }
+
+    /**
+     * Check and print help message if there is no options or `--help` option
+     * from command line, if `--version` is specified on the command line
+     * print version information and exit.
+     * NOTE: The function name is not strictly speaking correct anymore
+     * as it also checks whether the version needs to be printed, but
+     * refactoring this would have meant changing all command line tools
+     * and unnecessarily increased the blast radius of this change.
+     *
+     * @param commandOpts Acceptable options for a command
+     * @param message     Message to display on successful check
+     */
+    public static void printHelpAndExitIfNeeded(CommandDefaultOptions commandOpts, String message) {
+        if (isPrintHelpNeeded(commandOpts)) {
+            printUsageAndDie(commandOpts.parser, message);
+        }
+        if (isPrintVersionNeeded(commandOpts)) {
+            printVersionAndDie();
+        }
+    }
+
+    /**
+     * Check that all the listed options are present.
+     */
+    public static void checkRequiredArgs(OptionParser parser, OptionSet options, OptionSpec<?>... requiredList) {
+        for (OptionSpec<?> arg : requiredList) {
+            if (!options.has(arg)) {
+                printUsageAndDie(parser, String.format("Missing required argument \"%s\"", arg));
+            }
+        }
+    }
+
+    /**
+     * Check that none of the listed options are present.
+     */
+    public static void checkInvalidArgs(OptionParser parser,
+                                        OptionSet options,
+                                        OptionSpec<?> usedOption,
+                                        OptionSpec<?>... invalidOptions) {
+        if (options.has(usedOption)) {
+            for (OptionSpec<?> arg : invalidOptions) {
+                if (options.has(arg)) {
+                    printUsageAndDie(parser, String.format("Option \"%s\" can't be used with option \"%s\"", usedOption, arg));
+                }
+            }
+        }
+    }
+
+    /**
+     * Check that none of the listed options are present.
+     */
+    public static void checkInvalidArgs(OptionParser parser,
+                                        OptionSet options,
+                                        OptionSpec<?> usedOption,
+                                        Set<OptionSpec<?>> invalidOptions) {
+        OptionSpec<?>[] array = new OptionSpec<?>[invalidOptions.size()];
+        invalidOptions.toArray(array);
+        checkInvalidArgs(parser, options, usedOption, array);
+    }
+
+    /**
+     * Check that none of the listed options are present with the combination of used options.
+     */
+    public static void checkInvalidArgsSet(OptionParser parser,
+                                           OptionSet options,
+                                           Set<OptionSpec<?>> usedOptions,
+                                           Set<OptionSpec<?>> invalidOptions,
+                                           Optional<String> trailingAdditionalMessage) {
+        if (usedOptions.stream().filter(options::has).count() == usedOptions.size()) {
+            for (OptionSpec<?> arg : invalidOptions) {
+                if (options.has(arg)) {
+                    printUsageAndDie(parser, String.format("Option combination \"%s\" can't be used with option \"%s\"%s",
+                            usedOptions, arg, trailingAdditionalMessage.orElse("")));
+                }
+            }
+        }
+    }
+
+    public static void printUsageAndDie(OptionParser parser, String message) {

Review Comment:
   I wonder if `printUsageAndExit` might be a more appropriate name. Same below. WDYT?



##########
server-common/src/main/java/org/apache/kafka/server/util/CommandLineUtils.java:
##########
@@ -0,0 +1,201 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.kafka.server.util;
+
+import joptsimple.OptionParser;
+import joptsimple.OptionSet;
+import joptsimple.OptionSpec;
+import org.apache.kafka.common.utils.AppInfoParser;
+import org.apache.kafka.common.utils.Exit;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Optional;
+import java.util.Properties;
+import java.util.Set;
+
+/**
+ * Helper functions for dealing with command line utilities.
+ */
+public class CommandLineUtils {
+    /**
+     * Check if there are no options or `--help` option from command line.
+     *
+     * @param commandOpts Acceptable options for a command
+     * @return true on matching the help check condition
+     */
+    public static boolean isPrintHelpNeeded(CommandDefaultOptions commandOpts) {
+        return commandOpts.args.length == 0 || commandOpts.options.has(commandOpts.helpOpt);
+    }
+
+    /**
+     * Check if there is `--version` option from command line.
+     *
+     * @param commandOpts Acceptable options for a command
+     * @return true on matching the help check condition
+     */
+    public static boolean isPrintVersionNeeded(CommandDefaultOptions commandOpts) {
+        return commandOpts.options.has(commandOpts.versionOpt);
+    }
+
+    /**
+     * Check and print help message if there is no options or `--help` option
+     * from command line, if `--version` is specified on the command line
+     * print version information and exit.
+     * NOTE: The function name is not strictly speaking correct anymore
+     * as it also checks whether the version needs to be printed, but
+     * refactoring this would have meant changing all command line tools
+     * and unnecessarily increased the blast radius of this change.
+     *
+     * @param commandOpts Acceptable options for a command
+     * @param message     Message to display on successful check
+     */
+    public static void printHelpAndExitIfNeeded(CommandDefaultOptions commandOpts, String message) {
+        if (isPrintHelpNeeded(commandOpts)) {
+            printUsageAndDie(commandOpts.parser, message);
+        }
+        if (isPrintVersionNeeded(commandOpts)) {
+            printVersionAndDie();
+        }
+    }
+
+    /**
+     * Check that all the listed options are present.
+     */
+    public static void checkRequiredArgs(OptionParser parser, OptionSet options, OptionSpec<?>... requiredList) {
+        for (OptionSpec<?> arg : requiredList) {
+            if (!options.has(arg)) {
+                printUsageAndDie(parser, String.format("Missing required argument \"%s\"", arg));
+            }
+        }
+    }
+
+    /**
+     * Check that none of the listed options are present.
+     */
+    public static void checkInvalidArgs(OptionParser parser,
+                                        OptionSet options,
+                                        OptionSpec<?> usedOption,
+                                        OptionSpec<?>... invalidOptions) {
+        if (options.has(usedOption)) {
+            for (OptionSpec<?> arg : invalidOptions) {
+                if (options.has(arg)) {
+                    printUsageAndDie(parser, String.format("Option \"%s\" can't be used with option \"%s\"", usedOption, arg));
+                }
+            }
+        }
+    }
+
+    /**
+     * Check that none of the listed options are present.
+     */
+    public static void checkInvalidArgs(OptionParser parser,
+                                        OptionSet options,
+                                        OptionSpec<?> usedOption,
+                                        Set<OptionSpec<?>> invalidOptions) {
+        OptionSpec<?>[] array = new OptionSpec<?>[invalidOptions.size()];
+        invalidOptions.toArray(array);
+        checkInvalidArgs(parser, options, usedOption, array);
+    }
+
+    /**
+     * Check that none of the listed options are present with the combination of used options.
+     */
+    public static void checkInvalidArgsSet(OptionParser parser,
+                                           OptionSet options,
+                                           Set<OptionSpec<?>> usedOptions,
+                                           Set<OptionSpec<?>> invalidOptions,
+                                           Optional<String> trailingAdditionalMessage) {
+        if (usedOptions.stream().filter(options::has).count() == usedOptions.size()) {
+            for (OptionSpec<?> arg : invalidOptions) {
+                if (options.has(arg)) {
+                    printUsageAndDie(parser, String.format("Option combination \"%s\" can't be used with option \"%s\"%s",
+                            usedOptions, arg, trailingAdditionalMessage.orElse("")));
+                }
+            }
+        }
+    }
+
+    public static void printUsageAndDie(OptionParser parser, String message) {
+        System.err.println(message);
+        try {
+            parser.printHelpOn(System.err);
+        } catch (IOException e) {
+            throw new RuntimeException(e);
+        }
+        Exit.exit(1, message);
+    }
+
+    public static void printVersionAndDie() {
+        System.out.println(AppInfoParser.getVersion());
+        Exit.exit(0);
+    }
+
+    /**
+     * Parse key-value pairs in the form key=value.
+     * Value may contain equals sign.
+     */
+    public static Properties parseKeyValueArgs(List<String> args) {
+        return parseKeyValueArgs(args, true);
+    }
+
+    /**
+     * Parse key-value pairs in the form key=value.
+     * Value may contain equals sign.
+     */
+    public static Properties parseKeyValueArgs(List<String> args, boolean acceptMissingValue) {
+        Properties props = new Properties();
+        List<String[]> splits = new ArrayList<>();
+        args.forEach(arg -> {
+            String[] split = arg.split("=", 2);
+            if (split != null && split.length > 0) {

Review Comment:
   The `null` check should not be needed here.



-- 
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: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [kafka] clolov commented on a diff in pull request #13131: KAFKA-14628: Move CommandLineUtils and CommandDefaultOptions shared classes

Posted by "clolov (via GitHub)" <gi...@apache.org>.
clolov commented on code in PR #13131:
URL: https://github.com/apache/kafka/pull/13131#discussion_r1083906152


##########
server-common/src/main/java/org/apache/kafka/server/util/CommandLineUtils.java:
##########
@@ -0,0 +1,201 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.kafka.server.util;
+
+import joptsimple.OptionParser;
+import joptsimple.OptionSet;
+import joptsimple.OptionSpec;
+import org.apache.kafka.common.utils.AppInfoParser;
+import org.apache.kafka.common.utils.Exit;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Optional;
+import java.util.Properties;
+import java.util.Set;
+
+/**
+ * Helper functions for dealing with command line utilities.
+ */
+public class CommandLineUtils {
+    /**
+     * Check if there are no options or `--help` option from command line.
+     *
+     * @param commandOpts Acceptable options for a command
+     * @return true on matching the help check condition
+     */
+    public static boolean isPrintHelpNeeded(CommandDefaultOptions commandOpts) {
+        return commandOpts.args.length == 0 || commandOpts.options.has(commandOpts.helpOpt);
+    }
+
+    /**
+     * Check if there is `--version` option from command line.
+     *
+     * @param commandOpts Acceptable options for a command
+     * @return true on matching the help check condition
+     */
+    public static boolean isPrintVersionNeeded(CommandDefaultOptions commandOpts) {
+        return commandOpts.options.has(commandOpts.versionOpt);
+    }
+
+    /**
+     * Check and print help message if there is no options or `--help` option
+     * from command line, if `--version` is specified on the command line
+     * print version information and exit.
+     * NOTE: The function name is not strictly speaking correct anymore
+     * as it also checks whether the version needs to be printed, but
+     * refactoring this would have meant changing all command line tools
+     * and unnecessarily increased the blast radius of this change.
+     *
+     * @param commandOpts Acceptable options for a command
+     * @param message     Message to display on successful check
+     */
+    public static void printHelpAndExitIfNeeded(CommandDefaultOptions commandOpts, String message) {
+        if (isPrintHelpNeeded(commandOpts)) {
+            printUsageAndDie(commandOpts.parser, message);
+        }
+        if (isPrintVersionNeeded(commandOpts)) {
+            printVersionAndDie();
+        }
+    }
+
+    /**
+     * Check that all the listed options are present.
+     */
+    public static void checkRequiredArgs(OptionParser parser, OptionSet options, OptionSpec<?>... requiredList) {
+        for (OptionSpec<?> arg : requiredList) {
+            if (!options.has(arg)) {
+                printUsageAndDie(parser, String.format("Missing required argument \"%s\"", arg));
+            }
+        }
+    }
+
+    /**
+     * Check that none of the listed options are present.
+     */
+    public static void checkInvalidArgs(OptionParser parser,
+                                        OptionSet options,
+                                        OptionSpec<?> usedOption,
+                                        OptionSpec<?>... invalidOptions) {
+        if (options.has(usedOption)) {
+            for (OptionSpec<?> arg : invalidOptions) {
+                if (options.has(arg)) {
+                    printUsageAndDie(parser, String.format("Option \"%s\" can't be used with option \"%s\"", usedOption, arg));
+                }
+            }
+        }
+    }
+
+    /**
+     * Check that none of the listed options are present.
+     */
+    public static void checkInvalidArgs(OptionParser parser,
+                                        OptionSet options,
+                                        OptionSpec<?> usedOption,
+                                        Set<OptionSpec<?>> invalidOptions) {
+        OptionSpec<?>[] array = new OptionSpec<?>[invalidOptions.size()];
+        invalidOptions.toArray(array);
+        checkInvalidArgs(parser, options, usedOption, array);
+    }
+
+    /**
+     * Check that none of the listed options are present with the combination of used options.
+     */
+    public static void checkInvalidArgsSet(OptionParser parser,
+                                           OptionSet options,
+                                           Set<OptionSpec<?>> usedOptions,
+                                           Set<OptionSpec<?>> invalidOptions,
+                                           Optional<String> trailingAdditionalMessage) {
+        if (usedOptions.stream().filter(options::has).count() == usedOptions.size()) {
+            for (OptionSpec<?> arg : invalidOptions) {
+                if (options.has(arg)) {
+                    printUsageAndDie(parser, String.format("Option combination \"%s\" can't be used with option \"%s\"%s",
+                            usedOptions, arg, trailingAdditionalMessage.orElse("")));
+                }
+            }
+        }
+    }
+
+    public static void printUsageAndDie(OptionParser parser, String message) {
+        System.err.println(message);
+        try {
+            parser.printHelpOn(System.err);
+        } catch (IOException e) {
+            throw new RuntimeException(e);
+        }
+        Exit.exit(1, message);

Review Comment:
   Ah, got it. I wasn't aware that Scala needs a different Exit to the one already defined in this method! I agree with the subsequent suggestion.



-- 
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: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [kafka] clolov commented on a diff in pull request #13131: KAFKA-14628: Move CommandLineUtils and CommandDefaultOptions shared classes

Posted by "clolov (via GitHub)" <gi...@apache.org>.
clolov commented on code in PR #13131:
URL: https://github.com/apache/kafka/pull/13131#discussion_r1083920980


##########
server-common/src/main/java/org/apache/kafka/server/util/CommandLineUtils.java:
##########
@@ -0,0 +1,201 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.kafka.server.util;
+
+import joptsimple.OptionParser;
+import joptsimple.OptionSet;
+import joptsimple.OptionSpec;
+import org.apache.kafka.common.utils.AppInfoParser;
+import org.apache.kafka.common.utils.Exit;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Optional;
+import java.util.Properties;
+import java.util.Set;
+
+/**
+ * Helper functions for dealing with command line utilities.
+ */
+public class CommandLineUtils {
+    /**
+     * Check if there are no options or `--help` option from command line.
+     *
+     * @param commandOpts Acceptable options for a command
+     * @return true on matching the help check condition
+     */
+    public static boolean isPrintHelpNeeded(CommandDefaultOptions commandOpts) {
+        return commandOpts.args.length == 0 || commandOpts.options.has(commandOpts.helpOpt);
+    }
+
+    /**
+     * Check if there is `--version` option from command line.
+     *
+     * @param commandOpts Acceptable options for a command
+     * @return true on matching the help check condition
+     */
+    public static boolean isPrintVersionNeeded(CommandDefaultOptions commandOpts) {
+        return commandOpts.options.has(commandOpts.versionOpt);
+    }
+
+    /**
+     * Check and print help message if there is no options or `--help` option
+     * from command line, if `--version` is specified on the command line
+     * print version information and exit.
+     * NOTE: The function name is not strictly speaking correct anymore
+     * as it also checks whether the version needs to be printed, but
+     * refactoring this would have meant changing all command line tools
+     * and unnecessarily increased the blast radius of this change.
+     *
+     * @param commandOpts Acceptable options for a command
+     * @param message     Message to display on successful check
+     */
+    public static void printHelpAndExitIfNeeded(CommandDefaultOptions commandOpts, String message) {
+        if (isPrintHelpNeeded(commandOpts)) {
+            printUsageAndDie(commandOpts.parser, message);
+        }
+        if (isPrintVersionNeeded(commandOpts)) {
+            printVersionAndDie();
+        }
+    }
+
+    /**
+     * Check that all the listed options are present.
+     */
+    public static void checkRequiredArgs(OptionParser parser, OptionSet options, OptionSpec<?>... requiredList) {
+        for (OptionSpec<?> arg : requiredList) {
+            if (!options.has(arg)) {
+                printUsageAndDie(parser, String.format("Missing required argument \"%s\"", arg));
+            }
+        }
+    }
+
+    /**
+     * Check that none of the listed options are present.
+     */
+    public static void checkInvalidArgs(OptionParser parser,
+                                        OptionSet options,
+                                        OptionSpec<?> usedOption,
+                                        OptionSpec<?>... invalidOptions) {
+        if (options.has(usedOption)) {
+            for (OptionSpec<?> arg : invalidOptions) {
+                if (options.has(arg)) {
+                    printUsageAndDie(parser, String.format("Option \"%s\" can't be used with option \"%s\"", usedOption, arg));
+                }
+            }
+        }
+    }
+
+    /**
+     * Check that none of the listed options are present.
+     */
+    public static void checkInvalidArgs(OptionParser parser,
+                                        OptionSet options,
+                                        OptionSpec<?> usedOption,
+                                        Set<OptionSpec<?>> invalidOptions) {
+        OptionSpec<?>[] array = new OptionSpec<?>[invalidOptions.size()];
+        invalidOptions.toArray(array);
+        checkInvalidArgs(parser, options, usedOption, array);
+    }
+
+    /**
+     * Check that none of the listed options are present with the combination of used options.
+     */
+    public static void checkInvalidArgsSet(OptionParser parser,
+                                           OptionSet options,
+                                           Set<OptionSpec<?>> usedOptions,
+                                           Set<OptionSpec<?>> invalidOptions,
+                                           Optional<String> trailingAdditionalMessage) {
+        if (usedOptions.stream().filter(options::has).count() == usedOptions.size()) {
+            for (OptionSpec<?> arg : invalidOptions) {
+                if (options.has(arg)) {
+                    printUsageAndDie(parser, String.format("Option combination \"%s\" can't be used with option \"%s\"%s",
+                            usedOptions, arg, trailingAdditionalMessage.orElse("")));
+                }
+            }
+        }
+    }
+
+    public static void printUsageAndDie(OptionParser parser, String message) {
+        System.err.println(message);
+        try {
+            parser.printHelpOn(System.err);
+        } catch (IOException e) {
+            throw new RuntimeException(e);
+        }
+        Exit.exit(1, message);

Review Comment:
   Actually, sorry, I still don't get it and I should have explained my question better. What I meant to ask is whether we can put all calls to Exit.exit(1) only in `CommandLineUtils` functions where we expact a termination of the program and not make explicit reference to it in other commands? For example, here https://github.com/apache/kafka/blob/0803659dc5b5e0aeae82b29e6ba4d07cb855bc1c/core/src/main/scala/kafka/admin/ZkSecurityMigrator.scala#L74 we have pushed Exit.exit(1) down into `CommandLineUtils`. And here https://github.com/apache/kafka/blob/0803659dc5b5e0aeae82b29e6ba4d07cb855bc1c/core/src/main/scala/kafka/admin/ZkSecurityMigrator.scala#L103-L104 we have not.



-- 
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: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [kafka] fvaleri commented on a diff in pull request #13131: KAFKA-14628: Move CommandLineUtils and CommandDefaultOptions shared classes

Posted by "fvaleri (via GitHub)" <gi...@apache.org>.
fvaleri commented on code in PR #13131:
URL: https://github.com/apache/kafka/pull/13131#discussion_r1083319516


##########
server-common/src/main/java/org/apache/kafka/server/util/CommandLineUtils.java:
##########
@@ -0,0 +1,201 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.kafka.server.util;
+
+import joptsimple.OptionParser;
+import joptsimple.OptionSet;
+import joptsimple.OptionSpec;
+import org.apache.kafka.common.utils.AppInfoParser;
+import org.apache.kafka.common.utils.Exit;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Optional;
+import java.util.Properties;
+import java.util.Set;
+
+/**
+ * Helper functions for dealing with command line utilities.
+ */
+public class CommandLineUtils {
+    /**
+     * Check if there are no options or `--help` option from command line.
+     *
+     * @param commandOpts Acceptable options for a command
+     * @return true on matching the help check condition
+     */
+    public static boolean isPrintHelpNeeded(CommandDefaultOptions commandOpts) {
+        return commandOpts.args.length == 0 || commandOpts.options.has(commandOpts.helpOpt);
+    }
+
+    /**
+     * Check if there is `--version` option from command line.
+     *
+     * @param commandOpts Acceptable options for a command
+     * @return true on matching the help check condition
+     */
+    public static boolean isPrintVersionNeeded(CommandDefaultOptions commandOpts) {
+        return commandOpts.options.has(commandOpts.versionOpt);
+    }
+
+    /**
+     * Check and print help message if there is no options or `--help` option
+     * from command line, if `--version` is specified on the command line
+     * print version information and exit.
+     * NOTE: The function name is not strictly speaking correct anymore
+     * as it also checks whether the version needs to be printed, but
+     * refactoring this would have meant changing all command line tools
+     * and unnecessarily increased the blast radius of this change.
+     *
+     * @param commandOpts Acceptable options for a command
+     * @param message     Message to display on successful check
+     */
+    public static void printHelpAndExitIfNeeded(CommandDefaultOptions commandOpts, String message) {
+        if (isPrintHelpNeeded(commandOpts)) {
+            printUsageAndDie(commandOpts.parser, message);
+        }
+        if (isPrintVersionNeeded(commandOpts)) {
+            printVersionAndDie();
+        }
+    }
+
+    /**
+     * Check that all the listed options are present.
+     */
+    public static void checkRequiredArgs(OptionParser parser, OptionSet options, OptionSpec<?>... requiredList) {
+        for (OptionSpec<?> arg : requiredList) {
+            if (!options.has(arg)) {
+                printUsageAndDie(parser, String.format("Missing required argument \"%s\"", arg));
+            }
+        }
+    }
+
+    /**
+     * Check that none of the listed options are present.
+     */
+    public static void checkInvalidArgs(OptionParser parser,
+                                        OptionSet options,
+                                        OptionSpec<?> usedOption,
+                                        OptionSpec<?>... invalidOptions) {
+        if (options.has(usedOption)) {
+            for (OptionSpec<?> arg : invalidOptions) {
+                if (options.has(arg)) {
+                    printUsageAndDie(parser, String.format("Option \"%s\" can't be used with option \"%s\"", usedOption, arg));
+                }
+            }
+        }
+    }
+
+    /**
+     * Check that none of the listed options are present.
+     */
+    public static void checkInvalidArgs(OptionParser parser,
+                                        OptionSet options,
+                                        OptionSpec<?> usedOption,
+                                        Set<OptionSpec<?>> invalidOptions) {
+        OptionSpec<?>[] array = new OptionSpec<?>[invalidOptions.size()];
+        invalidOptions.toArray(array);
+        checkInvalidArgs(parser, options, usedOption, array);
+    }
+
+    /**
+     * Check that none of the listed options are present with the combination of used options.
+     */
+    public static void checkInvalidArgsSet(OptionParser parser,
+                                           OptionSet options,
+                                           Set<OptionSpec<?>> usedOptions,
+                                           Set<OptionSpec<?>> invalidOptions,
+                                           Optional<String> trailingAdditionalMessage) {
+        if (usedOptions.stream().filter(options::has).count() == usedOptions.size()) {
+            for (OptionSpec<?> arg : invalidOptions) {
+                if (options.has(arg)) {
+                    printUsageAndDie(parser, String.format("Option combination \"%s\" can't be used with option \"%s\"%s",
+                            usedOptions, arg, trailingAdditionalMessage.orElse("")));
+                }
+            }
+        }
+    }
+
+    public static void printUsageAndDie(OptionParser parser, String message) {
+        System.err.println(message);
+        try {
+            parser.printHelpOn(System.err);
+        } catch (IOException e) {
+            throw new RuntimeException(e);
+        }
+        Exit.exit(1, message);

Review Comment:
   Yeah, good idea. Done.



-- 
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: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [kafka] fvaleri commented on pull request #13131: KAFKA-14628: Move CommandLineUtils and CommandDefaultOptions shared classes

Posted by GitBox <gi...@apache.org>.
fvaleri commented on PR #13131:
URL: https://github.com/apache/kafka/pull/13131#issuecomment-1396922639

   @mimaison @ijuma 


-- 
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: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [kafka] fvaleri commented on pull request #13131: KAFKA-14628: Move CommandLineUtils and CommandDefaultOptions shared classes

Posted by GitBox <gi...@apache.org>.
fvaleri commented on PR #13131:
URL: https://github.com/apache/kafka/pull/13131#issuecomment-1396923697

   @clolov @vamossagar12 @tinaselenge 


-- 
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: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [kafka] fvaleri commented on pull request #13131: KAFKA-14628: Move CommandLineUtils and CommandDefaultOptions shared classes

Posted by "fvaleri (via GitHub)" <gi...@apache.org>.
fvaleri commented on PR #13131:
URL: https://github.com/apache/kafka/pull/13131#issuecomment-1399304541

   @clolov @vamossagar12 I took your suggestions. Thanks.


-- 
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: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [kafka] clolov commented on a diff in pull request #13131: KAFKA-14628: Move CommandLineUtils and CommandDefaultOptions shared classes

Posted by "clolov (via GitHub)" <gi...@apache.org>.
clolov commented on code in PR #13131:
URL: https://github.com/apache/kafka/pull/13131#discussion_r1083906152


##########
server-common/src/main/java/org/apache/kafka/server/util/CommandLineUtils.java:
##########
@@ -0,0 +1,201 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.kafka.server.util;
+
+import joptsimple.OptionParser;
+import joptsimple.OptionSet;
+import joptsimple.OptionSpec;
+import org.apache.kafka.common.utils.AppInfoParser;
+import org.apache.kafka.common.utils.Exit;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Optional;
+import java.util.Properties;
+import java.util.Set;
+
+/**
+ * Helper functions for dealing with command line utilities.
+ */
+public class CommandLineUtils {
+    /**
+     * Check if there are no options or `--help` option from command line.
+     *
+     * @param commandOpts Acceptable options for a command
+     * @return true on matching the help check condition
+     */
+    public static boolean isPrintHelpNeeded(CommandDefaultOptions commandOpts) {
+        return commandOpts.args.length == 0 || commandOpts.options.has(commandOpts.helpOpt);
+    }
+
+    /**
+     * Check if there is `--version` option from command line.
+     *
+     * @param commandOpts Acceptable options for a command
+     * @return true on matching the help check condition
+     */
+    public static boolean isPrintVersionNeeded(CommandDefaultOptions commandOpts) {
+        return commandOpts.options.has(commandOpts.versionOpt);
+    }
+
+    /**
+     * Check and print help message if there is no options or `--help` option
+     * from command line, if `--version` is specified on the command line
+     * print version information and exit.
+     * NOTE: The function name is not strictly speaking correct anymore
+     * as it also checks whether the version needs to be printed, but
+     * refactoring this would have meant changing all command line tools
+     * and unnecessarily increased the blast radius of this change.
+     *
+     * @param commandOpts Acceptable options for a command
+     * @param message     Message to display on successful check
+     */
+    public static void printHelpAndExitIfNeeded(CommandDefaultOptions commandOpts, String message) {
+        if (isPrintHelpNeeded(commandOpts)) {
+            printUsageAndDie(commandOpts.parser, message);
+        }
+        if (isPrintVersionNeeded(commandOpts)) {
+            printVersionAndDie();
+        }
+    }
+
+    /**
+     * Check that all the listed options are present.
+     */
+    public static void checkRequiredArgs(OptionParser parser, OptionSet options, OptionSpec<?>... requiredList) {
+        for (OptionSpec<?> arg : requiredList) {
+            if (!options.has(arg)) {
+                printUsageAndDie(parser, String.format("Missing required argument \"%s\"", arg));
+            }
+        }
+    }
+
+    /**
+     * Check that none of the listed options are present.
+     */
+    public static void checkInvalidArgs(OptionParser parser,
+                                        OptionSet options,
+                                        OptionSpec<?> usedOption,
+                                        OptionSpec<?>... invalidOptions) {
+        if (options.has(usedOption)) {
+            for (OptionSpec<?> arg : invalidOptions) {
+                if (options.has(arg)) {
+                    printUsageAndDie(parser, String.format("Option \"%s\" can't be used with option \"%s\"", usedOption, arg));
+                }
+            }
+        }
+    }
+
+    /**
+     * Check that none of the listed options are present.
+     */
+    public static void checkInvalidArgs(OptionParser parser,
+                                        OptionSet options,
+                                        OptionSpec<?> usedOption,
+                                        Set<OptionSpec<?>> invalidOptions) {
+        OptionSpec<?>[] array = new OptionSpec<?>[invalidOptions.size()];
+        invalidOptions.toArray(array);
+        checkInvalidArgs(parser, options, usedOption, array);
+    }
+
+    /**
+     * Check that none of the listed options are present with the combination of used options.
+     */
+    public static void checkInvalidArgsSet(OptionParser parser,
+                                           OptionSet options,
+                                           Set<OptionSpec<?>> usedOptions,
+                                           Set<OptionSpec<?>> invalidOptions,
+                                           Optional<String> trailingAdditionalMessage) {
+        if (usedOptions.stream().filter(options::has).count() == usedOptions.size()) {
+            for (OptionSpec<?> arg : invalidOptions) {
+                if (options.has(arg)) {
+                    printUsageAndDie(parser, String.format("Option combination \"%s\" can't be used with option \"%s\"%s",
+                            usedOptions, arg, trailingAdditionalMessage.orElse("")));
+                }
+            }
+        }
+    }
+
+    public static void printUsageAndDie(OptionParser parser, String message) {
+        System.err.println(message);
+        try {
+            parser.printHelpOn(System.err);
+        } catch (IOException e) {
+            throw new RuntimeException(e);
+        }
+        Exit.exit(1, message);

Review Comment:
   ~~Ah, got it. I wasn't aware that Scala needs a different Exit to the one already defined in this method! I agree with the subsequent suggestion.~~



-- 
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: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [kafka] clolov commented on a diff in pull request #13131: KAFKA-14628: Move CommandLineUtils and CommandDefaultOptions shared classes

Posted by "clolov (via GitHub)" <gi...@apache.org>.
clolov commented on code in PR #13131:
URL: https://github.com/apache/kafka/pull/13131#discussion_r1083908982


##########
core/src/main/scala/kafka/admin/ZkSecurityMigrator.scala:
##########
@@ -100,6 +101,7 @@ object ZkSecurityMigrator extends Logging {
         false
       case _ =>
         CommandLineUtils.printUsageAndDie(opts.parser, usageMessage)
+        Exit.exit(1)

Review Comment:
   Could you use the new `ToolsUtils.printUsageAndDie(...)` which you introduced?



-- 
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: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [kafka] vamossagar12 commented on pull request #13131: KAFKA-14628: Move CommandLineUtils and CommandDefaultOptions shared classes

Posted by "vamossagar12 (via GitHub)" <gi...@apache.org>.
vamossagar12 commented on PR #13131:
URL: https://github.com/apache/kafka/pull/13131#issuecomment-1399976298

   Thanks @fvaleri . LGTM.


-- 
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: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [kafka] fvaleri commented on a diff in pull request #13131: KAFKA-14628: Move CommandLineUtils and CommandDefaultOptions shared classes

Posted by "fvaleri (via GitHub)" <gi...@apache.org>.
fvaleri commented on code in PR #13131:
URL: https://github.com/apache/kafka/pull/13131#discussion_r1085675650


##########
server-common/src/main/java/org/apache/kafka/server/util/CommandLineUtils.java:
##########
@@ -0,0 +1,201 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.kafka.server.util;
+
+import joptsimple.OptionParser;
+import joptsimple.OptionSet;
+import joptsimple.OptionSpec;
+import org.apache.kafka.common.utils.AppInfoParser;
+import org.apache.kafka.common.utils.Exit;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Optional;
+import java.util.Properties;
+import java.util.Set;
+
+/**
+ * Helper functions for dealing with command line utilities.
+ */
+public class CommandLineUtils {
+    /**
+     * Check if there are no options or `--help` option from command line.
+     *
+     * @param commandOpts Acceptable options for a command
+     * @return true on matching the help check condition
+     */
+    public static boolean isPrintHelpNeeded(CommandDefaultOptions commandOpts) {
+        return commandOpts.args.length == 0 || commandOpts.options.has(commandOpts.helpOpt);
+    }
+
+    /**
+     * Check if there is `--version` option from command line.
+     *
+     * @param commandOpts Acceptable options for a command
+     * @return true on matching the help check condition
+     */
+    public static boolean isPrintVersionNeeded(CommandDefaultOptions commandOpts) {
+        return commandOpts.options.has(commandOpts.versionOpt);
+    }
+
+    /**
+     * Check and print help message if there is no options or `--help` option
+     * from command line, if `--version` is specified on the command line
+     * print version information and exit.
+     * NOTE: The function name is not strictly speaking correct anymore

Review Comment:
   What about maybePrintHelpOrVersion? 
   
   I think the exit part is kind of implicit when you ask for help or version.



-- 
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: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [kafka] vamossagar12 commented on a diff in pull request #13131: KAFKA-14628: Move CommandLineUtils and CommandDefaultOptions shared classes

Posted by "vamossagar12 (via GitHub)" <gi...@apache.org>.
vamossagar12 commented on code in PR #13131:
URL: https://github.com/apache/kafka/pull/13131#discussion_r1083271427


##########
core/src/main/scala/kafka/admin/ConfigCommand.scala:
##########
@@ -771,8 +772,8 @@ object ConfigCommand extends Logging {
       .withRequiredArg
       .describedAs("command config property file")
       .ofType(classOf[String])
-    val alterOpt = parser.accepts("alter", "Alter the configuration for the entity.")
-    val describeOpt = parser.accepts("describe", "List configs for the given entity.")
+    val alterOpt: OptionSpec[String] = parser.accepts("alter", "Alter the configuration for the entity.").withOptionalArg()
+    val describeOpt: OptionSpec[String] = parser.accepts("describe", "List configs for the given entity.").withOptionalArg()

Review Comment:
   The addition of `OptionSpec` and `withOptionalArg` is not necessary strictly right?



##########
server-common/src/main/java/org/apache/kafka/server/util/CommandLineUtils.java:
##########
@@ -0,0 +1,201 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.kafka.server.util;
+
+import joptsimple.OptionParser;
+import joptsimple.OptionSet;
+import joptsimple.OptionSpec;
+import org.apache.kafka.common.utils.AppInfoParser;
+import org.apache.kafka.common.utils.Exit;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Optional;
+import java.util.Properties;
+import java.util.Set;
+
+/**
+ * Helper functions for dealing with command line utilities.
+ */
+public class CommandLineUtils {
+    /**
+     * Check if there are no options or `--help` option from command line.
+     *
+     * @param commandOpts Acceptable options for a command
+     * @return true on matching the help check condition
+     */
+    public static boolean isPrintHelpNeeded(CommandDefaultOptions commandOpts) {
+        return commandOpts.args.length == 0 || commandOpts.options.has(commandOpts.helpOpt);
+    }
+
+    /**
+     * Check if there is `--version` option from command line.
+     *
+     * @param commandOpts Acceptable options for a command
+     * @return true on matching the help check condition
+     */
+    public static boolean isPrintVersionNeeded(CommandDefaultOptions commandOpts) {
+        return commandOpts.options.has(commandOpts.versionOpt);
+    }
+
+    /**
+     * Check and print help message if there is no options or `--help` option
+     * from command line, if `--version` is specified on the command line
+     * print version information and exit.
+     * NOTE: The function name is not strictly speaking correct anymore
+     * as it also checks whether the version needs to be printed, but
+     * refactoring this would have meant changing all command line tools
+     * and unnecessarily increased the blast radius of this change.
+     *
+     * @param commandOpts Acceptable options for a command
+     * @param message     Message to display on successful check
+     */
+    public static void printHelpAndExitIfNeeded(CommandDefaultOptions commandOpts, String message) {
+        if (isPrintHelpNeeded(commandOpts)) {
+            printUsageAndDie(commandOpts.parser, message);
+        }
+        if (isPrintVersionNeeded(commandOpts)) {
+            printVersionAndDie();
+        }
+    }
+
+    /**
+     * Check that all the listed options are present.
+     */
+    public static void checkRequiredArgs(OptionParser parser, OptionSet options, OptionSpec<?>... requiredList) {
+        for (OptionSpec<?> arg : requiredList) {
+            if (!options.has(arg)) {
+                printUsageAndDie(parser, String.format("Missing required argument \"%s\"", arg));
+            }
+        }
+    }
+
+    /**
+     * Check that none of the listed options are present.
+     */
+    public static void checkInvalidArgs(OptionParser parser,
+                                        OptionSet options,
+                                        OptionSpec<?> usedOption,
+                                        OptionSpec<?>... invalidOptions) {
+        if (options.has(usedOption)) {
+            for (OptionSpec<?> arg : invalidOptions) {
+                if (options.has(arg)) {
+                    printUsageAndDie(parser, String.format("Option \"%s\" can't be used with option \"%s\"", usedOption, arg));
+                }
+            }
+        }
+    }
+
+    /**
+     * Check that none of the listed options are present.
+     */
+    public static void checkInvalidArgs(OptionParser parser,
+                                        OptionSet options,
+                                        OptionSpec<?> usedOption,
+                                        Set<OptionSpec<?>> invalidOptions) {
+        OptionSpec<?>[] array = new OptionSpec<?>[invalidOptions.size()];
+        invalidOptions.toArray(array);
+        checkInvalidArgs(parser, options, usedOption, array);
+    }
+
+    /**
+     * Check that none of the listed options are present with the combination of used options.
+     */
+    public static void checkInvalidArgsSet(OptionParser parser,
+                                           OptionSet options,
+                                           Set<OptionSpec<?>> usedOptions,
+                                           Set<OptionSpec<?>> invalidOptions,
+                                           Optional<String> trailingAdditionalMessage) {
+        if (usedOptions.stream().filter(options::has).count() == usedOptions.size()) {
+            for (OptionSpec<?> arg : invalidOptions) {
+                if (options.has(arg)) {
+                    printUsageAndDie(parser, String.format("Option combination \"%s\" can't be used with option \"%s\"%s",
+                            usedOptions, arg, trailingAdditionalMessage.orElse("")));
+                }
+            }
+        }
+    }
+
+    public static void printUsageAndDie(OptionParser parser, String message) {
+        System.err.println(message);
+        try {
+            parser.printHelpOn(System.err);
+        } catch (IOException e) {
+            throw new RuntimeException(e);
+        }
+        Exit.exit(1, message);

Review Comment:
   I had the same question as @clolov , but I guess there's no getting around what you are saying. Either ways, would it make sense to consolidate all the duplicate method calls i.e
   
   ```
   CommandLineUtils.printUsageAndDie(opts.parser, message)
   Exit.exit(1)
   ```
   
   into a separate method and add a comment to it saying the scala Exit is temporary and not needed when migrating to Java? That should make it easier for the immediate future migrations and reduce the duplicacy. WDYT?



-- 
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: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [kafka] fvaleri commented on pull request #13131: KAFKA-14628: Move CommandLineUtils and CommandDefaultOptions shared classes

Posted by GitBox <gi...@apache.org>.
fvaleri commented on PR #13131:
URL: https://github.com/apache/kafka/pull/13131#issuecomment-1397311675

   Take a look at `DumpLogSegments` to see how `CommandDefaultOptions` is used. It is much better than building the option list at the start of the `main/execute` method.


-- 
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: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [kafka] vamossagar12 commented on a diff in pull request #13131: KAFKA-14628: Move CommandLineUtils and CommandDefaultOptions shared classes

Posted by "vamossagar12 (via GitHub)" <gi...@apache.org>.
vamossagar12 commented on code in PR #13131:
URL: https://github.com/apache/kafka/pull/13131#discussion_r1083413458


##########
core/src/main/scala/kafka/utils/ToolsUtils.scala:
##########
@@ -64,4 +65,18 @@ object ToolsUtils {
         println(s"%-${maxLengthOfDisplayName}s : $specifier".format(metricName, value))
     }
   }
+
+  /**
+   * This is a simple wrapper around `CommandLineUtils.printUsageAndDie`.
+   * It is needed for tools migration (KAFKA-14525), as there is no Java equivalent for return type `Nothing`.
+   * Can be removed once [[kafka.admin.ConsumerGroupCommand]], [[kafka.tools.ConsoleConsumer]]
+   * and [[kafka.tools.ConsoleProducer]] are migrated.
+   *
+   * @param parser Command line options parser.
+   * @param message Error message.
+   */
+  def printUsageAndDie(parser: OptionParser, message: String): Nothing = {

Review Comment:
   I am thinking if we can add another parameter to this to say if we want to do an Exit(1) from this method or not. I see some places where Exit(1) is not being invoked from the the tools classes. WDYT?



-- 
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: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [kafka] vamossagar12 commented on pull request #13131: KAFKA-14628: Move CommandLineUtils and CommandDefaultOptions shared classes

Posted by "vamossagar12 (via GitHub)" <gi...@apache.org>.
vamossagar12 commented on PR #13131:
URL: https://github.com/apache/kafka/pull/13131#issuecomment-1399432431

   > @clolov @vamossagar12 took your suggestions. Thanks.
   
   Thanks @fvaleri ! Couple of minor comments. This should be good to go after that!


-- 
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: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [kafka] ijuma commented on pull request #13131: KAFKA-14628: Move CommandLineUtils and CommandDefaultOptions shared classes

Posted by "ijuma (via GitHub)" <gi...@apache.org>.
ijuma commented on PR #13131:
URL: https://github.com/apache/kafka/pull/13131#issuecomment-1408995921

   Thanks for doing this. One thing I didn't understand is why the shared classes are in `server-common` instead of `tools`. Is this because `core` also uses the class?


-- 
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: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [kafka] fvaleri commented on pull request #13131: KAFKA-14628: Move CommandLineUtils and CommandDefaultOptions shared classes

Posted by "fvaleri (via GitHub)" <gi...@apache.org>.
fvaleri commented on PR #13131:
URL: https://github.com/apache/kafka/pull/13131#issuecomment-1409100823

   > Thanks for doing this. One thing I didn't understand is why the shared classes are in `server-common` instead of `tools`. Is this because `core` also uses the class?
   
   Good question. CommandLineUtils is mostly used by tools, but Kafka.scala has a dependency on it. In order to move these 2 shared classes to the tools module, we would need to add tools dependency to core. Is this correct?
   


-- 
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: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [kafka] fvaleri commented on a diff in pull request #13131: KAFKA-14628: Move CommandLineUtils and CommandDefaultOptions shared classes

Posted by "fvaleri (via GitHub)" <gi...@apache.org>.
fvaleri commented on code in PR #13131:
URL: https://github.com/apache/kafka/pull/13131#discussion_r1085675650


##########
server-common/src/main/java/org/apache/kafka/server/util/CommandLineUtils.java:
##########
@@ -0,0 +1,201 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.kafka.server.util;
+
+import joptsimple.OptionParser;
+import joptsimple.OptionSet;
+import joptsimple.OptionSpec;
+import org.apache.kafka.common.utils.AppInfoParser;
+import org.apache.kafka.common.utils.Exit;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Optional;
+import java.util.Properties;
+import java.util.Set;
+
+/**
+ * Helper functions for dealing with command line utilities.
+ */
+public class CommandLineUtils {
+    /**
+     * Check if there are no options or `--help` option from command line.
+     *
+     * @param commandOpts Acceptable options for a command
+     * @return true on matching the help check condition
+     */
+    public static boolean isPrintHelpNeeded(CommandDefaultOptions commandOpts) {
+        return commandOpts.args.length == 0 || commandOpts.options.has(commandOpts.helpOpt);
+    }
+
+    /**
+     * Check if there is `--version` option from command line.
+     *
+     * @param commandOpts Acceptable options for a command
+     * @return true on matching the help check condition
+     */
+    public static boolean isPrintVersionNeeded(CommandDefaultOptions commandOpts) {
+        return commandOpts.options.has(commandOpts.versionOpt);
+    }
+
+    /**
+     * Check and print help message if there is no options or `--help` option
+     * from command line, if `--version` is specified on the command line
+     * print version information and exit.
+     * NOTE: The function name is not strictly speaking correct anymore

Review Comment:
   What about maybePrintHelpOrVersion?



-- 
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: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org