List of usage examples for org.apache.commons.cli GnuParser GnuParser
GnuParser
From source file:fr.ens.biologie.genomique.eoulsan.actions.ClusterTaskAction.java
@Override public void action(final List<String> arguments) { final Options options = makeOptions(); final CommandLineParser parser = new GnuParser(); int argsOptions = 0; try {/*from w w w . j av a 2 s .com*/ // parse the command line arguments final CommandLine line = parser.parse(options, arguments.toArray(new String[arguments.size()]), true); // Help option if (line.hasOption("help")) { help(options); } } catch (ParseException e) { Common.errorExit(e, "Error while parsing parameter file: " + e.getMessage()); } if (arguments.size() != argsOptions + 1) { help(options); } final DataFile contextFile = new DataFile(arguments.get(0)); // Execute task run(contextFile); }
From source file:com.digitalpebble.behemoth.util.CorpusFilter.java
public int run(String[] args) throws Exception { Options options = new Options(); // automatically generate the help statement HelpFormatter formatter = new HelpFormatter(); // create the parser CommandLineParser parser = new GnuParser(); options.addOption("h", "help", false, "print this message"); options.addOption("i", "input", true, "input Behemoth corpus"); options.addOption("o", "output", true, "output Behemoth corpus"); // parse the command line arguments CommandLine line = null;/*from w ww. j av a 2s . c o m*/ try { line = parser.parse(options, args); String input = line.getOptionValue("i"); String output = line.getOptionValue("o"); if (line.hasOption("help")) { formatter.printHelp("CorpusFilter", options); return 0; } if (input == null | output == null) { formatter.printHelp("CorpusFilter", options); return -1; } } catch (ParseException e) { formatter.printHelp("CorpusFilter", options); } final FileSystem fs = FileSystem.get(getConf()); Path inputPath = new Path(line.getOptionValue("i")); Path outputPath = new Path(line.getOptionValue("o")); JobConf job = new JobConf(getConf()); job.setJarByClass(this.getClass()); job.setJobName("CorpusFilter : " + 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); boolean isFilterRequired = BehemothMapper.isRequired(job); // should be the case here if (!isFilterRequired) { System.err.println("No filters configured. Check your behemoth-site.xml"); return -1; } job.setMapperClass(BehemothMapper.class); job.setNumReduceTasks(0); FileInputFormat.addInputPath(job, inputPath); FileOutputFormat.setOutputPath(job, outputPath); try { JobClient.runJob(job); } catch (Exception e) { e.printStackTrace(); fs.delete(outputPath, true); } finally { } return 0; }
From source file:TikaServiceWinRun4J.java
public int serviceMain(String[] args) { Properties properties = new Properties(); try {/*from w w w .ja v a 2 s . c om*/ properties.load(ClassLoader.getSystemResourceAsStream("tikaserver-version.properties")); } catch (IOException e) { throw new RuntimeException(e); } logger.info("Starting Tika server " + properties.getProperty("tikaserver.version")); try { Options options = getOptions(); CommandLineParser cliParser = new GnuParser(); CommandLine line = cliParser.parse(options, args); int port = DEFAULT_PORT; if (line.hasOption("port")) { port = Integer.valueOf(line.getOptionValue("port")); } if (line.hasOption("help")) { HelpFormatter helpFormatter = new HelpFormatter(); helpFormatter.printHelp("tikaserver", options); System.exit(-1); } server = new Server(port); ServletContextHandler context = new ServletContextHandler(ServletContextHandler.NO_SESSIONS); context.setContextPath("/"); server.setHandler(context); context.addServlet( new ServletHolder(new ServletContainer(new PackagesResourceConfig("org.apache.tika.server"))), "/*"); server.start(); logger.info("Started"); server.join(); return 0; } catch (Exception ex) { logger.fatal("Can't start", ex); return 1; } }
From source file:edu.umd.shrawanraina.HBaseWordCountFetch.java
/** * Runs this tool.// ww w . ja v a 2 s. co m */ @SuppressWarnings({ "static-access" }) public int run(String[] args) throws Exception { Options options = new Options(); options.addOption( OptionBuilder.withArgName("table").hasArg().withDescription("HBase table name").create(TABLE)); options.addOption( OptionBuilder.withArgName("word").hasArg().withDescription("word to look up").create(WORD)); CommandLine cmdline; CommandLineParser parser = new GnuParser(); try { cmdline = parser.parse(options, args); } catch (ParseException exp) { System.err.println("Error parsing command line: " + exp.getMessage()); return -1; } if (!cmdline.hasOption(TABLE) || !cmdline.hasOption(WORD)) { System.out.println("args: " + Arrays.toString(args)); HelpFormatter formatter = new HelpFormatter(); formatter.setWidth(120); formatter.printHelp(this.getClass().getName(), options); ToolRunner.printGenericCommandUsage(System.out); return -1; } String tableName = cmdline.getOptionValue(TABLE); String word = cmdline.getOptionValue(WORD); Configuration conf = getConf(); conf.addResource(new Path("/etc/hbase/conf/hbase-site.xml")); Configuration hbaseConfig = HBaseConfiguration.create(conf); HConnection hbaseConnection = HConnectionManager.createConnection(hbaseConfig); HTableInterface table = hbaseConnection.getTable(tableName); Get get = new Get(Bytes.toBytes(word)); Result result = table.get(get); int count = Bytes.toInt(result.getValue(HBaseWordCount.CF, HBaseWordCount.COUNT)); LOG.info("word: " + word + ", count: " + count); LOG.info("word: " + word + ", result: " + result.getValue(HBaseWordCount.CF, HBaseWordCount.COUNT)); return 0; }
From source file:edu.umd.cloud9.example.hbase.HBaseWordCountFetch.java
/** * Runs this tool.// w w w .j a v a 2s . c om */ @SuppressWarnings({ "static-access" }) public int run(String[] args) throws Exception { Options options = new Options(); options.addOption( OptionBuilder.withArgName("table").hasArg().withDescription("HBase table name").create(TABLE)); options.addOption( OptionBuilder.withArgName("word").hasArg().withDescription("word to look up").create(WORD)); CommandLine cmdline; CommandLineParser parser = new GnuParser(); try { cmdline = parser.parse(options, args); } catch (ParseException exp) { System.err.println("Error parsing command line: " + exp.getMessage()); return -1; } if (!cmdline.hasOption(TABLE) || !cmdline.hasOption(WORD)) { System.out.println("args: " + Arrays.toString(args)); HelpFormatter formatter = new HelpFormatter(); formatter.setWidth(120); formatter.printHelp(this.getClass().getName(), options); ToolRunner.printGenericCommandUsage(System.out); return -1; } String tableName = cmdline.getOptionValue(TABLE); String word = cmdline.getOptionValue(WORD); Configuration conf = getConf(); conf.addResource(new Path("/etc/hbase/conf/hbase-site.xml")); Configuration hbaseConfig = HBaseConfiguration.create(conf); HConnection hbaseConnection = HConnectionManager.createConnection(hbaseConfig); HTableInterface table = hbaseConnection.getTable(tableName); Get get = new Get(Bytes.toBytes(word)); Result result = table.get(get); int count = Bytes.toInt(result.getValue(HBaseWordCount.CF, HBaseWordCount.COUNT)); LOG.info("word: " + word + ", count: " + count); return 0; }
From source file:com.zimbra.cs.mailclient.smtp.SmtpClient.java
@Override protected void parseArguments(String[] args, MailConfig config) { CommandLineParser parser = new GnuParser(); CommandLine cl = null;/*from w w w . ja v a2s. co m*/ try { cl = parser.parse(options, args); } catch (ParseException e) { System.err.println(e.getMessage()); System.exit(1); } if (cl.hasOption('p')) { config.setPort(Integer.parseInt(cl.getOptionValue('p'))); } else { config.setPort(SmtpConfig.DEFAULT_PORT); } if (cl.hasOption('m')) { config.setMechanism(cl.getOptionValue('m').toUpperCase()); } if (cl.hasOption('u')) { config.setAuthenticationId(cl.getOptionValue('u')); } if (cl.hasOption('w')) { setPassword(cl.getOptionValue('w')); } if (cl.hasOption('r')) { config.setRealm(cl.getOptionValue('r')); } if (cl.hasOption('s')) { config.setSecurity(MailConfig.Security.SSL); } if (cl.hasOption('d')) { config.getLogger().setLevel(Log.Level.trace); } if (cl.hasOption('h')) { usage(); System.exit(0); } String[] remaining = cl.getArgs(); config.setHost(remaining.length > 0 ? remaining[0] : "localhost"); }
From source file:LookupPostings.java
/** * Runs this tool./*w w w.j av a2s .c o m*/ */ @SuppressWarnings({ "static-access" }) public int run(String[] args) throws Exception { Options options = new Options(); options.addOption(OptionBuilder.withArgName("path").hasArg().withDescription("input path").create(INDEX)); options.addOption( OptionBuilder.withArgName("path").hasArg().withDescription("output path").create(COLLECTION)); CommandLine cmdline = null; CommandLineParser parser = new GnuParser(); try { cmdline = parser.parse(options, args); } catch (ParseException exp) { System.err.println("Error parsing command line: " + exp.getMessage()); System.exit(-1); } if (!cmdline.hasOption(INDEX) || !cmdline.hasOption(COLLECTION)) { System.out.println("args: " + Arrays.toString(args)); HelpFormatter formatter = new HelpFormatter(); formatter.setWidth(120); formatter.printHelp(LookupPostings.class.getName(), options); ToolRunner.printGenericCommandUsage(System.out); System.exit(-1); } String indexPath = cmdline.getOptionValue(INDEX); String collectionPath = cmdline.getOptionValue(COLLECTION); if (collectionPath.endsWith(".gz")) { System.out.println("gzipped collection is not seekable: use compressed version!"); System.exit(-1); } Configuration config = new Configuration(); FileSystem fs = FileSystem.get(config); MapFile.Reader reader = new MapFile.Reader(new Path(indexPath + "/part-r-00000"), config); FSDataInputStream collection = fs.open(new Path(collectionPath)); BufferedReader d = new BufferedReader(new InputStreamReader(collection)); Text key = new Text(); PairOfWritables<IntWritable, ArrayListWritable<PairOfInts>> value = new PairOfWritables<IntWritable, ArrayListWritable<PairOfInts>>(); System.out.println("Looking up postings for the term \"starcross'd\""); key.set("starcross'd"); reader.get(key, value); ArrayListWritable<PairOfInts> postings = value.getRightElement(); for (PairOfInts pair : postings) { System.out.println(pair); collection.seek(pair.getLeftElement()); System.out.println(d.readLine()); } key.set("gold"); reader.get(key, value); System.out.println("Complete postings list for 'gold': " + value); Int2IntFrequencyDistribution goldHist = new Int2IntFrequencyDistributionEntry(); postings = value.getRightElement(); for (PairOfInts pair : postings) { goldHist.increment(pair.getRightElement()); } System.out.println("histogram of tf values for gold"); for (PairOfInts pair : goldHist) { System.out.println(pair.getLeftElement() + "\t" + pair.getRightElement()); } key.set("silver"); reader.get(key, value); System.out.println("Complete postings list for 'silver': " + value); Int2IntFrequencyDistribution silverHist = new Int2IntFrequencyDistributionEntry(); postings = value.getRightElement(); for (PairOfInts pair : postings) { silverHist.increment(pair.getRightElement()); } System.out.println("histogram of tf values for silver"); for (PairOfInts pair : silverHist) { System.out.println(pair.getLeftElement() + "\t" + pair.getRightElement()); } key.set("bronze"); Writable w = reader.get(key, value); if (w == null) { System.out.println("the term bronze does not appear in the collection"); } collection.close(); reader.close(); return 0; }
From source file:com.github.hdl.tensorflow.yarn.app.TFServerLauncher.java
public boolean init(String[] args) throws ParseException, IOException { CommandLine cliParser = new GnuParser().parse(opts, args); if (args.length == 0) { throw new IllegalArgumentException("No args specified for tf server to initialize"); }/* w w w . ja v a 2 s.c o m*/ if (!cliParser.hasOption(OPT_CS) || !cliParser.hasOption(OPT_JN) || !cliParser.hasOption(OPT_TI)) { LOG.error("invalid args for tf server!"); return false; } clusterSpecString = ClusterSpec.decodeJsonString(cliParser.getOptionValue(OPT_CS)); jobName = cliParser.getOptionValue(OPT_JN); taskIndex = Integer.parseInt(cliParser.getOptionValue(OPT_TI)); LOG.info("cs: " + clusterSpecString + "; + jn: " + jobName + "; ti: " + taskIndex); cluster = ClusterSpec.toClusterMapFromJsonString(clusterSpecString); return true; }
From source file:com.strider.appinv.agent.Scanner.java
/** * Parses command line arguments/* www.j a v a2s . com*/ * * @param options * @param args * @return CommandLine * @throws ParseException */ private static CommandLine getCommandLine(final Options options, final String[] args) throws ParseException { final CommandLineParser parser = new GnuParser(); CommandLine line = null; try { line = parser.parse(options, args); } catch (ParseException e) { help(options); } return line; }
From source file:fr.ens.biologie.genomique.eoulsan.actions.HadoopExecAction.java
@Override public void action(final List<String> arguments) { final Options options = makeOptions(); final CommandLineParser parser = new GnuParser(); String jobDescription = null; int argsOptions = 0; try {//from w w w. j av a 2 s .c o m // parse the command line arguments final CommandLine line = parser.parse(options, arguments.toArray(new String[arguments.size()]), true); // Help option if (line.hasOption("help")) { help(options); } if (line.hasOption("d")) { jobDescription = line.getOptionValue("d"); argsOptions += 2; } } catch (ParseException e) { Common.errorExit(e, "Error while parsing command line arguments: " + e.getMessage()); } if (arguments.size() != argsOptions + 3) { help(options); } final File paramFile = new File(arguments.get(argsOptions)); final File designFile = new File(arguments.get(argsOptions + 1)); final String hdfsPath = arguments.get(argsOptions + 2); // Execute program in hadoop mode run(paramFile, designFile, hdfsPath, jobDescription); }