List of usage examples for org.apache.commons.cli Options Options
Options
From source file:com.aerospike.examples.pk.StorePrimaryKey.java
public static void main(String[] args) throws AerospikeException { try {/*from ww w . ja v a2 s. co m*/ Options options = new Options(); options.addOption("h", "host", true, "Server hostname (default: 172.28.128.6)"); options.addOption("p", "port", true, "Server port (default: 3000)"); options.addOption("n", "namespace", true, "Namespace (default: test)"); options.addOption("s", "set", true, "Set (default: demo)"); options.addOption("u", "usage", false, "Print usage."); CommandLineParser parser = new PosixParser(); CommandLine cl = parser.parse(options, args, false); String host = cl.getOptionValue("h", "172.28.128.6"); String portString = cl.getOptionValue("p", "3000"); int port = Integer.parseInt(portString); String namespace = cl.getOptionValue("n", "test"); String set = cl.getOptionValue("s", "demo"); log.debug("Host: " + host); log.debug("Port: " + port); log.debug("Namespace: " + namespace); log.debug("Set: " + set); @SuppressWarnings("unchecked") List<String> cmds = cl.getArgList(); if (cmds.size() == 0 && cl.hasOption("u")) { logUsage(options); return; } StorePrimaryKey as = new StorePrimaryKey(host, port, namespace, set); as.work(); } catch (Exception e) { log.error("Critical error", e); } }
From source file:cc.twittertools.stream.GatherStatusStream.java
@SuppressWarnings("static-access") public static void main(String[] args) throws TwitterException { Options options = new Options(); options.addOption(OptionBuilder.withArgName("list").hasArgs() .withDescription("comma-separated list of BCP 47 language identifiers").withLongOpt(LANGUAGE_OPTION) .create('l')); options.addOption(OptionBuilder.withArgName("list").hasArgs() .withDescription(//from ww w . ja v a 2 s. c om "comma-separated list of longitude,latitude pairs specifying a set of bounding boxes") .withLongOpt(LOCATIONS_OPTION).create('g')); options.addOption("n", NO_BOUNDING_BOX_OPTION, false, "do not consider places' bounding box"); 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()); HelpFormatter formatter = new HelpFormatter(); formatter.printHelp(RunQueriesThrift.class.getName(), options); System.exit(-1); } PatternLayout layoutStandard = new PatternLayout(); layoutStandard.setConversionPattern("[%p] %d %c %M - %m%n"); PatternLayout layoutSimple = new PatternLayout(); layoutSimple.setConversionPattern("%m%n"); // Filter for the statuses: we only want INFO messages LevelRangeFilter filter = new LevelRangeFilter(); filter.setLevelMax(Level.INFO); filter.setLevelMin(Level.INFO); filter.setAcceptOnMatch(true); filter.activateOptions(); TimeBasedRollingPolicy statusesRollingPolicy = new TimeBasedRollingPolicy(); statusesRollingPolicy.setFileNamePattern("statuses.log" + HOUR_ROLL); statusesRollingPolicy.activateOptions(); RollingFileAppender statusesAppender = new RollingFileAppender(); statusesAppender.setRollingPolicy(statusesRollingPolicy); statusesAppender.addFilter(filter); statusesAppender.setLayout(layoutSimple); statusesAppender.activateOptions(); TimeBasedRollingPolicy warningsRollingPolicy = new TimeBasedRollingPolicy(); warningsRollingPolicy.setFileNamePattern("warnings.log" + HOUR_ROLL); warningsRollingPolicy.activateOptions(); RollingFileAppender warningsAppender = new RollingFileAppender(); warningsAppender.setRollingPolicy(statusesRollingPolicy); warningsAppender.setThreshold(Level.WARN); warningsAppender.setLayout(layoutStandard); warningsAppender.activateOptions(); ConsoleAppender consoleAppender = new ConsoleAppender(); consoleAppender.setThreshold(Level.WARN); consoleAppender.setLayout(layoutStandard); consoleAppender.activateOptions(); // configures the root logger Logger rootLogger = Logger.getRootLogger(); rootLogger.setLevel(Level.INFO); rootLogger.removeAllAppenders(); rootLogger.addAppender(consoleAppender); rootLogger.addAppender(statusesAppender); rootLogger.addAppender(warningsAppender); // creates filters for the query FilterQuery fq = new FilterQuery(); StringBuilder criteria = new StringBuilder(); /* * @see https://dev.twitter.com/docs/streaming-apis/parameters#language */ final boolean filterLanguage = cmdline.hasOption(LANGUAGE_OPTION); String[] languages = null; if (filterLanguage) { languages = cmdline.getOptionValue(LANGUAGE_OPTION).split(","); fq.language(languages); criteria.append("languages: [" + cmdline.getOptionValue(LANGUAGE_OPTION) + "]\t"); } final String[] langs = languages; /* * @see https://dev.twitter.com/docs/streaming-apis/parameters#locations */ double[][] locations = null; if (cmdline.hasOption(LOCATIONS_OPTION)) { String[] locationsArg = cmdline.getOptionValue(LOCATIONS_OPTION).split(","); int nCoords = locationsArg.length; if (nCoords % 2 == 0) { int pairs = nCoords / 2; locations = new double[pairs][2]; int cnt = 0; for (int i = 0; i < pairs; i++) { locations[i][0] = Double.parseDouble(locationsArg[cnt]); cnt++; locations[i][1] = Double.parseDouble(locationsArg[cnt]); cnt++; } fq.locations(locations); criteria.append("locations: [" + cmdline.getOptionValue(LOCATIONS_OPTION) + "]\t"); } else { System.err.println("There is a missing coordinate. See " + "https://dev.twitter.com/docs/streaming-apis/parameters#locations"); System.exit(-1); } } else { fq.locations(new double[][] { { -180, -90 }, { 180, 90 } }); } final double[][] loc = locations; final boolean no_bounding_box = cmdline.hasOption(NO_BOUNDING_BOX_OPTION); if (no_bounding_box) { criteria.append("--no-bounding-box\t"); } // creates a custom logger and log messages final Logger logger = Logger.getLogger(GatherStatusStream.class); logger.info(criteria); RawStreamListener rawListener = new RawStreamListener() { @Override public void onMessage(String rawString) { if (no_bounding_box && loc != null) { try { JSONObject status = new JSONObject(rawString); JSONObject coordObj = status.getJSONObject("coordinates"); JSONArray coords = coordObj.getJSONArray("coordinates"); double longitude = coords.getDouble(0); double latitude = coords.getDouble(1); // checks location for (int i = 0; i < loc.length; i += 2) { if (((loc[i][0] <= longitude) && (longitude <= loc[i + 1][0])) || ((loc[i][1] <= latitude) && (latitude <= loc[i + 1][1]))) { break; } else if (i == loc.length - 1) return; } } catch (JSONException e) { /* Either "Coordinates" is null or trash is coming*/ return; } } if (filterLanguage) { try { JSONObject status = new JSONObject(rawString); // checks language String lang = status.getString("lang"); for (int i = 0; i < langs.length; i++) { if (langs[i].equals(lang)) break; else if (i == langs.length - 1) return; } } catch (JSONException e) { /* Trash is coming */ return; } } cnt++; logger.info(rawString); if (cnt % 1000 == 0) { System.out.println(cnt + " messages received."); } } @Override public void onException(Exception ex) { logger.warn(ex); } }; TwitterStream twitterStream = new TwitterStreamFactory().getInstance(); twitterStream.addListener(rawListener); twitterStream.filter(fq); }
From source file:com.ricston.akka.matrix.Main.java
public static void main(String[] args) throws InterruptedException, NumberFormatException, IOException { CommandLineParser parser = new GnuParser(); int numberOfActors = -1; Options options = new Options(); // Set the command line options recognized by this program. setUpCLOptions(options);//ww w . jav a2 s .co m // Parse the command line arguments. CommandLine cmd = null; try { cmd = parser.parse(options, args); } catch (ParseException e) { quitApp(options); } if (cmd.hasOption(ACTORS)) { // Set the number of actors to create if the user supplied this argument. numberOfActors = Integer.parseInt(cmd.getOptionValue(ACTORS)); } if (cmd.hasOption(GENERATE)) { // Generate a file containing two randomly generated matrices. String[] genArgs = cmd.getOptionValues(GENERATE); if (genArgs.length != 5) { quitApp(options); } MatrixFile.writeMatrixFile(genArgs[0], generateMatrix(Integer.parseInt(genArgs[1]), Integer.parseInt(genArgs[2])), generateMatrix(Integer.parseInt(genArgs[3]), Integer.parseInt(genArgs[4])), new ArrayList<List<Double>>()); } if (cmd.hasOption(COMPUTE)) { // Compute the matrix multiplication of the matrices in the files // given as argument. String[] compArgs = cmd.getOptionValues(COMPUTE); ActorRef managerRef = null; if (numberOfActors > 0) { final int actors = numberOfActors; managerRef = actorOf(new UntypedActorFactory() { public UntypedActor create() { return new ManagerActor(actors); } }); } else { managerRef = actorOf(ManagerActor.class); } // Start the manager actor. managerRef.start(); // Create and send an AllJobsMsg. managerRef.tell(new AllJobsMsg(compArgs)); } if (cmd.getArgs().length > 0 || args.length == 0) { // Handle unrecognized arguments. quitApp(options); } }
From source file:gr.forth.ics.isl.preprocessfilter2.controller.Controller.java
public static void main(String[] args) throws XPathExpressionException, ParserConfigurationException, SAXException, IOException, PreprocessFilterException, TransformerException, ParseException { PropertyReader prop = new PropertyReader(); //The following block of code is executed if there are arguments from the command line if (args.length > 0) { try {//from ww w . j a v a 2 s . c om Options options = new Options(); CommandLineParser PARSER = new PosixParser(); Option inputFile = new Option("inputFile", true, "input xml file"); Option outputFile = new Option("outputFile", true, "output xml file"); Option parentNode = new Option("parentNode", true, "output xml file"); Option newValuesFile = new Option("newValuesFile", true, "new values xml file"); Option newParentNode = new Option("newParentNode", true, "new parent node"); Option newValueTag = new Option("newValueTag", true, "new value tag"); Option oldValueTag = new Option("oldValueTag", true, "old value tag"); Option sameValueTag = new Option("sameValueTag", true, "same value tag"); Option createNewValues = new Option("createNewValues", true, "create new values option"); options.addOption(inputFile).addOption(outputFile).addOption(parentNode).addOption(newValuesFile) .addOption(newParentNode).addOption(newValueTag).addOption(oldValueTag) .addOption(sameValueTag).addOption(createNewValues); CommandLine cli = PARSER.parse(options, args); String inputFileArg = cli.getOptionValue("inputFile"); String outputFileArg = cli.getOptionValue("outputFile"); String parentNodeArg = cli.getOptionValue("parentNode"); String newValuesFileArg = cli.getOptionValue("newValuesFile"); String newParentNodeArg = cli.getOptionValue("newParentNode"); String newValueTagArg = cli.getOptionValue("newValueTag"); String oldValueTagArg = cli.getOptionValue("oldValueTag"); String sameValueTagArg = cli.getOptionValue("sameValueTag"); String createNewValuesArg = cli.getOptionValue("createNewValues"); PreprocessFilterUtilities process = new PreprocessFilterUtilities(); if (createNewValuesArg.equals("yes")) { if (process.createNewValuesFile(inputFileArg, newValuesFileArg, parentNodeArg)) { System.out.println("Succesfull PreProcessing!!!"); } } else { if (process.createOutputFile(inputFileArg, outputFileArg, parentNodeArg, newParentNodeArg, newValueTagArg, oldValueTagArg, sameValueTagArg, newValuesFileArg)) { System.out.println("Succesfull PreProcessing!!!"); } } } catch (PreprocessFilterException ex) { Logger.getLogger(Controller.class.getName()).log(Level.SEVERE, null, ex); throw new PreprocessFilterException("PreProcess Filter Exception:", ex); } } //If there are no command line arguments then the .config file is being used. else { try { String inputFilePathProp = prop.getProperty(inputFilePath); String outputFilePathProp = prop.getProperty(outputFilePath); String parentNodeProp = prop.getProperty(parentNode); String newValuesFilePathProp = prop.getProperty(newValuesFilePath); String newParentNodeProp = prop.getProperty(newParentNode); String newValueTagProp = prop.getProperty(newValueTag); String oldValueTagProp = prop.getProperty(oldValueTag); String sameValueTagProp = prop.getProperty(sameValueTag); String createNewValuesFileProp = prop.getProperty(createNewValuesFile); PreprocessFilterUtilities process = new PreprocessFilterUtilities(); //The filter's code is executed with the .config file's resources as parameters if (createNewValuesFileProp.equals("yes")) { process.createNewValuesFile(inputFilePathProp, newValuesFilePathProp, parentNodeProp); } else { process.createOutputFile(inputFilePathProp, outputFilePathProp, parentNodeProp, newParentNodeProp, newValueTagProp, oldValueTagProp, sameValueTagProp, newValuesFilePathProp); } } catch (PreprocessFilterException ex) { Logger.getLogger(Controller.class.getName()).log(Level.SEVERE, null, ex); throw new PreprocessFilterException("PreProcess Filter Exception:", ex); } } }
From source file:com.act.lcms.db.io.LoadConstructAnalysisTableIntoDB.java
public static void main(String[] args) throws Exception { Options opts = new Options(); opts.addOption(Option.builder("i").argName("path").desc("The TSV file to read").hasArg().required() .longOpt("input-file").build()); // DB connection options. opts.addOption(Option.builder().argName("database url") .desc("The url to use when connecting to the LCMS db").hasArg().longOpt("db-url").build()); opts.addOption(Option.builder("u").argName("database user").desc("The LCMS DB user").hasArg() .longOpt("db-user").build()); opts.addOption(Option.builder("p").argName("database password").desc("The LCMS DB password").hasArg() .longOpt("db-pass").build()); opts.addOption(Option.builder("H").argName("database host") .desc(String.format("The LCMS DB host (default = %s)", DB.DEFAULT_HOST)).hasArg().longOpt("db-host") .build());/* w ww . ja v a 2s.c o m*/ opts.addOption(Option.builder("P").argName("database port") .desc(String.format("The LCMS DB port (default = %d)", DB.DEFAULT_PORT)).hasArg().longOpt("db-port") .build()); opts.addOption(Option.builder("N").argName("database name") .desc(String.format("The LCMS DB name (default = %s)", DB.DEFAULT_DB_NAME)).hasArg() .longOpt("db-name").build()); // Everybody needs a little help from their friends. opts.addOption( Option.builder("h").argName("help").desc("Prints this help message").longOpt("help").build()); CommandLine cl = null; try { CommandLineParser parser = new DefaultParser(); cl = parser.parse(opts, args); } catch (ParseException e) { System.err.format("Argument parsing failed: %s\n", e.getMessage()); HelpFormatter fmt = new HelpFormatter(); fmt.printHelp(LoadConstructAnalysisTableIntoDB.class.getCanonicalName(), opts, true); System.exit(1); } if (cl.hasOption("help")) { new HelpFormatter().printHelp(LoadConstructAnalysisTableIntoDB.class.getCanonicalName(), opts, true); return; } File inputFile = new File(cl.getOptionValue("input-file")); if (!inputFile.exists()) { System.err.format("Unable to find input file at %s\n", cl.getOptionValue("input-file")); new HelpFormatter().printHelp(LoadConstructAnalysisTableIntoDB.class.getCanonicalName(), opts, true); System.exit(1); } DB db; if (cl.hasOption("db-url")) { db = new DB().connectToDB(cl.getOptionValue("db-url")); } else { Integer port = null; if (cl.getOptionValue("P") != null) { port = Integer.parseInt(cl.getOptionValue("P")); } db = new DB().connectToDB(cl.getOptionValue("H"), port, cl.getOptionValue("N"), cl.getOptionValue("u"), cl.getOptionValue("p")); } try { db.getConn().setAutoCommit(false); ConstructAnalysisFileParser parser = new ConstructAnalysisFileParser(); parser.parse(inputFile); List<Pair<Integer, DB.OPERATION_PERFORMED>> results = ChemicalAssociatedWithPathway .insertOrUpdateChemicalsAssociatedWithPathwayFromParser(db, parser); if (results != null) { for (Pair<Integer, DB.OPERATION_PERFORMED> r : results) { System.out.format("%d: %s\n", r.getLeft(), r.getRight()); } } // If we didn't encounter an exception, commit the transaction. db.getConn().commit(); } catch (Exception e) { System.err.format("Caught exception when trying to load plate composition, rolling back. %s\n", e.getMessage()); db.getConn().rollback(); throw (e); } finally { db.getConn().close(); } }
From source file:fr.inria.atlanmod.kyanos.benchmarks.KyanosGraphTraverser.java
public static void main(String[] args) { Options options = new Options(); Option inputOpt = OptionBuilder.create(IN); inputOpt.setArgName("INPUT"); inputOpt.setDescription("Input Kyanos resource directory"); inputOpt.setArgs(1);/*from w w w. j a v a 2 s .c om*/ inputOpt.setRequired(true); Option inClassOpt = OptionBuilder.create(EPACKAGE_CLASS); inClassOpt.setArgName("CLASS"); inClassOpt.setDescription("FQN of EPackage implementation class"); inClassOpt.setArgs(1); inClassOpt.setRequired(true); Option optFileOpt = OptionBuilder.create(OPTIONS_FILE); optFileOpt.setArgName("FILE"); optFileOpt.setDescription("Properties file holding the options to be used in the Kyanos Resource"); optFileOpt.setArgs(1); options.addOption(inputOpt); options.addOption(inClassOpt); options.addOption(optFileOpt); CommandLineParser parser = new PosixParser(); try { PersistenceBackendFactoryRegistry.getFactories().put(NeoBlueprintsURI.NEO_GRAPH_SCHEME, new BlueprintsPersistenceBackendFactory()); CommandLine commandLine = parser.parse(options, args); URI uri = NeoBlueprintsURI.createNeoGraphURI(new File(commandLine.getOptionValue(IN))); Class<?> inClazz = KyanosGraphTraverser.class.getClassLoader() .loadClass(commandLine.getOptionValue(EPACKAGE_CLASS)); inClazz.getMethod("init").invoke(null); ResourceSet resourceSet = new ResourceSetImpl(); resourceSet.getResourceFactoryRegistry().getProtocolToFactoryMap() .put(NeoBlueprintsURI.NEO_GRAPH_SCHEME, PersistentResourceFactory.eINSTANCE); Resource resource = resourceSet.createResource(uri); Map<String, Object> loadOpts = new HashMap<String, Object>(); if (commandLine.hasOption(OPTIONS_FILE)) { Properties properties = new Properties(); properties.load(new FileInputStream(new File(commandLine.getOptionValue(OPTIONS_FILE)))); for (final Entry<Object, Object> entry : properties.entrySet()) { loadOpts.put((String) entry.getKey(), (String) entry.getValue()); } } resource.load(loadOpts); LOG.log(Level.INFO, "Start counting"); int count = 0; long begin = System.currentTimeMillis(); for (Iterator<EObject> iterator = resource.getAllContents(); iterator.hasNext(); iterator .next(), count++) ; long end = System.currentTimeMillis(); LOG.log(Level.INFO, "End counting"); LOG.log(Level.INFO, MessageFormat.format("Resource {0} contains {1} elements", uri, count)); LOG.log(Level.INFO, MessageFormat.format("Time spent: {0}", MessageUtil.formatMillis(end - begin))); if (resource instanceof PersistentResourceImpl) { PersistentResourceImpl.shutdownWithoutUnload((PersistentResourceImpl) resource); } else { resource.unload(); } } catch (ParseException e) { MessageUtil.showError(e.toString()); MessageUtil.showError("Current arguments: " + Arrays.toString(args)); HelpFormatter formatter = new HelpFormatter(); formatter.printHelp("java -jar <this-file.jar>", options, true); } catch (Throwable e) { MessageUtil.showError(e.toString()); } }
From source file:com.continuuity.loom.runtime.DummyProvisioner.java
public static void main(final String[] args) throws Exception { Options options = new Options(); options.addOption("h", "host", true, "Loom server to connect to"); options.addOption("p", "port", true, "Loom server port to connect to"); options.addOption("c", "concurrency", true, "default concurrent threads"); options.addOption("f", "failurePercent", true, "% of the time a provisioner should fail its task"); options.addOption("o", "once", false, "whether or not only one task should be taken before exiting"); options.addOption("d", "taskDuration", true, "number of milliseconds it should take to finish a task"); options.addOption("s", "sleepMs", true, "number of milliseconds a thread will sleep before taking another task"); options.addOption("n", "numTasks", true, "number of tasks to try and take from the queue. Default is infinite."); options.addOption("t", "tenant", true, "tenant id to use."); try {//from w w w. j a v a2 s . c om CommandLineParser parser = new BasicParser(); CommandLine cmd = parser.parse(options, args); host = cmd.hasOption('h') ? cmd.getOptionValue('h') : "localhost"; port = cmd.hasOption('p') ? Integer.valueOf(cmd.getOptionValue('p')) : 55054; concurrency = cmd.hasOption('c') ? Integer.valueOf(cmd.getOptionValue('c')) : 5; failurePercent = cmd.hasOption('f') ? Integer.valueOf(cmd.getOptionValue('f')) : 0; runOnce = cmd.hasOption('o'); taskMs = cmd.hasOption('d') ? Long.valueOf(cmd.getOptionValue('d')) : 1000; sleepMs = cmd.hasOption('s') ? Long.valueOf(cmd.getOptionValue('s')) : 1000; numTasks = cmd.hasOption('n') ? Integer.valueOf(cmd.getOptionValue('n')) : -1; tenant = cmd.hasOption('t') ? cmd.getOptionValue('t') : "loom"; } catch (ParseException e) { LOG.error("exception parsing input arguments.", e); return; } if (concurrency < 1) { LOG.error("invalid concurrency level {}.", concurrency); return; } if (runOnce) { new Provisioner("dummy-0", tenant, host, port, failurePercent, taskMs, sleepMs, 1).runOnce(); } else { LOG.info(String.format( "running with %d threads, connecting to %s:%d using tenant %s, with a failure rate of" + "%d percent, task time of %d ms, and sleep time of %d ms between fetches", concurrency, host, port, tenant, failurePercent, taskMs, sleepMs)); pool = Executors.newFixedThreadPool(concurrency); try { int tasksPerProvisioner = numTasks >= 0 ? numTasks / concurrency : -1; int extra = numTasks < 0 ? 0 : numTasks % concurrency; pool.execute(new Provisioner("dummy-0", tenant, host, port, failurePercent, taskMs, sleepMs, tasksPerProvisioner + extra)); for (int i = 1; i < concurrency; i++) { pool.execute(new Provisioner("dummy-" + i, tenant, host, port, failurePercent, taskMs, sleepMs, tasksPerProvisioner)); } } catch (Exception e) { LOG.error("Caught exception, shutting down now.", e); pool.shutdownNow(); } pool.shutdown(); } }
From source file:de.mpi_bremen.mgg.FastaValidatorUi.java
/** * @param args the command line arguments *///from w w w. ja v a 2 s.c om public static void main(String[] args) { // create Options object Options options = new Options(); // add verbose option options.addOption("v", "verbose", false, "verbose mode"); // add inputfile option options.addOption("f", "file", true, "FASTA-formatted input file"); // add help option options.addOption("h", "help", false, "print this help message"); // add sequencetype option options.addOption("t", "seqtype", true, "sequence type (allowed values: all|dna|rna|protein)"); // add gui-mode option options.addOption("nogui", false, "start in non-interactive mode"); //create the cmdline-parser GnuParser parser = new GnuParser(); // parse the command line arguments try { //get cmdline arguments cmdline = parser.parse(options, args); } catch (ParseException exp) { System.err.println(exp.getMessage()); HelpFormatter formatter = new HelpFormatter(); formatter.printHelp("FastaValidatorUi", options, true); System.exit(1); //indicate error to external environment } if (cmdline.hasOption("h")) { HelpFormatter formatter = new HelpFormatter(); formatter.printHelp("FastaValidatorUi", options, true); System.exit(0); //indicate no errors to external environment } //which mode? (gui or cmdline) if (cmdline.hasOption("nogui")) { //cmdline-mode //create new cmdline-gui FvCmdline cmdlinegui = new FvCmdline(); //set verbosity cmdlinegui.setVerbose(cmdline.hasOption("v")); //handle input file if (cmdline.hasOption("f")) { cmdlinegui.setInput(cmdline.getOptionValue("f")); } //set sequencetype if (cmdline.hasOption("t")) { cmdlinegui.setSequencetype(getSeqtype(cmdline.getOptionValue("t"))); } else { cmdlinegui.setSequencetype(FastaValidator.Sequencetype.ALL); } //trigger validation and exit with exitcode int exitcode = cmdlinegui.validate().getValue(); System.exit(exitcode); } else { //gui-mode //start gui in its own thread SwingUtilities.invokeLater(new Runnable() { public void run() { FvGui w = new FvGui(); } }); } }
From source file:apps.LuceneQuery.java
public static void main(String[] args) { Options options = new Options(); options.addOption("d", null, true, "index directory"); options.addOption("i", null, true, "input file"); options.addOption("s", null, true, "stop word file"); options.addOption("n", null, true, "max # of results"); options.addOption("o", null, true, "a TREC-style output file"); options.addOption("r", null, true, "an optional QREL file, if specified," + "we save results only for queries for which we find at least one relevant entry."); options.addOption("prob", null, true, "question sampling probability"); options.addOption("max_query_qty", null, true, "a maximum number of queries to run"); options.addOption("bm25_b", null, true, "BM25 parameter: b"); options.addOption("bm25_k1", null, true, "BM25 parameter: k1"); options.addOption("bm25fixed", null, false, "use the fixed BM25 similarity"); options.addOption("seed", null, true, "random seed"); Joiner commaJoin = Joiner.on(','); Joiner spaceJoin = Joiner.on(' '); options.addOption("source_type", null, true, "query source type: " + commaJoin.join(SourceFactory.getQuerySourceList())); CommandLineParser parser = new org.apache.commons.cli.GnuParser(); QrelReader qrels = null;/*from w w w . jav a2 s . co m*/ try { CommandLine cmd = parser.parse(options, args); String indexDir = null; if (cmd.hasOption("d")) { indexDir = cmd.getOptionValue("d"); } else { Usage("Specify 'index directory'", options); } String inputFileName = null; if (cmd.hasOption("i")) { inputFileName = cmd.getOptionValue("i"); } else { Usage("Specify 'input file'", options); } DictNoComments stopWords = null; if (cmd.hasOption("s")) { String stopWordFileName = cmd.getOptionValue("s"); stopWords = new DictNoComments(new File(stopWordFileName), true /* lowercasing */); System.out.println("Using the stopword file: " + stopWordFileName); } String sourceName = cmd.getOptionValue("source_type"); if (sourceName == null) Usage("Specify document source type", options); int numRet = 100; if (cmd.hasOption("n")) { numRet = Integer.parseInt(cmd.getOptionValue("n")); System.out.println("Retrieving at most " + numRet + " candidate entries."); } String trecOutFileName = null; if (cmd.hasOption("o")) { trecOutFileName = cmd.getOptionValue("o"); } else { Usage("Specify 'a TREC-style output file'", options); } double fProb = 1.0f; if (cmd.hasOption("prob")) { try { fProb = Double.parseDouble(cmd.getOptionValue("prob")); } catch (NumberFormatException e) { Usage("Wrong format for 'question sampling probability'", options); } } if (fProb <= 0 || fProb > 1) { Usage("Question sampling probability should be >0 and <=1", options); } System.out.println("Sample the following fraction of questions: " + fProb); float bm25_k1 = UtilConst.BM25_K1_DEFAULT, bm25_b = UtilConst.BM25_B_DEFAULT; if (cmd.hasOption("bm25_k1")) { try { bm25_k1 = Float.parseFloat(cmd.getOptionValue("bm25_k1")); } catch (NumberFormatException e) { Usage("Wrong format for 'bm25_k1'", options); } } if (cmd.hasOption("bm25_b")) { try { bm25_b = Float.parseFloat(cmd.getOptionValue("bm25_b")); } catch (NumberFormatException e) { Usage("Wrong format for 'bm25_b'", options); } } long seed = 0; String tmpl = cmd.getOptionValue("seed"); if (tmpl != null) seed = Long.parseLong(tmpl); System.out.println("Using seed: " + seed); Random randGen = new Random(seed); System.out.println(String.format("BM25 parameters k1=%f b=%f ", bm25_k1, bm25_b)); boolean useFixedBM25 = cmd.hasOption("bm25fixed"); EnglishAnalyzer analyzer = new EnglishAnalyzer(); Similarity similarity = null; if (useFixedBM25) { System.out.println(String.format("Using fixed BM25Simlarity, k1=%f b=%f", bm25_k1, bm25_b)); similarity = new BM25SimilarityFix(bm25_k1, bm25_b); } else { System.out.println(String.format("Using Lucene BM25Similarity, k1=%f b=%f", bm25_k1, bm25_b)); similarity = new BM25Similarity(bm25_k1, bm25_b); } int maxQueryQty = Integer.MAX_VALUE; if (cmd.hasOption("max_query_qty")) { try { maxQueryQty = Integer.parseInt(cmd.getOptionValue("max_query_qty")); } catch (NumberFormatException e) { Usage("Wrong format for 'max_query_qty'", options); } } System.out.println(String.format("Executing at most %d queries", maxQueryQty)); if (cmd.hasOption("r")) { String qrelFile = cmd.getOptionValue("r"); System.out.println("Using the qrel file: '" + qrelFile + "', queries not returning a relevant entry will be ignored."); qrels = new QrelReader(qrelFile); } System.out.println(String.format("Using indexing directory %s", indexDir)); LuceneCandidateProvider candProvider = new LuceneCandidateProvider(indexDir, analyzer, similarity); TextCleaner textCleaner = new TextCleaner(stopWords); QuerySource inpQuerySource = SourceFactory.createQuerySource(sourceName, inputFileName); QueryEntry inpQuery = null; BufferedWriter trecOutFile = new BufferedWriter(new FileWriter(new File(trecOutFileName))); int questNum = 0, questQty = 0; long totalTimeMS = 0; while ((inpQuery = inpQuerySource.next()) != null) { if (questQty >= maxQueryQty) break; ++questNum; String queryID = inpQuery.mQueryId; if (randGen.nextDouble() <= fProb) { ++questQty; String tokQuery = spaceJoin.join(textCleaner.cleanUp(inpQuery.mQueryText)); String query = TextCleaner.luceneSafeCleanUp(tokQuery).trim(); ResEntry[] results = null; if (query.isEmpty()) { results = new ResEntry[0]; System.out.println(String.format("WARNING, empty query id = '%s'", inpQuery.mQueryId)); } else { try { long start = System.currentTimeMillis(); results = candProvider.getCandidates(questNum, query, numRet); long end = System.currentTimeMillis(); long searchTimeMS = end - start; totalTimeMS += searchTimeMS; System.out.println(String.format( "Obtained results for the query # %d (answered %d queries), queryID %s the search took %d ms, we asked for max %d entries got %d", questNum, questQty, queryID, searchTimeMS, numRet, results.length)); } catch (ParseException e) { e.printStackTrace(); System.err.println( "Error parsing query: " + query + " orig question is :" + inpQuery.mQueryText); System.exit(1); } } boolean bSave = true; if (qrels != null) { boolean bOk = false; for (ResEntry r : results) { String label = qrels.get(queryID, r.mDocId); if (candProvider.isRelevLabel(label, 1)) { bOk = true; break; } } if (!bOk) bSave = false; } // System.out.println(String.format("Ranking results the query # %d queryId='%s' save results? %b", // questNum, queryID, bSave)); if (bSave) { saveTrecResults(queryID, results, trecOutFile, TREC_RUN, numRet); } } if (questNum % 1000 == 0) System.out.println(String.format("Proccessed %d questions", questNum)); } System.out.println(String.format("Proccessed %d questions, the search took %f MS on average", questQty, (float) totalTimeMS / questQty)); trecOutFile.close(); } catch (ParseException e) { e.printStackTrace(); Usage("Cannot parse arguments: " + e, options); } catch (Exception e) { System.err.println("Terminating due to an exception: " + e); System.exit(1); } }
From source file:co.cask.cdap.data.tools.CoprocessorBuildTool.java
public static void main(final String[] args) throws ParseException { Options options = new Options().addOption(new Option("h", "help", false, "Print this usage message.")) .addOption(new Option("f", "force", false, "Overwrites any coprocessors that already exist.")); CommandLineParser parser = new BasicParser(); CommandLine commandLine = parser.parse(options, args); String[] commandArgs = commandLine.getArgs(); // if help is an option, or if there isn't a single 'ensure' command, print usage and exit. if (commandLine.hasOption("h") || commandArgs.length != 1 || !"check".equalsIgnoreCase(commandArgs[0])) { HelpFormatter helpFormatter = new HelpFormatter(); helpFormatter.printHelp(CoprocessorBuildTool.class.getName() + " check", "Checks that HBase coprocessors required by CDAP are loaded onto HDFS. " + "If not, the coprocessors are built and placed on HDFS.", options, ""); System.exit(0);//from w ww .j ava 2s . com } boolean overwrite = commandLine.hasOption("f"); CConfiguration cConf = CConfiguration.create(); Configuration hConf = HBaseConfiguration.create(); Injector injector = Guice.createInjector(new ConfigModule(cConf, hConf), // for LocationFactory new PrivateModule() { @Override protected void configure() { bind(FileContext.class).toProvider(FileContextProvider.class).in(Scopes.SINGLETON); expose(LocationFactory.class); } @Provides @Singleton private LocationFactory providesLocationFactory(Configuration hConf, CConfiguration cConf, FileContext fc) { final String namespace = cConf.get(Constants.CFG_HDFS_NAMESPACE); return new FileContextLocationFactory(hConf, fc, namespace); } }); try { SecurityUtil.loginForMasterService(cConf); } catch (Exception e) { LOG.error("Failed to login as CDAP user", e); System.exit(1); } LocationFactory locationFactory = injector.getInstance(LocationFactory.class); HBaseTableUtil tableUtil = new HBaseTableUtilFactory(cConf).get(); CoprocessorManager coprocessorManager = new CoprocessorManager(cConf, locationFactory, tableUtil); try { Location location = coprocessorManager.ensureCoprocessorExists(overwrite); LOG.info("coprocessor exists at {}.", location); } catch (IOException e) { LOG.error("Unable to build and upload coprocessor jars.", e); System.exit(1); } }