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-unsubscr...@kafka.apache.org

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

Reply via email to