Search in sources :

Example 11 with ParseException

use of org.apache.commons.cli.ParseException in project groovy by apache.

the class GroovyMain method process.

/**
     * Process the users request.
     *
     * @param line the parsed command line.
     * @throws ParseException if invalid options are chosen
     */
private static boolean process(CommandLine line) throws ParseException, IOException {
    List args = line.getArgList();
    if (line.hasOption('D')) {
        String[] values = line.getOptionValues('D');
        for (int i = 0; i < values.length; i++) {
            setSystemPropertyFrom(values[i]);
        }
    }
    GroovyMain main = new GroovyMain();
    // add the ability to parse scripts with a specified encoding
    main.conf.setSourceEncoding(line.getOptionValue('c', main.conf.getSourceEncoding()));
    main.isScriptFile = !line.hasOption('e');
    main.debug = line.hasOption('d');
    main.conf.setDebug(main.debug);
    main.conf.setParameters(line.hasOption("pa"));
    main.processFiles = line.hasOption('p') || line.hasOption('n');
    main.autoOutput = line.hasOption('p');
    main.editFiles = line.hasOption('i');
    if (main.editFiles) {
        main.backupExtension = line.getOptionValue('i');
    }
    main.autoSplit = line.hasOption('a');
    String sp = line.getOptionValue('a');
    if (sp != null)
        main.splitPattern = sp;
    if (main.isScriptFile) {
        if (args.isEmpty())
            throw new ParseException("neither -e or filename provided");
        main.script = (String) args.remove(0);
        if (main.script.endsWith(".java"))
            throw new ParseException("error: cannot compile file with .java extension: " + main.script);
    } else {
        main.script = line.getOptionValue('e');
    }
    main.processSockets = line.hasOption('l');
    if (main.processSockets) {
        // default port to listen to
        String p = line.getOptionValue('l', "1960");
        main.port = Integer.parseInt(p);
    }
    // we use "," as default, because then split will create
    // an empty array if no option is set
    String disabled = line.getOptionValue("disableopt", ",");
    String[] deopts = disabled.split(",");
    for (String deopt_i : deopts) {
        main.conf.getOptimizationOptions().put(deopt_i, false);
    }
    if (line.hasOption("indy")) {
        CompilerConfiguration.DEFAULT.getOptimizationOptions().put("indy", true);
        main.conf.getOptimizationOptions().put("indy", true);
    }
    if (line.hasOption("basescript")) {
        main.conf.setScriptBaseClass(line.getOptionValue("basescript"));
    }
    if (line.hasOption("configscript")) {
        String path = line.getOptionValue("configscript");
        File groovyConfigurator = new File(path);
        Binding binding = new Binding();
        binding.setVariable("configuration", main.conf);
        CompilerConfiguration configuratorConfig = new CompilerConfiguration();
        ImportCustomizer customizer = new ImportCustomizer();
        customizer.addStaticStars("org.codehaus.groovy.control.customizers.builder.CompilerCustomizationBuilder");
        configuratorConfig.addCompilationCustomizers(customizer);
        GroovyShell shell = new GroovyShell(binding, configuratorConfig);
        shell.evaluate(groovyConfigurator);
    }
    main.args = args;
    return main.run();
}
Also used : Binding(groovy.lang.Binding) CompilerConfiguration(org.codehaus.groovy.control.CompilerConfiguration) List(java.util.List) ImportCustomizer(org.codehaus.groovy.control.customizers.ImportCustomizer) ParseException(org.apache.commons.cli.ParseException) GroovyShell(groovy.lang.GroovyShell)

Example 12 with ParseException

use of org.apache.commons.cli.ParseException in project hadoop by apache.

the class ApplicationMaster method init.

/**
   * Parse command line options
   *
   * @param args Command line args
   * @return Whether init successful and run should be invoked
   * @throws ParseException
   * @throws IOException
   */
