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:com.martinkampjensen.thesis.Main.java

private static boolean query(CommandLine cmdLine) {
    // Options./* w w w .j a  v a2 s  .  co m*/
    if (cmdLine.hasOption(_oSeed)) {
        seed(cmdLine);
    }

    // Actions.
    if (cmdLine.hasOption(_oAnalyze)) {
        analyze(cmdLine);
    } else if (cmdLine.hasOption(_oBarriers)) {
        barriers(cmdLine);
    } else if (cmdLine.hasOption(_oCheck)) {
        check(cmdLine);
    } else if (cmdLine.hasOption(_oConnect)) {
        connect(cmdLine);
    } else if (cmdLine.hasOption(_oEvaluate)) {
        evaluate(cmdLine);
    } else if (cmdLine.hasOption(_oExecute)) {
        execute(cmdLine);
    } else if (cmdLine.hasOption(_oExtract)) {
        extract(cmdLine);
    } else if (cmdLine.hasOption(_oMinimize)) {
        minimize(cmdLine);
    } else if (cmdLine.hasOption(_oPrint)) {
        print(cmdLine);
    } else if (cmdLine.hasOption(_oSample)) {
        sample(cmdLine);
    } else if (cmdLine.hasOption(_oVisualize)) {
        visualize(cmdLine);
    } else {
        return false;
    }

    return true;
}

From source file:com.gsinnovations.howdah.AbstractJob.java

protected static void maybePut(Map<String, String> args, CommandLine cmdLine, Option... opt) {
    for (Option o : opt) {

        // the option appeared on the command-line, or it has a value
        // (which is likely a default value).
        if (cmdLine.hasOption(o) || cmdLine.getValue(o) != null) {

            // nulls are ok, for cases where options are simple flags.
            Object vo = cmdLine.getValue(o);
            String value = (vo == null) ? null : vo.toString();
            args.put(o.getPreferredName(), value);
        }/*from ww  w  .j  a va  2s  . c om*/
    }
}

From source file:com.elex.dmp.lda.InMemoryCollapsedVariationalBayes0.java

