sunjincheng121 commented on a change in pull request #8675: [FLINK-12716][python] Add an interactive shell for Python Table API URL: https://github.com/apache/flink/pull/8675#discussion_r293640776
########## File path: flink-python/src/main/java/org/apache/flink/python/client/PythonShellParser.java ########## @@ -0,0 +1,322 @@ +/* + * 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.flink.python.client; + +import org.apache.flink.util.Preconditions; + +import org.apache.commons.cli.CommandLine; +import org.apache.commons.cli.DefaultParser; +import org.apache.commons.cli.HelpFormatter; +import org.apache.commons.cli.Option; +import org.apache.commons.cli.Options; +import org.apache.commons.cli.ParseException; + +import java.util.ArrayList; +import java.util.List; + +/** + * Command line parser for Python shell. + */ +public class PythonShellParser { + private static final Option OPTION_HELP = Option + .builder("h") + .required(false) + .longOpt("help") + .desc("Show the help message with descriptions of all options.") + .build(); + + private static final Option OPTION_CONTAINER = Option + .builder("n") + .required(false) + .longOpt("container") + .hasArg() + .desc("Number of YARN container to allocate (=Number of Task Managers)") + .build(); + + private static final Option OPTION_JM_MEMORY = Option + .builder("jm") + .required(false) + .longOpt("jobManagerMemory") + .hasArg() + .desc("Memory for JobManager Container with optional unit (default: MB)") + .build(); + + private static final Option OPTION_NAME = Option + .builder("nm") + .required(false) + .longOpt("name") + .hasArg() + .desc("Set a custom name for the application on YARN") + .build(); + + private static final Option OPTION_QUEUE = Option + .builder("qu") + .required(false) + .longOpt("queue") + .hasArg() + .desc("Specify YARN queue.") + .build(); + + private static final Option OPTION_SLOTS = Option + .builder("s") + .required(false) + .longOpt("slots") + .hasArg() + .desc("Number of slots per TaskManager") + .build(); + + private static final Option OPTION_TM_MEMORY = Option + .builder("tm") + .required(false) + .longOpt("taskManagerMemory") + .hasArg() + .desc("Memory per TaskManager Container with optional unit (default: MB)") + .build(); + + // cluster types + private static final String LOCAL_RUN = "local"; + private static final String REMOTE_RUN = "remote"; + private static final String YARN_RUN = "yarn"; + + // Options that will be used in mini cluster. + private static final Options LOCAL_OPTIONS = getLocalOptions(new Options()); + + // Options that will be used in remote cluster. + private static final Options REMOTE_OPTIONS = getRemoteOptions(new Options()); + + // Options that will be used in yarn cluster. + private static final Options YARN_OPTIONS = getYarnOptions(new Options()); + + public static void main(String[] args) { + if (args.length < 1) { + printError("You should specify cluster type or -h | --help option"); + System.exit(1); + } + String command = args[0]; + List<String> commandOptions = null; + try { + switch (command) { + case LOCAL_RUN: + commandOptions = parseLocal(args); + break; + case REMOTE_RUN: + commandOptions = parseRemote(args); + break; + case YARN_RUN: + commandOptions = parseYarn(args); + break; + case "-h": + case "--help": + printHelp(); + break; + default: + printError(String.format("\"%s\" is not a valid cluster type or -h | --help option.\n", command)); + System.exit(1); + } + if (commandOptions != null) { + for (String option : commandOptions) { + System.out.print(option); + System.out.print('\0'); + } + } + } catch (Throwable e) { + printError("Error while running the command."); + e.printStackTrace(); + System.exit(1); + } + } + + private static void buildGeneralOptions(Options options) { + options.addOption(OPTION_HELP); + } + + private static Options getLocalOptions(Options options) { + buildGeneralOptions(options); + return options; + } + + private static Options getRemoteOptions(Options options) { + buildGeneralOptions(options); + return options; + } + + private static Options getYarnOptions(Options options) { + buildGeneralOptions(options); + options.addOption(OPTION_CONTAINER); + options.addOption(OPTION_JM_MEMORY); + options.addOption(OPTION_NAME); + options.addOption(OPTION_QUEUE); + options.addOption(OPTION_SLOTS); + options.addOption(OPTION_TM_MEMORY); + return options; + } + + /** + * Prints the error message and help for the client. + * + * @param msg error message + */ + private static void printError(String msg) { + System.err.println(msg); + System.err.println("Valid cluster type are \"local\", \"remote <hostname> <portnumber>\", \"yarn\"."); + System.err.println(); + System.err.println("Specify the help option (-h or --help) to get help on the command."); + } + + /** + * Prints the help for the client. + */ + private static void printHelp() { Review comment: I think we should have deffrence help message for `local`, `remote` and `yarn`, otherwise we do not need add the `-h` option for `local`, `remote` and `yarn`. What do you think? ---------------------------------------------------------------- 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. For queries about this service, please contact Infrastructure at: us...@infra.apache.org With regards, Apache Git Services