public boolean init(String[] args) throws ParseException, IOException {
    Options opts = new Options();
    opts.addOption("app_attempt_id", true, "App Attempt ID. Not to be used unless for testing purposes");
    opts.addOption("shell_env", true, "Environment for shell script. Specified as env_key=env_val pairs");
    opts.addOption("container_memory", true, "Amount of memory in MB to be requested to run the shell command");
    opts.addOption("container_vcores", true, "Amount of virtual cores to be requested to run the shell command");
    opts.addOption("num_containers", true, "No. of containers on which the shell command needs to be executed");
    opts.addOption("priority", true, "Application Priority. Default 0");
    opts.addOption("container_retry_policy", true, "Retry policy when container fails to run, " + "0: NEVER_RETRY, 1: RETRY_ON_ALL_ERRORS, " + "2: RETRY_ON_SPECIFIC_ERROR_CODES");
    opts.addOption("container_retry_error_codes", true, "When retry policy is set to RETRY_ON_SPECIFIC_ERROR_CODES, error " + "codes is specified with this option, " + "e.g. --container_retry_error_codes 1,2,3");
    opts.addOption("container_max_retries", true, "If container could retry, it specifies max retires");
    opts.addOption("container_retry_interval", true, "Interval between each retry, unit is milliseconds");
    opts.addOption("debug", false, "Dump out debug information");
    opts.addOption("help", false, "Print usage");
    CommandLine cliParser = new GnuParser().parse(opts, args);
    if (args.length == 0) {
        printUsage(opts);
        throw new IllegalArgumentException("No args specified for application master to initialize");
    }
    //Check whether customer log4j.properties file exists
    if (fileExist(log4jPath)) {
        try {
            Log4jPropertyHelper.updateLog4jConfiguration(ApplicationMaster.class, log4jPath);
        } catch (Exception e) {
            LOG.warn("Can not set up custom log4j properties. " + e);
        }
    }
    if (cliParser.hasOption("help")) {
        printUsage(opts);
        return false;
    }
    if (cliParser.hasOption("debug")) {
        dumpOutDebugInfo();
    }
    Map<String, String> envs = System.getenv();
    if (!envs.containsKey(Environment.CONTAINER_ID.name())) {
        if (cliParser.hasOption("app_attempt_id")) {
            String appIdStr = cliParser.getOptionValue("app_attempt_id", "");
            appAttemptID = ApplicationAttemptId.fromString(appIdStr);
        } else {
            throw new IllegalArgumentException("Application Attempt Id not set in the environment");
        }
    } else {
        ContainerId containerId = ContainerId.fromString(envs.get(Environment.CONTAINER_ID.name()));
        appAttemptID = containerId.getApplicationAttemptId();
    }
    if (!envs.containsKey(ApplicationConstants.APP_SUBMIT_TIME_ENV)) {
        throw new RuntimeException(ApplicationConstants.APP_SUBMIT_TIME_ENV + " not set in the environment");
    }
    if (!envs.containsKey(Environment.NM_HOST.name())) {
        throw new RuntimeException(Environment.NM_HOST.name() + " not set in the environment");
    }
    if (!envs.containsKey(Environment.NM_HTTP_PORT.name())) {
        throw new RuntimeException(Environment.NM_HTTP_PORT + " not set in the environment");
    }
    if (!envs.containsKey(Environment.NM_PORT.name())) {
        throw new RuntimeException(Environment.NM_PORT.name() + " not set in the environment");
    }
    LOG.info("Application master for app" + ", appId=" + appAttemptID.getApplicationId().getId() + ", clustertimestamp=" + appAttemptID.getApplicationId().getClusterTimestamp() + ", attemptId=" + appAttemptID.getAttemptId());
    if (!fileExist(shellCommandPath) && envs.get(DSConstants.DISTRIBUTEDSHELLSCRIPTLOCATION).isEmpty()) {
        throw new IllegalArgumentException("No shell command or shell script specified to be executed by application master");
    }
    if (fileExist(shellCommandPath)) {
        shellCommand = readContent(shellCommandPath);
    }
    if (fileExist(shellArgsPath)) {
        shellArgs = readContent(shellArgsPath);
    }
    if (cliParser.hasOption("shell_env")) {
        String[] shellEnvs = cliParser.getOptionValues("shell_env");
        for (String env : shellEnvs) {
            env = env.trim();
            int index = env.indexOf('=');
            if (index == -1) {
                shellEnv.put(env, "");
                continue;
            }
            String key = env.substring(0, index);
            String val = "";
            if (index < (env.length() - 1)) {
                val = env.substring(index + 1);
            }
            shellEnv.put(key, val);
        }
    }
    if (envs.containsKey(DSConstants.DISTRIBUTEDSHELLSCRIPTLOCATION)) {
        scriptPath = envs.get(DSConstants.DISTRIBUTEDSHELLSCRIPTLOCATION);
        if (envs.containsKey(DSConstants.DISTRIBUTEDSHELLSCRIPTTIMESTAMP)) {
            shellScriptPathTimestamp = Long.parseLong(envs.get(DSConstants.DISTRIBUTEDSHELLSCRIPTTIMESTAMP));
        }
        if (envs.containsKey(DSConstants.DISTRIBUTEDSHELLSCRIPTLEN)) {
            shellScriptPathLen = Long.parseLong(envs.get(DSConstants.DISTRIBUTEDSHELLSCRIPTLEN));
        }
        if (!scriptPath.isEmpty() && (shellScriptPathTimestamp <= 0 || shellScriptPathLen <= 0)) {
            LOG.error("Illegal values in env for shell script path" + ", path=" + scriptPath + ", len=" + shellScriptPathLen + ", timestamp=" + shellScriptPathTimestamp);
            throw new IllegalArgumentException("Illegal values in env for shell script path");
        }
    }
    if (envs.containsKey(DSConstants.DISTRIBUTEDSHELLTIMELINEDOMAIN)) {
        domainId = envs.get(DSConstants.DISTRIBUTEDSHELLTIMELINEDOMAIN);
    }
    containerMemory = Integer.parseInt(cliParser.getOptionValue("container_memory", "10"));
    containerVirtualCores = Integer.parseInt(cliParser.getOptionValue("container_vcores", "1"));
    numTotalContainers = Integer.parseInt(cliParser.getOptionValue("num_containers", "1"));
    if (numTotalContainers == 0) {
        throw new IllegalArgumentException("Cannot run distributed shell with no containers");
    }
    requestPriority = Integer.parseInt(cliParser.getOptionValue("priority", "0"));
    containerRetryPolicy = ContainerRetryPolicy.values()[Integer.parseInt(cliParser.getOptionValue("container_retry_policy", "0"))];
    if (cliParser.hasOption("container_retry_error_codes")) {
        containerRetryErrorCodes = new HashSet<>();
        for (String errorCode : cliParser.getOptionValue("container_retry_error_codes").split(",")) {
            containerRetryErrorCodes.add(Integer.parseInt(errorCode));
        }
    }
    containerMaxRetries = Integer.parseInt(cliParser.getOptionValue("container_max_retries", "0"));
    containrRetryInterval = Integer.parseInt(cliParser.getOptionValue("container_retry_interval", "0"));
    if (YarnConfiguration.timelineServiceEnabled(conf)) {
        timelineServiceV2Enabled = ((int) YarnConfiguration.getTimelineServiceVersion(conf) == 2);
        timelineServiceV1Enabled = !timelineServiceV2Enabled;
    } else {
        timelineClient = null;
        timelineV2Client = null;
        LOG.warn("Timeline service is not enabled");
    }
    return true;
}
Also used : Options(org.apache.commons.cli.Options) CommandLine(org.apache.commons.cli.CommandLine) ContainerId(org.apache.hadoop.yarn.api.records.ContainerId) GnuParser(org.apache.commons.cli.GnuParser) URISyntaxException(java.net.URISyntaxException) ParseException(org.apache.commons.cli.ParseException) YarnException(org.apache.hadoop.yarn.exceptions.YarnException) IOException(java.io.IOException) UndeclaredThrowableException(java.lang.reflect.UndeclaredThrowableException) ClientHandlerException(com.sun.jersey.api.client.ClientHandlerException)

