Example usage for org.apache.commons.cli PosixParser parse

List of usage examples for org.apache.commons.cli PosixParser parse

Introduction

In this page you can find the example usage for org.apache.commons.cli PosixParser parse.

Prototype

public CommandLine parse(Options options, String[] arguments, Properties properties) throws ParseException 

Source Link

Document

Parse the arguments according to the specified options and properties.

Usage

From source file:uni.bielefeld.cmg.sparkhit.util.ParameterForScriptPiper.java

public DefaultParam importCommandLine() {

    /* Assigning Parameter ID to an ascending number */
    putParameterID();/*from w w  w .  ja  v a  2s .  co  m*/

    /* Assigning parameter descriptions to each parameter ID */
    addParameterInfo();

    /* need a Object parser of PosixParser class for the function parse of CommandLine class */
    PosixParser parser = new PosixParser();

    /* print out help information */
    HelpParam help = new HelpParam(parameter, parameterMap);

    /* check each parameter for assignment */
    try {
        long input_limit = -1;
        int threads = Runtime.getRuntime().availableProcessors();

        /* Set Object cl of CommandLine class for Parameter storage */
        CommandLine cl = parser.parse(parameter, arguments, true);
        if (cl.hasOption(HELP)) {
            help.printScriptPiperHelp();
            System.exit(0);
        }

        if (cl.hasOption(HELP2)) {
            help.printScriptPiperHelp();
            System.exit(0);
        }

        if (cl.hasOption(VERSION)) {
            System.exit(0);
        }

        /* Checking all parameters */

        String value;

        if ((value = cl.getOptionValue(PARTITIONS)) != null) {
            param.partitions = Integer.decode(value);
        }

        if ((value = cl.getOptionValue(INPUT_FASTQ)) != null) {
            param.inputFqPath = value;
        } else if ((value = cl.getOptionValue(INPUT_LINE)) != null) {
            param.inputFqLinePath = value;
            param.inputFqPath = value;
        } else {
            help.printScriptPiperHelp();
            System.exit(0);
            //                throw new IOException("Input file not specified.\nUse -help for list of options");
        }

        /* not applicable for HDFS and S3 */
        /* using TextFileBufferInput for such purpose */
        //         File inputFastq = new File(param.inputFqPath).getAbsoluteFile();
        //         if (!inputFastq.exists()){
        //            err.println("Input query file not found.");
        //            return;
        //i         }

        if ((value = cl.getOptionValue(OUTPUT_LINE)) != null) {
            param.outputPath = value;
        } else {
            help.printScriptPiperHelp();
            info.readMessage("Output file not set with -outfile options");
            info.screenDump();
            System.exit(0);
        }

        File outfile = new File(param.outputPath).getAbsoluteFile();
        if (outfile.exists()) {
            info.readParagraphedMessages(
                    "Output file : \n\t" + param.outputPath + "\nalready exists, will be overwrite.");
            info.screenDump();
            Runtime.getRuntime().exec("rm -rf " + param.outputPath);
        }

        if (cl.hasOption(FASTQ_FILTER)) {
            param.filterFastq = true;
        }

        if (cl.hasOption(FASTA_CONVERT)) {
            param.filterToFasta = true;
        }

        if (cl.hasOption(LINE_FASTA)) {
            param.lineToFasta = true;
        }

        if ((value = cl.getOptionValue(INPUT_TOOL)) != null) {
            param.tool = value;
            info.readParagraphedMessages("External tool locate at : \n\t" + param.tool
                    + "\nplease ensure that all nodes have such tools in the same path.\nOr it is located in a shared file system.\nOr use a Docker container");
            info.screenDump();
        } else {
            help.printScriptPiperHelp();
            info.readMessage("External tool path have not been set with parameter -tool");
            info.screenDump();
            System.exit(0);
        }

        if ((value = cl.getOptionValue(TOOL_DEPEND)) != null) {
            param.toolDepend = value;
            info.readParagraphedMessages("External tool Dependencies : \n\t" + param.toolDepend + "\n");
            info.screenDump();
        } else {
            info.readMessage(
                    "External tool dependencies have not been set, will run with tool build-in Interpreter");
            info.screenDump();
        }

        if ((value = cl.getOptionValue(TOOL_PARAM)) != null) {
            param.toolParam = value;
            info.readParagraphedMessages("External tool parameter : \n\t" + param.toolParam
                    + "\nThe entire command looks like : \n\t" + param.toolDepend + " " + param.tool + " "
                    + param.toolParam);
            info.screenDump();
            info.readParagraphedMessages("Final external command is : \n\t" + param.toolDepend + " "
                    + param.tool + " " + param.toolParam);
            info.screenDump();
        } else {
            info.readMessage("External tool parameter have not been set, will run with its default parameter");
            info.screenDump();
        }

        // param.bestNas = (param.alignLength * param.readIdentity) / 100;
        // param.bestKmers = param.alignLength - (param.alignLength - param.bestNas) * 4 - 3;

    } catch (IOException e) { // Don`t catch this, NaNaNaNa, U can`t touch this.
        info.readMessage("Parameter settings incorrect.");
        info.screenDump();
        e.printStackTrace();
        System.exit(0);
    } catch (RuntimeException e) {
        info.readMessage("Parameter settings incorrect.");
        info.screenDump();
        e.printStackTrace();
        System.exit(0);
    } catch (ParseException e) {
        info.readMessage("Parameter settings incorrect.");
        info.screenDump();
        e.printStackTrace();
        System.exit(0);
    }

    return param;
}

