List of usage examples for org.apache.commons.cli2.commandline Parser setGroup
public void setGroup(final Group group)
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(// w w w. ja v a2 s.c o m 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:it.jnrpe.plugins.TestCommandLineParsing.java
@Test public void testNoArgumentsOption() throws Exception { ClassLoader cl = TestCommandLineParsing.class.getClassLoader(); PluginDefinition pluginDef = PluginRepositoryUtil.parseXmlPluginDefinition(cl, cl.getResourceAsStream("check_mysql_plugin.xml")); GroupBuilder gBuilder = new GroupBuilder(); for (PluginOption po : pluginDef.getOptions()) { gBuilder = gBuilder.withOption(po.toOption()); }/*from w w w. j a v a 2 s . com*/ Group group = gBuilder.create(); Parser p = new Parser(); p.setGroup(group); CommandLine cli = p.parse(new String[] { "--hostname", "$ARG1$", "--port", "$ARG2$", "--database", "$ARG3$", "--user", "$ARG4$", "--password", "$ARG5$", "--check-slave" }); Assert.assertTrue(cli.hasOption("--check-slave")); }
From source file:broadwick.CliOptions.java
/** * Construct and provide GNU-compatible Options. Read the command line extracting the arguments, this * additionally displays the help message if the command line is empty. * * @param args the command line arguments. *//*from w w w . j a v a2 s. co m*/ public CliOptions(final String[] args) { buildCommandLineArguments(); final Parser parser = new Parser(); parser.setGroup(options); final HelpFormatter hf = new HelpFormatter(SPACE, SPACE, SPACE, LINEWIDTH); parser.setHelpFormatter(hf); parser.setHelpTrigger("--help"); cmdLine = parser.parseAndHelp(args); if (cmdLine == null) { hf.printHeader(); throw new BroadwickException("Empty command line."); } validateCommandLineArguments(); }
From source file:it.jnrpe.server.console.PluginCommand.java
public boolean execute(final String[] args) throws Exception { Parser p = new Parser(); p.setGroup(getCommandLineGroup()); try {/* w w w .ja va 2 s.co m*/ p.parse(args); } catch (Exception e) { getConsole().println(); // getConsole().println("\u001B[1mERROR:\u001B[0m " + // e.getMessage()); getConsole().println(highlight("ERROR: ") + e.getMessage()); getConsole().println(); printHelp(); return false; } PluginProxy plugin = (PluginProxy) pluginRepository.getPlugin(pluginName); InjectionUtils.inject(plugin, context); //plugin.setContext(context); ReturnValue retVal = plugin.execute(args); getConsole().println(retVal.getMessage()); return false; }
From source file:com.memonews.mahout.sentiment.SentimentModelTester.java
boolean parseArgs(final String[] args) { final DefaultOptionBuilder builder = new DefaultOptionBuilder(); final Option help = builder.withLongName("help").withDescription("print this list").create(); final ArgumentBuilder argumentBuilder = new ArgumentBuilder(); final Option inputFileOption = builder.withLongName("input").withRequired(true) .withArgument(argumentBuilder.withName("input").withMaximum(1).create()) .withDescription("where to get training data").create(); final Option modelFileOption = builder.withLongName("model").withRequired(true) .withArgument(argumentBuilder.withName("model").withMaximum(1).create()) .withDescription("where to get a model").create(); final Group normalArgs = new GroupBuilder().withOption(help).withOption(inputFileOption) .withOption(modelFileOption).create(); final Parser parser = new Parser(); parser.setHelpOption(help);/*from ww w . j a v a 2 s.c o m*/ parser.setHelpTrigger("--help"); parser.setGroup(normalArgs); parser.setHelpFormatter(new HelpFormatter(" ", "", " ", 130)); final CommandLine cmdLine = parser.parseAndHelp(args); if (cmdLine == null) { return false; } inputFile = (String) cmdLine.getValue(inputFileOption); modelFile = (String) cmdLine.getValue(modelFileOption); return true; }
From source file:com.tamingtext.tagrecommender.CountStackOverflowTags.java
public boolean parseArgs(String[] args) { DefaultOptionBuilder obuilder = new DefaultOptionBuilder(); ArgumentBuilder abuilder = new ArgumentBuilder(); GroupBuilder gbuilder = new GroupBuilder(); Option helpOpt = DefaultOptionCreator.helpOption(); Option inputFileOpt = obuilder.withLongName("inputFile").withRequired(true) .withArgument(abuilder.withName("inputFile").withMinimum(1).withMaximum(1).create()) .withDescription("The input file").withShortName("i").create(); Option outputFileOpt = obuilder.withLongName("outputFile").withRequired(true) .withArgument(abuilder.withName("outputFile").withMinimum(1).withMaximum(1).create()) .withDescription("The output file").withShortName("o").create(); Option limitOpt = obuilder.withLongName("limit").withRequired(false) .withArgument(abuilder.withName("limit").withMinimum(1).withMaximum(1).create()) .withDescription("Emit this many of the most frequent tags").withShortName("l").create(); Option cutoffOpt = obuilder.withLongName("cutoff").withRequired(false) .withArgument(abuilder.withName("cutoff").withMinimum(1).withMaximum(1).create()) .withDescription("Drop tags with a count less than this number").withShortName("c").create(); Group group = gbuilder.withName("Options").withOption(inputFileOpt).withOption(outputFileOpt) .withOption(limitOpt).withOption(cutoffOpt).create(); try {/* w w w .j a va 2 s.c om*/ Parser parser = new Parser(); parser.setGroup(group); CommandLine cmdLine = parser.parse(args); if (cmdLine.hasOption(helpOpt)) { CommandLineUtil.printHelp(group); return false; } inputFile = new File((String) cmdLine.getValue(inputFileOpt)); countFile = new File((String) cmdLine.getValue(outputFileOpt)); if (cmdLine.hasOption(limitOpt)) { limit = Integer.parseInt((String) cmdLine.getValue(limitOpt)); } if (cmdLine.hasOption(cutoffOpt)) { cutoff = Integer.parseInt((String) cmdLine.getValue(cutoffOpt)); } } catch (OptionException e) { log.error("Command-line option Exception", e); CommandLineUtil.printHelp(group); return false; } validate(); return true; }
From source file:com.digitalpebble.behemoth.tika.TikaDriver.java
public int run(String[] args) throws Exception { final FileSystem fs = FileSystem.get(getConf()); GroupBuilder gBuilder = new GroupBuilder().withName("Options:"); List<Option> options = new ArrayList<Option>(); Option inputOpt = buildOption("input", "i", "The input path", true, true, null); options.add(inputOpt);//w ww. ja v a 2s . co m Option outOpt = buildOption("output", "o", "The output path", true, true, null); options.add(outOpt); Option tikaOpt = buildOption("tikaProcessor", "t", "The fully qualified name of a TikaProcessor class that handles the extraction (optional)", true, false, null); options.add(tikaOpt); Option mimeTypeOpt = buildOption("mimeType", "m", "The mime type to use (optional)", true, false, ""); options.add(mimeTypeOpt); for (Option opt : options) { gBuilder = gBuilder.withOption(opt); } Group group = gBuilder.create(); try { Parser parser = new Parser(); parser.setGroup(group); // TODO catch exceptions with parsing of opts CommandLine cmdLine = parser.parse(args); Path inputPath = new Path(cmdLine.getValue(inputOpt).toString()); Path outputPath = new Path(cmdLine.getValue(outOpt).toString()); String handlerName = null; if (cmdLine.hasOption(tikaOpt)) { handlerName = cmdLine.getValue(tikaOpt).toString(); } JobConf job = new JobConf(getConf()); job.setJarByClass(this.getClass()); if (cmdLine.hasOption(mimeTypeOpt)) { String mimeType = cmdLine.getValue(mimeTypeOpt).toString(); job.set(TikaConstants.TIKA_MIME_TYPE_KEY, mimeType); } if (handlerName != null && handlerName.equals("") == false) { job.set(TIKA_PROCESSOR_KEY, handlerName); } job.setJobName("Tika : " + inputPath.toString()); job.setInputFormat(SequenceFileInputFormat.class); job.setOutputFormat(SequenceFileOutputFormat.class); job.setMapOutputKeyClass(Text.class); job.setMapOutputValueClass(BehemothDocument.class); job.setOutputKeyClass(Text.class); job.setOutputValueClass(BehemothDocument.class); job.setMapperClass(TikaMapper.class); boolean isFilterRequired = BehemothReducer.isRequired(job); if (isFilterRequired) job.setReducerClass(BehemothReducer.class); else { job.setNumReduceTasks(0); } FileInputFormat.addInputPath(job, inputPath); FileOutputFormat.setOutputPath(job, outputPath); try { long start = System.currentTimeMillis(); JobClient.runJob(job); long finish = System.currentTimeMillis(); if (log.isInfoEnabled()) { log.info("TikaDriver completed. Timing: " + (finish - start) + " ms"); } } catch (Exception e) { log.error("Exception", e); return -1; // don't delete the output as some of it could be used // fs.delete(outputPath, true); } finally { } } catch (OptionException e) { log.error("OptionException", e.getMessage()); HelpFormatter formatter = new HelpFormatter(); formatter.setGroup(group); formatter.print(); return -1; } return 0; }
From source file:com.tamingtext.tagrecommender.TestStackOverflowTagger.java
public boolean parseArgs(String[] args) { DefaultOptionBuilder obuilder = new DefaultOptionBuilder(); ArgumentBuilder abuilder = new ArgumentBuilder(); GroupBuilder gbuilder = new GroupBuilder(); Option helpOpt = DefaultOptionCreator.helpOption(); Option inputFileOpt = obuilder.withLongName("inputFile").withRequired(true) .withArgument(abuilder.withName("inputFile").withMinimum(1).withMaximum(1).create()) .withDescription("The input file").withShortName("i").create(); Option countFileOpt = obuilder.withLongName("countFile").withRequired(true) .withArgument(abuilder.withName("countFile").withMinimum(1).withMaximum(1).create()) .withDescription("The tag count file").withShortName("c").create(); Option outputFileOpt = obuilder.withLongName("outputFile").withRequired(true) .withArgument(abuilder.withName("outputFile").withMinimum(1).withMaximum(1).create()) .withDescription("The output file").withShortName("c").create(); Option solrUrlOpt = obuilder.withLongName("solrUrl").withRequired(true) .withArgument(abuilder.withName("solrUrl").withMinimum(1).withMaximum(1).create()) .withDescription("URL of the solr server").withShortName("s").create(); Group group = gbuilder.withName("Options").withOption(inputFileOpt).withOption(countFileOpt) .withOption(outputFileOpt).withOption(solrUrlOpt).create(); try {// w w w. java 2s .com Parser parser = new Parser(); parser.setGroup(group); CommandLine cmdLine = parser.parse(args); if (cmdLine.hasOption(helpOpt)) { CommandLineUtil.printHelp(group); return false; } inputFile = new File((String) cmdLine.getValue(inputFileOpt)); countFile = new File((String) cmdLine.getValue(countFileOpt)); outputFile = new File((String) cmdLine.getValue(outputFileOpt)); solrUrl = (String) cmdLine.getValue(solrUrlOpt); client = new TagRecommenderClient(solrUrl); } catch (OptionException e) { log.error("Command-line option Exception", e); CommandLineUtil.printHelp(group); return false; } catch (MalformedURLException e) { log.error("MalformedURLException", e); return false; } validate(); return true; }
From source file:it.jnrpe.plugins.PluginProxy.java
/** * Executes the proxied plugin passing the received arguments as parameters. * //from w w w . j a v a 2 s. c o m * @param argsAry * The parameters to be passed to the plugin * @return The return value of the plugin. * @throws BadThresholdException * - */ public ReturnValue execute(final String[] argsAry) throws BadThresholdException { // CommandLineParser clp = new PosixParser(); try { HelpFormatter hf = new HelpFormatter(); // configure a parser Parser cliParser = new Parser(); cliParser.setGroup(mainOptionsGroup); cliParser.setHelpFormatter(hf); CommandLine cl = cliParser.parse(argsAry); // Inject the context... InjectionUtils.inject(proxiedPlugin, getContext()); Thread.currentThread().setContextClassLoader(proxiedPlugin.getClass().getClassLoader()); ReturnValue retValue = proxiedPlugin.execute(new PluginCommandLine(cl)); if (retValue == null) { String msg = "Plugin [" + getPluginName() + "] with args [" + StringUtils.join(argsAry) + "] returned null"; retValue = new ReturnValue(Status.UNKNOWN, msg); } return retValue; } catch (BadThresholdException bte) { throw bte; } catch (OptionException e) { String msg = e.getMessage(); LOG.error(getContext(), "Error parsing plugin '" + getPluginName() + "' command line : " + msg, e); return new ReturnValue(Status.UNKNOWN, msg); } }
From source file:com.tamingtext.tagrecommender.ExtractStackOverflowData.java
public boolean parseArgs(String[] args) { DefaultOptionBuilder obuilder = new DefaultOptionBuilder(); ArgumentBuilder abuilder = new ArgumentBuilder(); GroupBuilder gbuilder = new GroupBuilder(); Option helpOpt = DefaultOptionCreator.helpOption(); Option inputFileOpt = obuilder.withLongName("inputFile").withRequired(true) .withArgument(abuilder.withName("inputFile").withMinimum(1).withMaximum(1).create()) .withDescription("The input file").withShortName("i").create(); Option trainingOutputOpt = obuilder.withLongName("trainingOutputFile").withRequired(true) .withArgument(abuilder.withName("trainingOutputFile").withMinimum(1).withMaximum(1).create()) .withDescription("The training data output file").withShortName("tr").create(); Option testOutputOpt = obuilder.withLongName("testOutputFile").withRequired(true) .withArgument(abuilder.withName("testOutputFile").withMinimum(1).withMaximum(1).create()) .withDescription("The test data output file").withShortName("te").create(); Option trainingDataSizeOpt = obuilder.withLongName("trainingDataSize").withRequired(false) .withArgument(abuilder.withName("trainingDataSize").withMinimum(1).withMaximum(1).create()) .withDescription("The number of questions to extract for training data").withShortName("trs") .create();/* www. j a v a 2 s .c o m*/ Option testDataSizeOpt = obuilder.withLongName("testDataSize").withRequired(false) .withArgument(abuilder.withName("testDataSize").withMinimum(1).withMaximum(1).create()) .withDescription("The number of questions to extract for training data").withShortName("tes") .create(); Group group = gbuilder.withName("Options").withOption(inputFileOpt).withOption(trainingOutputOpt) .withOption(testOutputOpt).withOption(trainingDataSizeOpt).withOption(testDataSizeOpt).create(); try { Parser parser = new Parser(); parser.setGroup(group); CommandLine cmdLine = parser.parse(args); if (cmdLine.hasOption(helpOpt)) { CommandLineUtil.printHelp(group); return false; } inputFile = new File((String) cmdLine.getValue(inputFileOpt)); trainingOutputFile = new File((String) cmdLine.getValue(trainingOutputOpt)); testOutputFile = new File((String) cmdLine.getValue(testOutputOpt)); if (cmdLine.hasOption(trainingDataSizeOpt)) { trainingDataSize = Integer.parseInt((String) cmdLine.getValue(trainingDataSizeOpt)); } if (cmdLine.hasOption(testDataSizeOpt)) { testDataSize = Integer.parseInt((String) cmdLine.getValue(testDataSizeOpt)); } } catch (OptionException e) { log.error("Command-line option Exception", e); CommandLineUtil.printHelp(group); return false; } validate(); return true; }