Example usage for org.apache.commons.cli2 CommandLine hasOption

List of usage examples for org.apache.commons.cli2 CommandLine hasOption

Introduction

In this page you can find the example usage for org.apache.commons.cli2 CommandLine hasOption.

Prototype

boolean hasOption(final Option option);

Source Link

Document

Detects the presence of an option in this CommandLine.

Usage

From source file:org.rvsnoop.ui.RvSnoopApplication.java

private CommandLine parseCommandLine(String[] args, Option helpOption, Option projectOption) {
    Group group = new GroupBuilder().withOption(helpOption).withOption(projectOption).create();
    Parser parser = new Parser();
    parser.setGroup(group);//  ww  w.ja v  a2s  .  c  o m
    parser.setHelpOption(helpOption);
    parser.setHelpFormatter(new HelpFormatter());
    CommandLine line = parser.parseAndHelp(args);
    if (line.hasOption(helpOption)) {
        System.exit(0);
    }
    return line;
}

From source file:org.rvsnoop.ui.RvSnoopApplication.java

private File loadProjectIfValid(Option project, CommandLine line) {
    if (!line.hasOption(project)) {
        return null;
    }/*  w w  w  . j a v a 2s.  c o  m*/
    String filename = line.getValue(project).toString();
    if (filename == null || filename.length() == 0) {
        logger.warn(getString("CLI.warn.noProject"));
        return null;
    }
    File file = new File(filename);
    if (!file.canRead()) {
        logger.error(getString("CLI.error.unreadableProject"), filename);
        return null;
    }
    return file;
}

From source file:tk.summerway.mahout9.tools.MyClusterDumper.java

