List of usage examples for org.apache.commons.cli CommandLineParser parse
CommandLine parse(Options options, String[] arguments) throws ParseException;
From source file:gr.demokritos.iit.demos.Demo.java
public static void main(String[] args) { try {/* www . j a v a2 s . com*/ Options options = new Options(); options.addOption("h", HELP, false, "show help."); options.addOption("i", INPUT, true, "The file containing JSON " + " representations of tweets or SAG posts - 1 per line" + " default file looked for is " + DEFAULT_INFILE); options.addOption("o", OUTPUT, true, "Where to write the output " + " default file looked for is " + DEFAULT_OUTFILE); options.addOption("p", PROCESS, true, "Type of processing to do " + " ner for Named Entity Recognition re for Relation Extraction" + " default is NER"); options.addOption("s", SAG, false, "Whether to process as SAG posts" + " default is off - if passed means process as SAG posts"); CommandLineParser parser = new BasicParser(); CommandLine cmd = parser.parse(options, args); // DEFAULTS String filename = DEFAULT_INFILE; String outfilename = DEFAULT_OUTFILE; String process = NER; boolean isSAG = false; if (cmd.hasOption(HELP)) { HelpFormatter formatter = new HelpFormatter(); formatter.printHelp("NER + RE extraction module", options); System.exit(0); } if (cmd.hasOption(INPUT)) { filename = cmd.getOptionValue(INPUT); } if (cmd.hasOption(OUTPUT)) { outfilename = cmd.getOptionValue(OUTPUT); } if (cmd.hasOption(SAG)) { isSAG = true; } if (cmd.hasOption(PROCESS)) { process = cmd.getOptionValue(PROCESS); } System.out.println(); System.out.println("Reading from file: " + filename); System.out.println("Process type: " + process); System.out.println("Processing SAG: " + isSAG); System.out.println("Writing to file: " + outfilename); System.out.println(); List<String> jsoni = new ArrayList(); Scanner in = new Scanner(new FileReader(filename)); while (in.hasNextLine()) { String json = in.nextLine(); jsoni.add(json); } PrintWriter writer = new PrintWriter(outfilename, "UTF-8"); System.out.println("Read " + jsoni.size() + " lines from " + filename); if (process.equalsIgnoreCase(RE)) { System.out.println("Running Relation Extraction"); System.out.println(); String json = API.RE(jsoni, isSAG); System.out.println(json); writer.print(json); } else { System.out.println("Running Named Entity Recognition"); System.out.println(); jsoni = API.NER(jsoni, isSAG); /* for(String json: jsoni){ NamedEntityList nel = NamedEntityList.fromJSON(json); nel.prettyPrint(); } */ for (String json : jsoni) { System.out.println(json); writer.print(json); } } writer.close(); } catch (ParseException | UnsupportedEncodingException | FileNotFoundException ex) { Logger.getLogger(Demo.class.getName()).log(Level.SEVERE, null, ex); } }
From source file:com.picdrop.security.SecureStoreMain.java
static public void main(String[] args) throws ParseException, IOException, FileNotFoundException, NoSuchAlgorithmException, CertificateException, KeyStoreException, KeyStoreException, InterruptedException { CommandLineParser cliP = new DefaultParser(); Options ops = generateBasicOptions(); CommandLine cli = cliP.parse(ops, args); HelpFormatter hlp = new HelpFormatter(); SecureStore ss;/*from w w w .ja va 2 s . c o m*/ String path = "."; try { if (cli.hasOption("help")) { hlp.printHelp("SecureStore", ops); System.exit(0); } if (cli.hasOption("keystore")) { path = cli.getOptionValue("keystore", "."); } if (cli.hasOption("create")) { ss = new SecureStore(path, false); ss.createKeyStore(); System.exit(0); } else { ss = new SecureStore(path, true); } if (cli.hasOption("list")) { Enumeration<String> en = ss.listAlias(); while (en.hasMoreElements()) { System.out.println(en.nextElement()); } System.exit(0); } if (cli.hasOption("store")) { ss.storeValue(cli.getOptionValues("store")[0], cli.getOptionValues("store")[1]); ss.writeStore(); System.exit(0); } if (cli.hasOption("clear")) { ss.deleteValue(cli.getOptionValue("clear")); ss.writeStore(); System.exit(0); } } finally { hlp.printHelp("SecureStore", ops); System.exit(0); } }
From source file:com.act.lcms.db.io.ExportPlateCompositionFromDB.java
public static void main(String[] args) throws Exception { Options opts = new Options(); opts.addOption(Option.builder("b").argName("barcode").desc("The barcode of the plate to print").hasArg() .longOpt("barcode").build()); opts.addOption(Option.builder("n").argName("name").desc("The name of the plate to print").hasArg() .longOpt("name").build()); opts.addOption(Option.builder("o").argName("output file").desc( "An output file to which to write this plate's composition table (writes to stdout if omitted") .hasArg().longOpt("output-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());//from w w w . java2s . co 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()); new HelpFormatter().printHelp(LoadPlateCompositionIntoDB.class.getCanonicalName(), opts, true); System.exit(1); } if (cl.hasOption("help")) { new HelpFormatter().printHelp(LoadPlateCompositionIntoDB.class.getCanonicalName(), opts, true); return; } if (!cl.hasOption("b") && !cl.hasOption("n")) { System.err.format("Must specify either plate barcode or plate name."); new HelpFormatter().printHelp(LoadPlateCompositionIntoDB.class.getCanonicalName(), opts, true); System.exit(1); } DB db = null; try { 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")); } Writer writer = null; if (cl.hasOption("o")) { writer = new FileWriter(cl.getOptionValue("o")); } else { writer = new OutputStreamWriter(System.out); } PlateCompositionWriter cw = new PlateCompositionWriter(); if (cl.hasOption("b")) { cw.writePlateCompositionByBarcode(db, cl.getOptionValue("b"), writer); } else if (cl.hasOption("n")) { cw.writePlateCompositionByName(db, cl.getOptionValue("n"), writer); } writer.close(); } finally { if (db != null) { db.close(); } } }
From source file:com.etsy.arbiter.Arbiter.java
public static void main(String[] args) throws ParseException, ConfigurationException, IOException, ParserConfigurationException, TransformerException { Options options = getOptions();//from www .ja va2 s . c o m CommandLineParser cmd = new GnuParser(); CommandLine parsed = cmd.parse(options, args); if (parsed.hasOption("h")) { printUsage(options); } if (!parsed.hasOption("i")) { throw new ParseException("Missing required argument: i"); } if (!parsed.hasOption("o")) { throw new ParseException("Missing required argument: o"); } String[] configFiles = parsed.getOptionValues("c"); String[] lowPrecedenceConfigFiles = parsed.getOptionValues("l"); String[] inputFiles = parsed.getOptionValues("i"); String outputDir = parsed.getOptionValue("o"); List<Config> parsedConfigFiles = readConfigFiles(configFiles, false); parsedConfigFiles.addAll(readConfigFiles(lowPrecedenceConfigFiles, true)); Config merged = ConfigurationMerger.mergeConfiguration(parsedConfigFiles); List<Workflow> workflows = readWorkflowFiles(inputFiles); boolean generateGraphviz = parsed.hasOption("g"); String graphvizFormat = parsed.getOptionValue("g", "svg"); OozieWorkflowGenerator generator = new OozieWorkflowGenerator(merged); generator.generateOozieWorkflows(outputDir, workflows, generateGraphviz, graphvizFormat); }
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());/*from www .j av a 2 s .c om*/ 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:com.example.geomesa.kafka08.KafkaListener.java
public static void main(String[] args) throws Exception { // read command line args for a connection to Kafka CommandLineParser parser = new BasicParser(); Options options = getCommonRequiredOptions(); CommandLine cmd = parser.parse(options, args); // create the consumer KafkaDataStore object Map<String, String> dsConf = getKafkaDataStoreConf(cmd); dsConf.put("isProducer", "false"); DataStore consumerDS = DataStoreFinder.getDataStore(dsConf); // verify that we got back our KafkaDataStore object properly if (consumerDS == null) { throw new Exception("Null consumer KafkaDataStore"); }/*from ww w .ja v a 2 s . co m*/ // create the schema which creates a topic in Kafka // (only needs to be done once) // TODO: This should be rolled into the Command line options to make this more general. registerListeners(consumerDS); while (true) { // Wait for user to terminate with ctrl-C. } }
From source file:eu.scape_project.pc.hadoop.DroidIdentifyHadoopJob.java
/** * The main entry point.// w ww.ja va 2 s .co m */ public static void main(String[] args) throws ParseException { Configuration conf = new Configuration(); GenericOptionsParser gop = new GenericOptionsParser(conf, args); HadoopJobCliConfig pc = new HadoopJobCliConfig(); CommandLineParser cmdParser = new PosixParser(); CommandLine cmd = cmdParser.parse(HadoopJobOptions.OPTIONS, gop.getRemainingArgs()); if ((args.length == 0) || (cmd.hasOption(HadoopJobOptions.HELP_OPT))) { HadoopJobOptions.exit("Usage", 0); } else { HadoopJobOptions.initOptions(cmd, pc); } String dir = pc.getDirStr(); String name = pc.getHadoopJobName(); if (name == null || name.equals("")) { name = "droid_identification"; } try { Job job = new Job(conf, name); // local debugging // job.getConfiguration().set("mapred.job.tracker", "local"); // job.getConfiguration().set("fs.default.name", "file:///"); job.setJarByClass(DroidIdentifyHadoopJob.class); job.setMapperClass(DroidIdentifyMapper.class); //job.setCombinerClass(DroidIdentifyReducer.class); job.setReducerClass(DroidIdentifyReducer.class); job.setInputFormatClass(TextInputFormat.class); job.setOutputFormatClass(TextOutputFormat.class); //SequenceFileOutputFormat.setOutputCompressionType(job, SequenceFile.CompressionType.NONE); //conf.setMapOutputKeyClass(Text.class); job.setMapOutputKeyClass(Text.class); job.setMapOutputValueClass(LongWritable.class); job.setOutputKeyClass(Text.class); job.setOutputValueClass(LongWritable.class); SequenceFileInputFormat.addInputPath(job, new Path(dir)); String outpath = "output/" + System.currentTimeMillis() + "dri"; FileOutputFormat.setOutputPath(job, new Path(outpath)); job.waitForCompletion(true); System.out.print(outpath); System.exit(0); } catch (Exception e) { logger.error("I/O error", e); } }
From source file:is.iclt.icenlp.runner.RunLemmald.java
public static void main(String[] args) { CommandLineParser parser = new GnuParser(); try {/* ww w.j a v a 2 s .c o m*/ // parse the command line arguments CommandLine cmdLine = parser.parse(options, args); if (cmdLine.hasOption("h")) { HelpFormatter formatter = new HelpFormatter(); formatter.printHelp("lemmatize.sh", options); } else { runConsole(cmdLine); } } catch (ParseException exp) { // oops, something went wrong System.err.println("Could not parse command line arguments. Reason: " + exp.getMessage()); } }
From source file:net.sf.brunneng.iqdb.db.DBWorker.java
public static void main(String[] args) { Options options = new Options(); Option actionOpt = new Option("a", "action", true, "The action on db, which should be executed." + BR_LINE + " Possible actions: " + BR_LINE + createActionsDescription()); actionOpt.setRequired(true);/*from w ww . ja v a2 s . c om*/ options.addOption(actionOpt); CommandLineParser parser = new GnuParser(); try { CommandLine commandLine = parser.parse(options, args); String actionStr = commandLine.getOptionValue("action"); DBAction action = null; try { action = DBAction.valueOf(actionStr); } catch (IllegalStateException exc) { System.out.println("Unknown action: " + actionStr); System.out.println("Possible actions: " + createActionsDescription()); System.exit(-1); } DBWorker worker = new DBWorker(action, null, null, null); worker.execute(); } catch (ParseException exc) { System.out.println("Unable to parse command line arguments. Error: " + exc.getMessage()); printUsageAndExit(options); } }
From source file:com.github.r351574nc3.amex.assignment1.App.java
public static void main(final String... args) { if (args.length < 1) { printUsage();//from w ww . j a v a 2 s . c o m System.exit(0); } final Options options = new Options(); options.addOption(OptionBuilder.withArgName("output").hasArg(true).withDescription("Path for CSV output") .create("o")); options.addOption( OptionBuilder.withArgName("input").hasArg(true).withDescription("Path for CSV input").create("i")); final CommandLineParser parser = new BasicParser(); CommandLine cmd = null; try { cmd = parser.parse(options, args); } catch (Exception e) { System.out.println(e.getMessage()); printUsage(); System.exit(0); } if ((cmd.hasOption('o') && cmd.hasOption('i')) || !(cmd.hasOption('o') || cmd.hasOption('i')) || (cmd.hasOption('o') && cmd.getArgs().length < 1)) { printUsage(); System.exit(0); } if (cmd.hasOption('o') && cmd.getArgs().length > 0) { final String outputName = cmd.getOptionValue("o"); final int iterations = Integer.parseInt(cmd.getArgs()[cmd.getArgs().length - 1]); new App().run(new File(outputName), iterations); } else if (cmd.hasOption('i')) { final String inputName = cmd.getOptionValue("i"); new App().run(new File(inputName)); } System.exit(0); }