public static int main2(String[] args, Configuration conf) throws Exception {
    DefaultOptionBuilder obuilder = new DefaultOptionBuilder();
    ArgumentBuilder abuilder = new ArgumentBuilder();
    GroupBuilder gbuilder = new GroupBuilder();

    Option helpOpt = DefaultOptionCreator.helpOption();

    Option inputDirOpt = obuilder.withLongName("input").withRequired(true)
            .withArgument(abuilder.withName("input").withMinimum(1).withMaximum(1).create())
            .withDescription("The Directory on HDFS containing the collapsed, properly formatted files having "
                    + "one doc per line")
            .withShortName("i").create();

    Option dictOpt = obuilder.withLongName("dictionary").withRequired(false)
            .withArgument(abuilder.withName("dictionary").withMinimum(1).withMaximum(1).create())
            .withDescription("The path to the term-dictionary format is ... ").withShortName("d").create();

    Option dfsOpt = obuilder.withLongName("dfs").withRequired(false)
            .withArgument(abuilder.withName("dfs").withMinimum(1).withMaximum(1).create())
            .withDescription("HDFS namenode URI").withShortName("dfs").create();

    Option numTopicsOpt = obuilder.withLongName("numTopics").withRequired(true)
            .withArgument(abuilder.withName("numTopics").withMinimum(1).withMaximum(1).create())
            .withDescription("Number of topics to learn").withShortName("top").create();

    Option outputTopicFileOpt = obuilder.withLongName("topicOutputFile").withRequired(true)
            .withArgument(abuilder.withName("topicOutputFile").withMinimum(1).withMaximum(1).create())
            .withDescription("File to write out p(term | topic)").withShortName("to").create();

    Option outputDocFileOpt = obuilder.withLongName("docOutputFile").withRequired(true)
            .withArgument(abuilder.withName("docOutputFile").withMinimum(1).withMaximum(1).create())
            .withDescription("File to write out p(topic | docid)").withShortName("do").create();

    Option alphaOpt = obuilder.withLongName("alpha").withRequired(false)
            .withArgument(abuilder.withName("alpha").withMinimum(1).withMaximum(1).withDefault("0.1").create())
            .withDescription("Smoothing parameter for p(topic | document) prior").withShortName("a").create();

    Option etaOpt = obuilder.withLongName("eta").withRequired(false)
            .withArgument(abuilder.withName("eta").withMinimum(1).withMaximum(1).withDefault("0.1").create())
            .withDescription("Smoothing parameter for p(term | topic)").withShortName("e").create();

    Option maxIterOpt = obuilder.withLongName("maxIterations").withRequired(false)
            .withArgument(/*from   w w  w .  j  a  va  2  s .  c  om*/
                    abuilder.withName("maxIterations").withMinimum(1).withMaximum(1).withDefault(10).create())
            .withDescription("Maximum number of training passes").withShortName("m").create();

    Option modelCorpusFractionOption = obuilder.withLongName("modelCorpusFraction").withRequired(false)
            .withArgument(abuilder.withName("modelCorpusFraction").withMinimum(1).withMaximum(1)
                    .withDefault(0.0).create())
            .withShortName("mcf").withDescription("For online updates, initial value of |model|/|corpus|")
            .create();

    Option burnInOpt = obuilder.withLongName("burnInIterations").withRequired(false)
            .withArgument(
                    abuilder.withName("burnInIterations").withMinimum(1).withMaximum(1).withDefault(5).create())
            .withDescription("Minimum number of iterations").withShortName("b").create();

    Option convergenceOpt = obuilder.withLongName("convergence").withRequired(false)
            .withArgument(
                    abuilder.withName("convergence").withMinimum(1).withMaximum(1).withDefault("0.0").create())
            .withDescription("Fractional rate of perplexity to consider convergence").withShortName("c")
            .create();

    Option reInferDocTopicsOpt = obuilder.withLongName("reInferDocTopics").withRequired(false)
            .withArgument(abuilder.withName("reInferDocTopics").withMinimum(1).withMaximum(1).withDefault("no")
                    .create())
            .withDescription("re-infer p(topic | doc) : [no | randstart | continue]").withShortName("rdt")
            .create();

    Option numTrainThreadsOpt = obuilder
            .withLongName("numTrainThreads").withRequired(false).withArgument(abuilder
                    .withName("numTrainThreads").withMinimum(1).withMaximum(1).withDefault("1").create())
            .withDescription("number of threads to train with").withShortName("ntt").create();

    Option numUpdateThreadsOpt = obuilder.withLongName("numUpdateThreads").withRequired(false)
            .withArgument(abuilder.withName("numUpdateThreads").withMinimum(1).withMaximum(1).withDefault("1")
                    .create())
            .withDescription("number of threads to update the model with").withShortName("nut").create();

    Option verboseOpt = obuilder.withLongName("verbose").withRequired(false)
            .withArgument(
                    abuilder.withName("verbose").withMinimum(1).withMaximum(1).withDefault("false").create())
            .withDescription("print verbose information, like top-terms in each topic, during iteration")
            .withShortName("v").create();

    Group group = gbuilder.withName("Options").withOption(inputDirOpt).withOption(numTopicsOpt)
            .withOption(alphaOpt).withOption(etaOpt).withOption(maxIterOpt).withOption(burnInOpt)
            .withOption(convergenceOpt).withOption(dictOpt).withOption(reInferDocTopicsOpt)
            .withOption(outputDocFileOpt).withOption(outputTopicFileOpt).withOption(dfsOpt)
            .withOption(numTrainThreadsOpt).withOption(numUpdateThreadsOpt)
            .withOption(modelCorpusFractionOption).withOption(verboseOpt).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 -1;
        }

        String inputDirString = (String) cmdLine.getValue(inputDirOpt);
        String dictDirString = cmdLine.hasOption(dictOpt) ? (String) cmdLine.getValue(dictOpt) : null;
        int numTopics = Integer.parseInt((String) cmdLine.getValue(numTopicsOpt));
        double alpha = Double.parseDouble((String) cmdLine.getValue(alphaOpt));
        double eta = Double.parseDouble((String) cmdLine.getValue(etaOpt));
        int maxIterations = Integer.parseInt((String) cmdLine.getValue(maxIterOpt));
        int burnInIterations = (Integer) cmdLine.getValue(burnInOpt);
        double minFractionalErrorChange = Double.parseDouble((String) cmdLine.getValue(convergenceOpt));
        int numTrainThreads = Integer.parseInt((String) cmdLine.getValue(numTrainThreadsOpt));
        int numUpdateThreads = Integer.parseInt((String) cmdLine.getValue(numUpdateThreadsOpt));
        String topicOutFile = (String) cmdLine.getValue(outputTopicFileOpt);
        String docOutFile = (String) cmdLine.getValue(outputDocFileOpt);
        String reInferDocTopics = (String) cmdLine.getValue(reInferDocTopicsOpt);
        boolean verbose = Boolean.parseBoolean((String) cmdLine.getValue(verboseOpt));
        double modelCorpusFraction = (Double) cmdLine.getValue(modelCorpusFractionOption);

        long start = System.nanoTime();

        if (conf.get("fs.default.name") == null) {
            String dfsNameNode = (String) cmdLine.getValue(dfsOpt);
            conf.set("fs.default.name", dfsNameNode);
        }
        String[] terms = loadDictionary(dictDirString, conf);
        logTime("dictionary loading", System.nanoTime() - start);
        start = System.nanoTime();
        Matrix corpus = loadVectors(inputDirString, conf);
        logTime("vector seqfile corpus loading", System.nanoTime() - start);
        start = System.nanoTime();
        InMemoryCollapsedVariationalBayes0 cvb0 = new InMemoryCollapsedVariationalBayes0(corpus, terms,
                numTopics, alpha, eta, numTrainThreads, numUpdateThreads, modelCorpusFraction, 1234);
        logTime("cvb0 init", System.nanoTime() - start);

        start = System.nanoTime();
        cvb0.setVerbose(verbose);
        cvb0.iterateUntilConvergence(minFractionalErrorChange, maxIterations, burnInIterations);
        logTime("total training time", System.nanoTime() - start);

        if ("randstart".equalsIgnoreCase(reInferDocTopics)) {
            cvb0.inferDocuments(0.0, 100, true);
        } else if ("continue".equalsIgnoreCase(reInferDocTopics)) {
            cvb0.inferDocuments(0.0, 100, false);
        }

        start = System.nanoTime();
        cvb0.writeModel(new Path(topicOutFile));
        DistributedRowMatrixWriter.write(new Path(docOutFile), conf, cvb0.docTopicCounts);
        logTime("printTopics", System.nanoTime() - start);
    } catch (OptionException e) {
        log.error("Error while parsing options", e);
        CommandLineUtil.printHelp(group);
    }
    return 0;
}

