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

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

Introduction

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

Prototype

public CommandLine parse(Options options, String[] arguments, boolean stopAtNonOption) throws ParseException 

Source Link

Usage

From source file:uk.ac.ebi.aamtool.ReactionDecoder.java

/**
 * @param args the command line areactionWithLayoutgumentheaderString
 *///  ww w.  j  a  va 2 s  .c o m
public static void main(String[] args) {
    try {
        CommandLineOptions cmd = new CommandLineOptions();
        Options createAAMOptions = cmd.createAAMOptions();
        Options createCompareOptions = cmd.createCompareOptions();
        Options createAnnotateOptions = cmd.createAnnotateOptions();

        DefaultParser parser1 = new DefaultParser();
        CommandLine aamLine = parser1.parse(createAAMOptions, args, true);
        DefaultParser parser2 = new DefaultParser();
        CommandLine compareLine = parser2.parse(createCompareOptions, args, true);
        DefaultParser parser3 = new DefaultParser();
        CommandLine annotateLine = parser3.parse(createAnnotateOptions, args, true);

        /*
         * Print the Header
         */
        getHeader();

        if (aamLine.hasOption('j') && aamLine.getOptionValue("j").equalsIgnoreCase("AAM")
                && aamLine.hasOption('Q') && aamLine.hasOption('q') && aamLine.hasOption('f')) {

            out.println("-- AAM --");
            ReactionDecoder rxn = new ReactionDecoder();
            rxn.AAMTask(aamLine, createAAMOptions);

        } else if (compareLine.hasOption('j') && compareLine.getOptionValue("j").equalsIgnoreCase("COMPARE")
                && compareLine.hasOption('Q') && compareLine.hasOption('q') && compareLine.hasOption('T')
                && compareLine.hasOption('t') && compareLine.hasOption('f')) {

            out.println("-- COMPARE --");
            ReactionDecoder rxn = new ReactionDecoder();
            rxn.CompareTask(compareLine, createCompareOptions);

        } else if (annotateLine.hasOption('j') && annotateLine.getOptionValue("j").equalsIgnoreCase("ANNOTATE")
                && annotateLine.hasOption('Q') && annotateLine.hasOption('q') && annotateLine.hasOption('f')) {

            out.println("-- ANNOTATE --");
            ReactionDecoder rxn = new ReactionDecoder();
            rxn.AnnotateTask(annotateLine, createAnnotateOptions);

        } else if (aamLine.hasOption('j') && aamLine.getOptionValue("j").equalsIgnoreCase("AAM")) {
            out.println("-- AAM USAGE --");
            printHelp(out, createAAMOptions);
        } else if (compareLine.hasOption('j') && compareLine.getOptionValue("j").equalsIgnoreCase("COMPARE")) {
            out.println("-- REACTION COMPARE USAGE --");
            printHelp(out, createCompareOptions);
        } else if (compareLine.hasOption('j') && compareLine.getOptionValue("j").equalsIgnoreCase("ANNOTATE")) {
            out.println("-- REACTION ANNOTATION USAGE --");
            printHelp(out, createAnnotateOptions);
        } else {
            out.println("-- REACTION DECODER HELP --");
            Map<String, Options> options = new TreeMap<>();
            options.put("Atom-Atom Mapping (AAM-Tool)", createAAMOptions);
            options.put("Reaction Annotation (RA-Tool)", createAnnotateOptions);
            options.put("Reaction Comparison (RC-Tool)", createCompareOptions);
            printHelp(options, 80, "EC-BLAST", "End of Help", 5, 3, true, out);
        }
    } catch (ParseException ex) {
        getLogger(ReactionDecoder.class.getName()).log(SEVERE, null, ex);
    } catch (Exception ex) {
        getLogger(ReactionDecoder.class.getName()).log(SEVERE, null, ex);
    }

}

From source file:uk.ac.ebi.smsd.cmd.ArgumentHandler.java

/**
 * Parses the options in the command line arguments and returns an array of
 * strings corresponding to the filenames given as arguments only
 *
 * @param args// ww  w  .j a  v  a  2 s  .com
 * @throws org.apache.commons.cli.ParseException
 */