Example 13 with ParseException

use of org.apache.commons.cli.ParseException in project hadoop by apache.

the class MiniDFSClusterManager method parseArguments.

/**
   * Parses arguments and fills out the member variables.
   * @param args Command-line arguments.
   * @return true on successful parse; false to indicate that the
   * program should exit.
   */
private boolean parseArguments(String[] args) {
    Options options = makeOptions();
    CommandLine cli;
    try {
        CommandLineParser parser = new GnuParser();
        cli = parser.parse(options, args);
    } catch (ParseException e) {
        LOG.warn("options parsing failed:  " + e.getMessage());
        new HelpFormatter().printHelp("...", options);
        return false;
    }
    if (cli.hasOption("help")) {
        new HelpFormatter().printHelp("...", options);
        return false;
    }
    if (cli.getArgs().length > 0) {
        for (String arg : cli.getArgs()) {
            LOG.error("Unrecognized option: " + arg);
            new HelpFormatter().printHelp("...", options);
            return false;
        }
    }
    // HDFS
    numDataNodes = intArgument(cli, "datanodes", 1);
    nameNodePort = intArgument(cli, "nnport", 0);
    nameNodeHttpPort = intArgument(cli, "httpport", 0);
    if (cli.hasOption("format")) {
        dfsOpts = StartupOption.FORMAT;
        format = true;
    } else {
        dfsOpts = StartupOption.REGULAR;
        format = false;
    }
    // Runner
    writeDetails = cli.getOptionValue("writeDetails");
    writeConfig = cli.getOptionValue("writeConfig");
    // General
    conf = new HdfsConfiguration();
    updateConfiguration(conf, cli.getOptionValues("D"));
    return true;
}
Also used : HelpFormatter(org.apache.commons.cli.HelpFormatter) Options(org.apache.commons.cli.Options) CommandLine(org.apache.commons.cli.CommandLine) GnuParser(org.apache.commons.cli.GnuParser) CommandLineParser(org.apache.commons.cli.CommandLineParser) ParseException(org.apache.commons.cli.ParseException) HdfsConfiguration(org.apache.hadoop.hdfs.HdfsConfiguration)

