List of usage examples for org.apache.commons.cli GnuParser GnuParser
GnuParser
From source file:cognition.pipeline.Main.java
/** * Entry point of Cognition-DNC/*ww w.ja va 2 s.c o m*/ */ public static void main(String[] args) { if (requiresHelp(args)) { CommandHelper.printHelp(); System.exit(0); } String path = "file:" + getCurrentFolder() + File.separator + "config" + File.separator + "applicationContext.xml"; logger.info("Loading context from " + path); context = new ClassPathXmlApplicationContext(path); Options options = getOptions(); CommandLineParser parser = new GnuParser(); Runtime.getRuntime().addShutdownHook(getShutDownBehaviour()); try { CommandLine cmd = parser.parse(options, args); processCommands(cmd); } catch (Exception e) { e.printStackTrace(); } }
From source file:cc.twittertools.index.ExtractTermStatisticsFromIndex.java
@SuppressWarnings("static-access") public static void main(String[] args) throws Exception { Options options = new Options(); options.addOption(OptionBuilder.withArgName("dir").hasArg().withDescription("index").create(INDEX_OPTION)); options.addOption(OptionBuilder.withArgName("num").hasArg().withDescription("min").create(MIN_OPTION)); CommandLine cmdline = null;/*from w ww. jav a 2 s . c om*/ 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_OPTION)) { HelpFormatter formatter = new HelpFormatter(); formatter.printHelp(ExtractTermStatisticsFromIndex.class.getName(), options); System.exit(-1); } String indexLocation = cmdline.getOptionValue(INDEX_OPTION); int min = cmdline.hasOption(MIN_OPTION) ? Integer.parseInt(cmdline.getOptionValue(MIN_OPTION)) : 1; PrintStream out = new PrintStream(System.out, true, "UTF-8"); IndexReader reader = DirectoryReader.open(FSDirectory.open(new File(indexLocation))); Terms terms = SlowCompositeReaderWrapper.wrap(reader).terms(StatusField.TEXT.name); TermsEnum termsEnum = terms.iterator(TermsEnum.EMPTY); long missingCnt = 0; int skippedTerms = 0; BytesRef bytes = new BytesRef(); while ((bytes = termsEnum.next()) != null) { byte[] buf = new byte[bytes.length]; System.arraycopy(bytes.bytes, 0, buf, 0, bytes.length); String term = new String(buf, "UTF-8"); int df = termsEnum.docFreq(); long cf = termsEnum.totalTermFreq(); if (df < min) { skippedTerms++; missingCnt += cf; continue; } out.println(term + "\t" + df + "\t" + cf); } reader.close(); out.close(); System.err.println("skipped terms: " + skippedTerms + ", cnt: " + missingCnt); }
From source file:com.consol.citrus.Citrus.java
/** * Main CLI method.//from w ww .ja va 2 s. co m * @param args */ public static void main(String[] args) { Options options = new CitrusCliOptions(); HelpFormatter formatter = new HelpFormatter(); try { CommandLine cmd = new GnuParser().parse(options, args); if (cmd.hasOption("help")) { formatter.printHelp("CITRUS TestFramework", options); return; } Citrus citrus = new Citrus(cmd); citrus.run(); } catch (ParseException e) { log.error("Failed to parse command line arguments", e); formatter.printHelp("CITRUS TestFramework", options); } }
From source file:edu.upc.eetac.dsa.exercices.java.lang.exceptions.App.java
public static void main(String[] args) throws FileNotFoundException { String filename = null;//w w w. j ava2s . c o m String maxInteger = null; Options options = new Options(); Option optionFile = OptionBuilder.withArgName("file").hasArg().withDescription("file with integers") .withLongOpt("file").create("f"); options.addOption(optionFile); Option optionMax = OptionBuilder.withArgName("max").hasArg() .withDescription("maximum integer allowed in the file").withLongOpt("max").create("M"); options.addOption(optionFile); options.addOption(optionMax); options.addOption("h", "help", false, "prints this message"); CommandLineParser parser = new GnuParser(); CommandLine line = null; try { // parse the command line arguments line = parser.parse(options, args); if (line.hasOption("h")) { // No hace falta preguntar por el parmetro "help". Ambos son sinnimos new HelpFormatter().printHelp(App.class.getCanonicalName(), options); return; } filename = line.getOptionValue("f"); if (filename == null) { throw new org.apache.commons.cli.ParseException( "You must provide the path to the file with numbers."); } maxInteger = line.getOptionValue("M"); } catch (ParseException exp) { System.err.println(exp.getMessage()); new HelpFormatter().printHelp(App.class.getCanonicalName(), options); return; } try { int[] numbers = (maxInteger != null) ? FileNumberReader.readFile(filename, Integer.parseInt(maxInteger)) : FileNumberReader.readFile(filename); for (int i = 0; i < numbers.length; i++) { System.out.println("Integer read: " + numbers[i]); } } catch (IOException e) { e.printStackTrace(); } catch (BigNumberException e) { e.printStackTrace(); } }
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);/* w ww . j av a 2s . co m*/ 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.garethahealy.fuse.aries.transactions.cli.Application.java
public static void main(String[] args) { LOG.info("Starting..."); DefaultCLIParser parser = new DefaultCLIParser(new GnuParser()); try {/* w w w . java2s . co m*/ CommandLine commandLine = parser.parse(args, parser.getOptions()); Map<String, String> databaseOptions = parser.getDatabaseOptions(commandLine); DbManagedConnectionFactory managedConnectionFactory = new MySqlManagedConnectionFactory(); LOG.info("About to run Aries..."); ConnectionManagerContainer connectionManagerContainer = getConnectionManagerContainer("datasource"); connectionManagerContainer .doRecovery(managedConnectionFactory.getManagedConnectionFactory(databaseOptions)); } catch (ParseException ex) { LOG.error("We hit a problem! {}", ExceptionUtils.getStackTrace(ex)); parser.displayHelp(false); } catch (XAException ex) { LOG.error("We hit a problem! {}", ExceptionUtils.getStackTrace(ex)); parser.displayHelp(false); } catch (SQLException ex) { LOG.error("We hit a problem! {}", ExceptionUtils.getStackTrace(ex)); parser.displayHelp(false); } }
From source file:cc.wikitools.lucene.FetchWikipediaArticle.java
@SuppressWarnings("static-access") public static void main(String[] args) throws Exception { Options options = new Options(); options.addOption(/*w w w . ja va2 s.c om*/ OptionBuilder.withArgName("path").hasArg().withDescription("index location").create(INDEX_OPTION)); options.addOption( OptionBuilder.withArgName("num").hasArg().withDescription("article id").create(ID_OPTION)); options.addOption( OptionBuilder.withArgName("string").hasArg().withDescription("article title").create(TITLE_OPTION)); 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(ID_OPTION) || cmdline.hasOption(TITLE_OPTION)) || !cmdline.hasOption(INDEX_OPTION)) { HelpFormatter formatter = new HelpFormatter(); formatter.printHelp(FetchWikipediaArticle.class.getName(), options); System.exit(-1); } File indexLocation = new File(cmdline.getOptionValue(INDEX_OPTION)); if (!indexLocation.exists()) { System.err.println("Error: " + indexLocation + " does not exist!"); System.exit(-1); } WikipediaSearcher searcher = new WikipediaSearcher(indexLocation); PrintStream out = new PrintStream(System.out, true, "UTF-8"); if (cmdline.hasOption(ID_OPTION)) { int id = Integer.parseInt(cmdline.getOptionValue(ID_OPTION)); Document doc = searcher.getArticle(id); if (doc == null) { System.err.print("id " + id + " doesn't exist!\n"); } else { out.println(doc.getField(IndexField.TEXT.name).stringValue()); } } else { String title = cmdline.getOptionValue(TITLE_OPTION); Document doc = searcher.getArticle(title); if (doc == null) { System.err.print("article \"" + title + "\" doesn't exist!\n"); } else { out.println(doc.getField(IndexField.TEXT.name).stringValue()); } } searcher.close(); out.close(); }
From source file:Homework4Execute.java
/** * @param args//from w w w . j ava2 s . c o m */ public static void main(String[] args) { Parser parser = new GnuParser(); Options options = getCommandLineOptions(); String task = null; String host = null; String dir = null; CommandLine commandLine = null; try { commandLine = parser.parse(options, args); task = (String) commandLine.getOptionValue("task"); if ("traceroute".equals(task) || "cmdWinTrace".equals(task)) { host = (String) commandLine.getOptionValue("host"); if (host == null) { System.err.println("--host parameter required at this task!"); System.exit(-1); } } if ("cmdWinDir".equals(task)) { dir = (String) commandLine.getOptionValue("dir"); if (dir == null) { System.err.println("--dir parameter required at this task!"); System.exit(-1); } } } catch (ParseException e) { HelpFormatter formatter = new HelpFormatter(); formatter.printHelp("java -jar homework4execute.jar", options); System.exit(-1); } if (task != null) { switch (task) { case "enviroment": PrintEnviroment.printEnviroment(); break; case "traceroute": RedirectOutput2Pipe.traceroute(host); break; case "cmdWinTrace": ProcessBuilderExecute.cmdWinTrace(host); break; case "cmdWinDir": RuntimeExecute.cmdWinDir(dir); break; case "uname": RedirectOutput2File.getUname(); break; } } else { RunWithThread.executeCommand("javac Teszt.java"); RunWithThread.executeCommand("java -cp ./ Teszt"); } }
From source file:com.github.jjYBdx4IL.utils.lang.ConstantClassCreator.java
public static void main(String[] args) { try {/* www . ja v a2s. com*/ CommandLineParser parser = new GnuParser(); Options options = new Options(); options.addOption(OPTNAME_H, OPTNAME_HELP, false, "show help (this page)"); options.addOption(null, OPTNAME_SYSPROP, true, "system property name(s)"); options.addOption(null, OPTNAME_OUTPUT_CLASSNAME, true, "output classname"); options.addOption(null, OPTNAME_OUTPUT_DIRECTORY, true, "output directory"); CommandLine line = parser.parse(options, args); if (line.hasOption(OPTNAME_H)) { HelpFormatter formatter = new HelpFormatter(); formatter.printHelp(ConstantClassCreator.class.getName(), options); } else if (line.hasOption(OPTNAME_SYSPROP)) { new ConstantClassCreator().run(line); } } catch (Exception ex) { throw new RuntimeException(ex); } }
From source file:cc.wikitools.lucene.ScoreWikipediaArticle.java
@SuppressWarnings("static-access") public static void main(String[] args) throws Exception { Options options = new Options(); options.addOption(/*from w w w. j av a 2 s. c o m*/ OptionBuilder.withArgName("path").hasArg().withDescription("index location").create(INDEX_OPTION)); options.addOption( OptionBuilder.withArgName("num").hasArg().withDescription("article id").create(ID_OPTION)); options.addOption( OptionBuilder.withArgName("string").hasArg().withDescription("article title").create(TITLE_OPTION)); options.addOption( OptionBuilder.withArgName("string").hasArg().withDescription("query text").create(QUERY_OPTION)); 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(ID_OPTION) || cmdline.hasOption(TITLE_OPTION)) || !cmdline.hasOption(INDEX_OPTION) || !cmdline.hasOption(QUERY_OPTION)) { HelpFormatter formatter = new HelpFormatter(); formatter.printHelp(ScoreWikipediaArticle.class.getName(), options); System.exit(-1); } File indexLocation = new File(cmdline.getOptionValue(INDEX_OPTION)); if (!indexLocation.exists()) { System.err.println("Error: " + indexLocation + " does not exist!"); System.exit(-1); } String queryText = cmdline.getOptionValue(QUERY_OPTION); WikipediaSearcher searcher = new WikipediaSearcher(indexLocation); PrintStream out = new PrintStream(System.out, true, "UTF-8"); if (cmdline.hasOption(ID_OPTION)) { out.println("score: " + searcher.scoreArticle(queryText, Integer.parseInt(cmdline.getOptionValue(ID_OPTION)))); } else { out.println("score: " + searcher.scoreArticle(queryText, cmdline.getOptionValue(TITLE_OPTION))); } searcher.close(); out.close(); }