private boolean buildParse(String[] args) {
    DefaultOptionBuilder obuilder = new DefaultOptionBuilder();
    ArgumentBuilder abuilder = new ArgumentBuilder();
    GroupBuilder gbuilder = new GroupBuilder();

    Option inputDirOpt = DefaultOptionCreator.inputOption().create();
    Option outputDirOpt = DefaultOptionCreator.outputOption().create();

    Option outputFormatOpt = obuilder.withLongName(OUTPUT_FORMAT_OPT)
            .withArgument(abuilder.withName(OUTPUT_FORMAT_OPT).create())
            .withDescription(/*from   w ww .j  a v  a  2  s. com*/
                    "The optional output format for the results. Options: TEXT, CSV, JSON or GRAPH_ML. Default is TEXT")
            .withShortName("of").create();

    Option substringOpt = obuilder.withLongName(SUBSTRING_OPTION)
            .withArgument(abuilder.withName(SUBSTRING_OPTION).create())
            .withDescription("The number of chars of the asFormatString() to print").withShortName("b")
            .create();

    Option pointsDirOpt = obuilder.withLongName(POINTS_DIR_OPTION)
            .withArgument(abuilder.withName(POINTS_DIR_OPTION).create())
            .withDescription(
                    "The directory containing points sequence files mapping input vectors to their cluster. "
                            + "If specified, then the program will output the points associated with a cluster")
            .withShortName("p").create();

    Option samplePointsOpt = obuilder.withLongName(SAMPLE_POINTS)
            .withArgument(abuilder.withName(SAMPLE_POINTS).create())
            .withDescription("Specifies the maximum number of points to include _per_ cluster.  The default "
                    + "is to include all points")
            .withShortName("sp").create();

    Option dictionaryOpt = obuilder.withLongName(DICTIONARY_OPTION)
            .withArgument(abuilder.withName(DICTIONARY_OPTION).create()).withDescription("The dictionary file")
            .withShortName("d").create();

    Option dictionaryTypeOpt = obuilder.withLongName(DICTIONARY_TYPE_OPTION)
            .withArgument(abuilder.withName(DICTIONARY_TYPE_OPTION).create())
            .withDescription("The dictionary file type (text|sequencefile), default is text")
            .withShortName("dt").create();

    Option numWordsOpt = obuilder.withLongName(NUM_WORDS_OPTION)
            .withArgument(abuilder.withName(NUM_WORDS_OPTION).create())
            .withDescription("The number of top terms to print").withShortName("n").create();

    Option evaluateOpt = obuilder.withLongName(EVALUATE_CLUSTERS)
            .withArgument(abuilder.withName(EVALUATE_CLUSTERS).create())
            .withDescription("Run ClusterEvaluator and CDbwEvaluator over the input.  "
                    + "The output will be appended to the rest of the output at the end. Default is false.")
            .withShortName("e").create();

    Option distanceMeasureOpt = obuilder.withLongName("distanceMeasure")
            .withArgument(abuilder.withName("distanceMeasure").create())
            .withDescription("k-means distance measure class name").withShortName("dm").create();

    Option helpOpt = obuilder.withLongName("help").withDescription("Print out help").withShortName("h")
            .create();

    Group group = gbuilder.withName("Options").withOption(inputDirOpt).withOption(outputDirOpt)
            .withOption(outputFormatOpt).withOption(substringOpt).withOption(pointsDirOpt)
            .withOption(samplePointsOpt).withOption(dictionaryOpt).withOption(dictionaryTypeOpt)
            .withOption(numWordsOpt).withOption(evaluateOpt).withOption(distanceMeasureOpt).withOption(helpOpt)
            .create();
    try {
        Parser parser = new Parser();
        parser.setGroup(group);
        parser.setHelpOption(helpOpt);
        CommandLine cmdLine = parser.parse(args);

        if (cmdLine.hasOption(helpOpt)) {
            CommandLineUtil.printHelp(group);
            return false;
        }

        seqFileDir = getInputPath();
        inputPath = getInputPath();
        inputFile = getInputFile();
        if (cmdLine.hasOption(inputDirOpt)) {
            seqFileDir = new Path(cmdLine.getValue(inputDirOpt).toString());
            inputPath = new Path(cmdLine.getValue(inputDirOpt).toString());
            inputFile = new File(cmdLine.getValue(inputDirOpt).toString());
        }
        log.info("seqFileDir value: {}", seqFileDir);
        log.info("inputPath value: {}", inputPath);
        log.info("inputFile value: {}", inputFile);

        outputPath = getOutputPath();
        outputFile = getOutputFile();
        if (cmdLine.hasOption(outputDirOpt)) {
            outputPath = new Path(cmdLine.getValue(outputDirOpt).toString());
            outputFile = new File(cmdLine.getValue(outputDirOpt).toString());
        }
        log.info("outputPath value: {}", outputPath);
        log.info("outputFile value: {}", outputFile);

        if (cmdLine.hasOption(pointsDirOpt)) {
            pointsDir = new Path(cmdLine.getValue(pointsDirOpt).toString());
        }
        log.info("pointsDir value: {}", pointsDir);

        if (cmdLine.hasOption(substringOpt)) {
            int sub = Integer.parseInt(cmdLine.getValue(substringOpt).toString());
            if (sub >= 0) {
                subString = sub;
            }
        }
        log.info("subString value: {}", subString);

        termDictionary = cmdLine.getValue(dictionaryOpt).toString();
        dictionaryFormat = cmdLine.getValue(dictionaryTypeOpt).toString();
        log.info("termDictionary value: {}", termDictionary);
        log.info("dictionaryFormat value: {}", dictionaryFormat);

        if (cmdLine.hasOption(numWordsOpt)) {
            numTopFeatures = Integer.parseInt(cmdLine.getValue(numWordsOpt).toString());
        }
        log.info("numTopFeatures value: {}", numTopFeatures);

        outputFormat = OUTPUT_FORMAT.TEXT;
        if (cmdLine.hasOption(outputFormatOpt)) {
            outputFormat = OUTPUT_FORMAT.valueOf(cmdLine.getValue(outputFormatOpt).toString());
        }
        log.info("outputFormat value: {}", outputFormat);

        if (cmdLine.hasOption(samplePointsOpt)) {
            maxPointsPerCluster = Long.parseLong(cmdLine.getValue(samplePointsOpt).toString());
        } else {
            maxPointsPerCluster = Long.MAX_VALUE;
        }
        log.info("maxPointsPerCluster value: {}", maxPointsPerCluster);

        runEvaluation = cmdLine.hasOption(evaluateOpt);
        log.info("runEvaluation value: {}", runEvaluation);

        String distanceMeasureClass = null;
        if (cmdLine.hasOption(distanceMeasureOpt)) {
            distanceMeasureClass = cmdLine.getValue(distanceMeasureOpt).toString();
        }
        if (distanceMeasureClass != null) {
            measure = ClassUtils.instantiateAs(distanceMeasureClass, DistanceMeasure.class);
        }
        log.info("distanceMeasureClass value: {}", distanceMeasureClass);

    } catch (OptionException e) {
        CommandLineUtil.printHelp(group);
        log.error("parse para error", e);
    }
    return true;
}