From source file:uni.bielefeld.cmg.sparkhit.util.ParameterForVariantCaller.java

public DefaultParam importCommandLine() {

    /* Assigning Parameter ID to an ascending number */
    putParameterID();/*ww w.  ja v  a  2 s. c o m*/

    /* Assigning parameter descriptions to each parameter ID */
    addParameterInfo();

    /* need a Object parser of PosixParser class for the function parse of CommandLine class */
    PosixParser parser = new PosixParser();

    /* print out help information */
    HelpParam help = new HelpParam(parameter, parameterMap);

    /* check each parameter for assignment */
    try {
        long input_limit = -1;
        int threads = Runtime.getRuntime().availableProcessors();

        /* Set Object cl of CommandLine class for Parameter storage */
        CommandLine cl = parser.parse(parameter, arguments, true);
        if (cl.hasOption(HELP)) {
            help.printScriptPiperHelp();
            System.exit(0);
        }

        if (cl.hasOption(HELP2)) {
            help.printScriptPiperHelp();
            System.exit(0);
        }

        if (cl.hasOption(VERSION)) {
            System.exit(0);
        }

        /* Checking all parameters */

        String value;

        if ((value = cl.getOptionValue(PARTITIONS)) != null) {
            param.partitions = Integer.decode(value);
        }

        if ((value = cl.getOptionValue(INPUT_LIST)) != null) {
            param.inputList = value;
        } else {
            help.printScriptPiperHelp();
            System.exit(0);
            //                throw new IOException("Input file not specified.\nUse -help for list of options");
        }

        /* not applicable for HDFS and S3 */
        /* using TextFileBufferInput for such purpose */
        //         File inputFastq = new File(param.inputFqPath).getAbsoluteFile();
        //         if (!inputFastq.exists()){
        //            err.println("Input query file not found.");
        //            return;
        //i         }

        if ((value = cl.getOptionValue(OUTPUT_LINE)) != null) {
            param.outputPath = value;
        } else {
            help.printScriptPiperHelp();
            info.readMessage("Output file not set with -outfile options");
            info.screenDump();
            System.exit(0);
        }

        File outfile = new File(param.outputPath).getAbsoluteFile();
        if (outfile.exists()) {
            info.readParagraphedMessages(
                    "Output file : \n\t" + param.outputPath + "\nalready exists, will be overwrite.");
            info.screenDump();
            Runtime.getRuntime().exec("rm -rf " + param.outputPath);
        }

        if ((value = cl.getOptionValue(INPUT_TOOL)) != null) {
            param.tool = value.replaceAll("\"", "");
            info.readParagraphedMessages("External tool locate at : \n\t" + param.tool
                    + "\nplease ensure that all nodes have such tools in the same path.\nOr it is located in a shared file system.\nOr use a Docker container");
            info.screenDump();
        } else {
            help.printScriptPiperHelp();
            info.readMessage("External tool path have not been set with parameter -tool");
            info.screenDump();
            System.exit(0);
        }

        if ((value = cl.getOptionValue(TOOL_DEPEND)) != null) {
            param.toolDepend = value.replaceAll("\"", "");
            info.readParagraphedMessages("External tool Dependencies : \n\t" + param.toolDepend + "\n");
            info.screenDump();
        } else {
            info.readMessage(
                    "External tool dependencies have not been set, will run with tool build-in Interpreter");
            info.screenDump();
        }

        if ((value = cl.getOptionValue(TOOL_PARAM)) != null) {
            param.toolParam = value.replaceAll("\"", "");
            info.readParagraphedMessages("External tool parameter : \n\t" + param.toolParam
                    + "\nThe entire command looks like : \n\t" + param.toolDepend + " " + param.tool + " "
                    + param.toolParam);
            info.screenDump();
            info.readParagraphedMessages("Final external command is : \n\t" + param.toolDepend + " "
                    + param.tool + " " + param.toolParam);
            info.screenDump();
        } else {
            info.readMessage("External tool parameter have not been set, will run with its default parameter");
            info.screenDump();
        }

        // param.bestNas = (param.alignLength * param.readIdentity) / 100;
        // param.bestKmers = param.alignLength - (param.alignLength - param.bestNas) * 4 - 3;

    } catch (IOException e) { // Don`t catch this, NaNaNaNa, U can`t touch this.
        info.readMessage("Parameter settings incorrect.");
        info.screenDump();
        e.printStackTrace();
        System.exit(0);
    } catch (RuntimeException e) {
        info.readMessage("Parameter settings incorrect.");
        info.screenDump();
        e.printStackTrace();
        System.exit(0);
    } catch (ParseException e) {
        info.readMessage("Parameter settings incorrect.");
        info.screenDump();
        e.printStackTrace();
        System.exit(0);
    }

    return param;
}