Example 14 with ParseException

use of org.apache.commons.cli.ParseException in project hadoop by apache.

the class OfflineImageViewerPB method run.

public static int run(String[] args) throws Exception {
    Options options = buildOptions();
    if (args.length == 0) {
        printUsage();
        return 0;
    }
    // print help and exit with zero exit code
    if (args.length == 1 && isHelpOption(args[0])) {
        printUsage();
        return 0;
    }
    CommandLineParser parser = new PosixParser();
    CommandLine cmd;
    try {
        cmd = parser.parse(options, args);
    } catch (ParseException e) {
        System.out.println("Error parsing command-line options: ");
        printUsage();
        return -1;
    }
    if (cmd.hasOption("h")) {
        // print help and exit with non zero exit code since
        // it is not expected to give help and other options together.
        printUsage();
        return -1;
    }
    String inputFile = cmd.getOptionValue("i");
    String processor = cmd.getOptionValue("p", "Web");
    String outputFile = cmd.getOptionValue("o", "-");
    String delimiter = cmd.getOptionValue("delimiter", PBImageDelimitedTextWriter.DEFAULT_DELIMITER);
    String tempPath = cmd.getOptionValue("t", "");
    Configuration conf = new Configuration();
    try (PrintStream out = outputFile.equals("-") ? System.out : new PrintStream(outputFile, "UTF-8")) {
        switch(processor) {
            case "FileDistribution":
                long maxSize = Long.parseLong(cmd.getOptionValue("maxSize", "0"));
                int step = Integer.parseInt(cmd.getOptionValue("step", "0"));
                boolean formatOutput = cmd.hasOption("format");
                new FileDistributionCalculator(conf, maxSize, step, formatOutput, out).visit(new RandomAccessFile(inputFile, "r"));
                break;
            case "XML":
                new PBImageXmlWriter(conf, out).visit(new RandomAccessFile(inputFile, "r"));
                break;
            case "ReverseXML":
                try {
                    OfflineImageReconstructor.run(inputFile, outputFile);
                } catch (Exception e) {
                    System.err.println("OfflineImageReconstructor failed: " + e.getMessage());
                    e.printStackTrace(System.err);
                    System.exit(1);
                }
                break;
            case "Web":
                String addr = cmd.getOptionValue("addr", "localhost:5978");
                try (WebImageViewer viewer = new WebImageViewer(NetUtils.createSocketAddr(addr))) {
                    viewer.start(inputFile);
                }
                break;
            case "Delimited":
                try (PBImageDelimitedTextWriter writer = new PBImageDelimitedTextWriter(out, delimiter, tempPath)) {
                    writer.visit(new RandomAccessFile(inputFile, "r"));
                }
                break;
            default:
                System.err.println("Invalid processor specified : " + processor);
                printUsage();
                return -1;
        }
        return 0;
    } catch (EOFException e) {
        System.err.println("Input file ended unexpectedly. Exiting");
    } catch (IOException e) {
        System.err.println("Encountered exception.  Exiting: " + e.getMessage());
        e.printStackTrace(System.err);
    }
    return -1;
}
Also used : Options(org.apache.commons.cli.Options) PrintStream(java.io.PrintStream) Configuration(org.apache.hadoop.conf.Configuration) PosixParser(org.apache.commons.cli.PosixParser) IOException(java.io.IOException) IOException(java.io.IOException) EOFException(java.io.EOFException) ParseException(org.apache.commons.cli.ParseException) CommandLine(org.apache.commons.cli.CommandLine) RandomAccessFile(java.io.RandomAccessFile) EOFException(java.io.EOFException) CommandLineParser(org.apache.commons.cli.CommandLineParser) ParseException(org.apache.commons.cli.ParseException)