From source file:com.martinkampjensen.thesis.Main.java

private static void minimize(CommandLine cmdLine) {
    final String fileOrClassName = (String) cmdLine.getValue(_oMinimize);
    final Model model = Util.instantiateModel(fileOrClassName);
    final boolean random = cmdLine.hasOption(_oMinimizeRandom);

    Application.minimize(model, random);
}

From source file:com.martinkampjensen.thesis.Main.java

private static void sample(CommandLine cmdLine) {
    final String fileOrClassName = (String) cmdLine.getValue(_oSample);
    final Model model = Util.instantiateModel(fileOrClassName);
    final int[] angleIds = parseIntArray(cmdLine, _oSampleAngleIds);
    final boolean random = cmdLine.hasOption(_oSampleRandom);

    Application.sample(model, angleIds, random);
}

From source file:com.ibm.jaql.util.shell.JaqlShellArguments.java

@SuppressWarnings("unchecked")
static JaqlShellArguments parseArgs(String... args) {
    // option builders
    final DefaultOptionBuilder obuilder = new DefaultOptionBuilder();
    final ArgumentBuilder abuilder = new ArgumentBuilder();
    final GroupBuilder gbuilder = new GroupBuilder();

    // create standard options
    Option optHelp = obuilder.withShortName("h").withShortName("?").withLongName("help")
            .withDescription("print this message").create();

    Option optJars = obuilder.withShortName("j").withLongName("jars")
            .withDescription(/*from  w  w  w .ja  v  a2s.  c  o m*/
                    "comma-separated list of jar files to include user defined expressions or data stores")
            .withArgument(abuilder.withName("args").withMinimum(1).withMaximum(1).create()).create();

    Option optSearchPath = obuilder.withShortName("jp").withLongName("jaql-path")
            .withDescription("colon seperated list of all search path entries")
            .withArgument(abuilder.withName("args").withMinimum(1).withMaximum(1).create()).create();

    Option optBatch = obuilder.withShortName("b").withLongName("batch")
            .withDescription("run in batch mode (i.e., do not read from stdin)").create();

    Option optOutOptions = obuilder.withShortName("o").withLongName("outoptions")
            .withDescription("output options: json, del and xml or an output IO descriptor. "
                    + "This option is ignored when not running in batch mode.")
            .withArgument(abuilder.withName("outoptions").withMinimum(1).withMaximum(1).create()).create();

    Option optEval = obuilder.withShortName("e").withLongName("eval")
            .withDescription("evaluate Jaql expression")
            .withArgument(abuilder.withName("expr").withMinimum(1).withMaximum(1).create()).create();

    // create mini-cluster options
    Option optCluster = obuilder.withShortName("c").withLongName("cluster")
            .withDescription("use existing cluster (i.e., do not launch a mini-cluster)").create();

    Option optNumNodes = obuilder.withShortName("n").withLongName("no-nodes")
            .withDescription("mini-cluster option: number of nodes to spawn")
            .withArgument(abuilder.withName("arg").withMinimum(1).withMaximum(1)
                    .withValidator(NumberValidator.getIntegerInstance()).create())
            .create();

    Option optDir = obuilder.withShortName("d").withLongName("hdfs-dir")
            .withDescription("mini-cluster option: root HDFs directory")
            .withArgument(abuilder.withName("arg").withMinimum(1).withMaximum(1).create()).create();

    Group clusterOptions = gbuilder.withName("Cluster options").withOption(optCluster).withOption(optDir)
            .withOption(optNumNodes).create();

    // create input files option
    Option optInputFiles = abuilder.withName("file").withDescription("list of input files").withMinimum(0)
            .create();

    Option optLog = obuilder.withShortName("l").withLongName("log")
            .withDescription("log options: json, del and xml or an output IO descriptor. ")
            .withArgument(abuilder.withName("arg").withMinimum(1).withMaximum(1).create()).create();

    // combine all options
    Group options = gbuilder.withName("options").withOption(optHelp).withOption(optJars)
            .withOption(optSearchPath).withOption(optBatch).withOption(optLog).withOption(optOutOptions)
            .withOption(optEval).withOption(optInputFiles).withOption(clusterOptions).create();

    // parse and print help if necessary
    CommandLine cl;
    try {
        Parser parser = new Parser();
        parser.setGroup(options);
        cl = parser.parse(args);
    } catch (OptionException e) {
        printHelpAndExit(e, null, options);
        return null;
    }
    if (cl.hasOption(optHelp)) {
        printHelpAndExit(null, options);
    }

    // validate arguments
    JaqlShellArguments result = new JaqlShellArguments();

    // mini-cluster options         
    if (cl.hasOption(optCluster)) {
        result.useExistingCluster = true;
    }
    if (cl.hasOption(optDir)) {
        if (result.useExistingCluster) {
            printHelpAndExit("Options " + optCluster.getPreferredName() + " and " + optDir.getPreferredName()
                    + " are mutually exclusive", options);
        }
        result.hdfsDir = (String) cl.getValue(optDir);
    }
    if (cl.hasOption(optNumNodes)) {
        if (result.useExistingCluster) {
            printHelpAndExit("Options " + optCluster.getPreferredName() + " and "
                    + optNumNodes.getPreferredName() + " are mutually exclusive", options);
        }
        result.numNodes = ((Number) cl.getValue(optNumNodes)).intValue();
    }

    // jar files
    if (cl.hasOption(optJars)) {
        result.jars = ((String) cl.getValue(optJars)).split(",");
        for (String jar : result.jars) {
            if (!new File(jar).exists()) {
                printHelpAndExit("Jar file " + jar + " not found", options);
            }
        }
    }

    // search path directories
    if (cl.hasOption(optSearchPath)) {
        result.searchPath = ((String) cl.getValue(optSearchPath)).split(":");
        for (String dir : result.searchPath) {
            if (!new File(dir).exists() || !new File(dir).isDirectory()) {
                printHelpAndExit("Search-path entry " + dir + " not found or is no directory", options);
            }
        }
    }

    if (cl.hasOption(optBatch)) {
        result.batchMode = true;
        if (cl.hasOption(optOutOptions)) {
            String format = (String) cl.getValue(optOutOptions);
            try {
                result.outputAdapter = getOutputAdapter(format);
            } catch (Exception e) {
                printHelpAndExit(e,
                        "\"" + format + "\" is neither a valid output format nor a valid IO descriptor",
                        options);
            }
        }
    }

    // input
    if (cl.hasOption(optEval)) {
        String eval = (String) cl.getValue(optEval);
        if (!eval.endsWith(";"))
            eval += ";";
        result.addInputStream(new ByteArrayInputStream(eval.getBytes()));
    }
    if (cl.hasOption(optInputFiles)) {
        List<String> files = (List<String>) cl.getValues(optInputFiles);
        for (String file : files) {
            try {
                result.addInputStream(new FileInputStream(file));
            } catch (FileNotFoundException e) {
                printHelpAndExit(e, "Input file " + file + " not found", options);
            }
        }
    }

    // error log
    if (cl.hasOption(optLog)) {
        String path = (String) cl.getValue(optLog);
        try {
            BufferedJsonRecord logFD = new BufferedJsonRecord();
            logFD.add(Adapter.TYPE_NAME, new JsonString("local"));
            logFD.add(Adapter.LOCATION_NAME, new JsonString(path));
            OutputAdapter oa = (OutputAdapter) JaqlUtil.getAdapterStore().output.getAdapter(logFD);
            result.logAdapter = oa;
        } catch (Exception e) {
            printHelpAndExit(e, "\"" + path + "\" invalid", options);
        }
    }

    if (!result.batchMode) {
        result.addStdin();
    }

    return result;
}