@SuppressWarnings("static-access")
public void parseCommandLineOptions(String[] args) throws ParseException {

    options = new Options();

    options.addOption("h", "help", false, "Help page for command usage");

    options.addOption("s", false, "SubStructure detection");

    options.addOption("a", false, "Add Hydrogen");

    options.addOption("x", false, "Match Atom Type");

    options.addOption("r", false, "Remove Hydrogen");

    options.addOption("z", false, "Ring matching");

    options.addOption("b", false, "Match Bond types (Single, Double etc)");

    options.addOption(Option.builder("q").argName("query_file").hasArg().desc("Query filename").build());

    options.addOption(Option.builder("t").argName("target_file").hasArg().desc("Target filename").build());
    options.addOption(Option.builder("S").argName("suffix").hasArg().desc("Add suffix to the files").build());

    options.addOption("g", false, "create png of the mapping");

    options.addOption(Option.builder("d").argName("WIDTHxHEIGHT").hasArg()
            .desc("Dimension of the image in pixels").build());

    options.addOption("m", false, "Report all Mappings");

    String filterDescr = "Default: 0, Stereo: 1, " + "Stereo+Fragment: 2, Stereo+Fragment+Energy: 3";

    options.addOption(Option.builder("f").argName("filter_number").hasArg().desc(filterDescr).build());

    options.addOption("A", false, "Appends output to existing files, else creates new files");

    options.addOption(Option.builder("N").desc("Do N-way MCS on the target SD file").build());

    options.addOption(
            Option.builder("Q").argName("query_type").hasArg().desc("Query type (MOL, SMI, etc.)").build());

    options.addOption(Option.builder("T").argName("target_type").hasArg()
            .desc("Target type (MOL, SMI, SMIF, etc.)").build());

    options.addOption(
            Option.builder("o").argName("filename").hasArg().desc("Output the substructure to a file").build());

    options.addOption(Option.builder("O").argName("type").hasArg().desc("Output type (SMI, MOL)").build());

    options.addOption(Option.builder("I").argName("option=value").hasArg().desc("Image options").build());

    DefaultParser parser = new DefaultParser();
    CommandLine line = parser.parse(options, args, true);

    if (line.hasOption('Q')) {
        queryType = line.getOptionValue("Q");
    } //else {
    //            queryType = "MOL";
    //        } //XXX default type?

    if (line.hasOption('T')) {
        targetType = line.getOptionValue("T");
    } else {
        targetType = "MOL";
    }

    if (line.hasOption('a')) {
        this.setApplyHAdding(true);
    }

    if (line.hasOption('r')) {
        this.setApplyHRemoval(true);
    }

    if (line.hasOption('m')) {
        this.setAllMapping(true);
    }

    if (line.hasOption('s')) {
        this.setSubstructureMode(true);
    }

    if (line.hasOption('g')) {
        this.setImage(true);
    }

    if (line.hasOption('b')) {
        this.setMatchBondType(true);
    }

    if (line.hasOption('z')) {
        this.setMatchRingType(true);
    }

    if (line.hasOption('x')) {
        this.setMatchAtomType(true);
    }

    remainingArgs = line.getArgs();

    if (line.hasOption('h') || line.getOptions().length == 0) {
        //            System.out.println("Hello");
        helpRequested = true;
    }

    if (line.hasOption('S')) {
        String[] suffix_reader = line.getOptionValues('S');
        if (suffix_reader.length < 1) {
            System.out.println("Suffix required!");
            helpRequested = true;
        }
        setSuffix(suffix_reader[0]);
        setApplySuffix(true);
    }

    if (line.hasOption('f')) {
        String[] filters = line.getOptionValues('f');
        if (filters.length < 1) {
            System.out.println("Chemical filter required (Ranges: 0 to 3)!");
            helpRequested = true;
        }
        setChemFilter((int) new Integer(filters[0]));
    }

    if (line.hasOption('q')) {
        queryFilepath = line.getOptionValue('q');
    }

    if (line.hasOption('t')) {
        targetFilepath = line.getOptionValue('t');
    }

    if (line.hasOption("A")) {
        this.setAppendMode(true);
    }

    if (line.hasOption("N")) {
        setNMCS(true);
    }

    if (line.hasOption("o")) {
        outputSubgraph = true;
        outputFilepath = line.getOptionValue("o");
    }

    if (line.hasOption("O")) {
        outputFiletype = line.getOptionValue("O");
    } else {
        outputFiletype = "MOL";
    }

    if (line.hasOption("d")) {
        String dimensionString = line.getOptionValue("d");
        if (dimensionString.contains("x")) {
            String[] parts = dimensionString.split("x");
            try {
                setImageWidth(Integer.parseInt(parts[0]));
                setImageHeight(Integer.parseInt(parts[1]));
                System.out.println("set image dim to " + getImageWidth() + "x" + getImageHeight());
            } catch (NumberFormatException nfe) {
                throw new ParseException("Malformed dimension string " + dimensionString);
            }
        } else {
            throw new ParseException("Malformed dimension string " + dimensionString);
        }
    }

    if (line.hasOption("I")) {
        imageProperties = line.getOptionProperties("I");
        if (imageProperties.isEmpty()) {
            // used just "-I" by itself
            isImageOptionHelp = true;
        }
    }
}