Example 15 with ParseException

use of org.apache.commons.cli.ParseException in project hadoop by apache.

the class OfflineImageViewer method main.

/**
   * Entry point to command-line-driven operation.  User may specify
   * options and start fsimage viewer from the command line.  Program
   * will process image file and exit cleanly or, if an error is
   * encountered, inform user and exit.
   *
   * @param args Command line options
   * @throws IOException 
   */
public static void main(String[] args) throws IOException {
    Options options = buildOptions();
    if (args.length == 0) {
        printUsage();
        return;
    }
    CommandLineParser parser = new PosixParser();
    CommandLine cmd;
    try {
        cmd = parser.parse(options, args);
    } catch (ParseException e) {
        System.out.println("Error parsing command-line options: ");
        printUsage();
        return;
    }
    if (cmd.hasOption("h")) {
        // print help and exit
        printUsage();
        return;
    }
    boolean skipBlocks = cmd.hasOption("skipBlocks");
    boolean printToScreen = cmd.hasOption("printToScreen");
    String inputFile = cmd.getOptionValue("i");
    String processor = cmd.getOptionValue("p", "Ls");
    String outputFile = cmd.getOptionValue("o");
    String delimiter = cmd.getOptionValue("delimiter");
    if (!(delimiter == null || processor.equals("Delimited"))) {
        System.out.println("Can only specify -delimiter with Delimited processor");
        printUsage();
        return;
    }
    ImageVisitor v;
    if (processor.equals("Indented")) {
        v = new IndentedImageVisitor(outputFile, printToScreen);
    } else if (processor.equals("XML")) {
        v = new XmlImageVisitor(outputFile, printToScreen);
    } else if (processor.equals("Delimited")) {
        v = delimiter == null ? new DelimitedImageVisitor(outputFile, printToScreen) : new DelimitedImageVisitor(outputFile, printToScreen, delimiter);
        skipBlocks = false;
    } else if (processor.equals("FileDistribution")) {
        long maxSize = Long.parseLong(cmd.getOptionValue("maxSize", "0"));
        int step = Integer.parseInt(cmd.getOptionValue("step", "0"));
        boolean formatOutput = cmd.hasOption("format");
        v = new FileDistributionVisitor(outputFile, maxSize, step, formatOutput);
    } else if (processor.equals("NameDistribution")) {
        v = new NameDistributionVisitor(outputFile, printToScreen);
    } else {
        v = new LsImageVisitor(outputFile, printToScreen);
        skipBlocks = false;
    }
    try {
        OfflineImageViewer d = new OfflineImageViewer(inputFile, v, skipBlocks);
        d.go();
    } catch (EOFException e) {
        System.err.println("Input file ended unexpectedly.  Exiting");
    } catch (IOException e) {
        System.err.println("Encountered exception.  Exiting: " + e.getMessage());
    }
}
Also used : Options(org.apache.commons.cli.Options) PosixParser(org.apache.commons.cli.PosixParser) IOException(java.io.IOException) CommandLine(org.apache.commons.cli.CommandLine) EOFException(java.io.EOFException) CommandLineParser(org.apache.commons.cli.CommandLineParser) ParseException(org.apache.commons.cli.ParseException)

Aggregations

ParseException (org.apache.commons.cli.ParseException)587 CommandLine (org.apache.commons.cli.CommandLine)489 CommandLineParser (org.apache.commons.cli.CommandLineParser)381 Options (org.apache.commons.cli.Options)370 DefaultParser (org.apache.commons.cli.DefaultParser)220 HelpFormatter (org.apache.commons.cli.HelpFormatter)205 GnuParser (org.apache.commons.cli.GnuParser)173 IOException (java.io.IOException)124 Option (org.apache.commons.cli.Option)109 File (java.io.File)90 PosixParser (org.apache.commons.cli.PosixParser)65 Path (org.apache.hadoop.fs.Path)50 ArrayList (java.util.ArrayList)42 Properties (java.util.Properties)35 BasicParser (org.apache.commons.cli.BasicParser)32 FileInputStream (java.io.FileInputStream)29 Job (org.apache.hadoop.mapreduce.Job)27 Configuration (org.apache.hadoop.conf.Configuration)26 List (java.util.List)25 URI (java.net.URI)21