From source file:my.mahout.AbstractJob.java

protected static void maybePut(Map<String, List<String>> args, CommandLine cmdLine, Option... opt) {
    for (Option o : opt) {

        // the option appeared on the command-line, or it has a value
        // (which is likely a default value). 
        if (cmdLine.hasOption(o) || cmdLine.getValue(o) != null
                || (cmdLine.getValues(o) != null && !cmdLine.getValues(o).isEmpty())) {

            // nulls are ok, for cases where options are simple flags.
            List<?> vo = cmdLine.getValues(o);
            if (vo != null && !vo.isEmpty()) {
                List<String> vals = Lists.newArrayList();
                for (Object o1 : vo) {
                    vals.add(o1.toString());
                }/*w ww  .  j a va2 s .c o m*/
                args.put(o.getPreferredName(), vals);
            } else {
                args.put(o.getPreferredName(), null);
            }
        }
    }
}

From source file:com.martinkampjensen.thesis.Main.java

private static void barriers(CommandLine cmdLine) {
    final String fileOrClassName = (String) cmdLine.getValue(_oBarriers);
    final String postScript = (String) cmdLine.getValue(_oBarriersPostScript);
    final String structure = (String) cmdLine.getValue(_oBarriersStructure);
    final File moleculeFile = new File(fileOrClassName);
    final File postScriptFile = (postScript == null ? null : new File(postScript));
    final File structureFile = (structure == null ? null : new File(structure));

    if (cmdLine.hasOption(_oBarriersTrajectory)) {
        final String trajectory = (String) cmdLine.getValue(_oBarriersTrajectory);
        final String energy = (String) cmdLine.getValue(_oBarriersEnergy);
        final File trajectoryFile = new File(trajectory);
        final File energyFile = new File(energy);
        final double pruning = parseDouble(cmdLine, _oBarriersPruning);
        final double neighbor = parseDouble(cmdLine, _oBarriersNeighbor);
        Application.barriers(moleculeFile, trajectoryFile, energyFile, postScriptFile, structureFile, pruning,
                neighbor);//from   w  ww  . ja  v  a  2  s  .  co  m
    } else {
        final String topology = (String) cmdLine.getValue(_oBarriersTopology);
        final File topologyFile = (topology == null ? null : new File(topology));
        final Model model = Util.instantiateModel(fileOrClassName, topologyFile);
        Application.barriers(moleculeFile, model, postScriptFile, structureFile);
    }
}

From source file:egat.cli.minform.MinFormCommandHandler.java

@Override
protected void handleAdditionalChildOptions(CommandLine commandLine) throws CommandProcessingException {
    all = commandLine.hasOption(allOption);
}

From source file:egat.cli.ieds.IEDSCommandHandler.java

@Override
protected void handleAdditionalChildOptions(CommandLine commandLine) throws CommandProcessingException {
    pure = commandLine.hasOption(pureOption);
}