List of usage examples for org.apache.commons.cli GnuParser GnuParser
GnuParser
From source file:edu.vt.middleware.ldap.cli.AbstractCli.java
/** * Parses command line options and dispatches to the requested action, or the * default action if no action is specified. * * @param args command line arguments//from w w w .j a v a 2 s . com */ public final void performAction(final String[] args) { initOptions(); try { if (args.length > 0) { final CommandLineParser parser = new GnuParser(); final CommandLine line = parser.parse(options, args, false); dispatch(line); } else { printExamples(); } } catch (ParseException pex) { System.err.println("Failed parsing command arguments: " + pex.getMessage()); } catch (IllegalArgumentException iaex) { String msg = "Operation failed: " + iaex.getMessage(); if (iaex.getCause() != null) { msg += " Underlying reason: " + iaex.getCause().getMessage(); } System.err.println(msg); } catch (RuntimeException rex) { throw rex; } catch (Exception ex) { System.err.println("Operation failed:"); ex.printStackTrace(System.err); } }
From source file:cc.wikitools.lucene.hadoop.FetchWikipediaArticleHdfs.java
@SuppressWarnings("static-access") @Override/* w w w . j a v a2 s . c o m*/ public int run(String[] args) throws Exception { Options options = new Options(); options.addOption( OptionBuilder.withArgName("path").hasArg().withDescription("index location").create(INDEX_OPTION)); options.addOption(OptionBuilder.withArgName("num").hasArg().withDescription("id").create(ID_OPTION)); options.addOption( OptionBuilder.withArgName("string").hasArg().withDescription("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); } String indexLocation = cmdline.getOptionValue(INDEX_OPTION); HdfsWikipediaSearcher searcher = new HdfsWikipediaSearcher(new Path(indexLocation), getConf()); 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(); return 0; }
From source file:com.palantir.atlasdb.shell.AtlasShellCli.java
public void run(String[] args) { Options options = new Options(); options.addOption(new Option("h", "help", false, "Prints help message.")); options.addOption(//from w w w . j av a 2s .co m new Option("headless", "Force headless (non-GUI) mode; unnecessary if a scriptlet was specified.")); options.addOption(new Option("scriptlet", true, "Ruby to execute at start; specifying a scriptlet automatically forces headless mode." + " If the argument corresponds to a valid .rb file, then the contents of that" + " file will be executed. Otherwise, the argument will be treated directly as Ruby code.")); try { CommandLineParser parser = new GnuParser(); CommandLine cli = parser.parse(options, args); if (cli.hasOption("h")) { System.out.println("AtlasDBShell is an interface for manually inspecting an AtlasDB\n" + // (authorized) "instance (similar to squirrel or sqlplus for a SQL database).\n"); new HelpFormatter().printHelp("atlasDBShell", options, true); System.exit(0); // (authorized) } String scriptlet = cli.getOptionValue("scriptlet", ""); if (!scriptlet.isEmpty() && cli.hasOption("headless")) { System.out.println( "Note: specifying a scriptlet automatically forces atlasShell to run in headless mode."); // (authorized) } if (!scriptlet.isEmpty()) { atlasShellRun.runHeadless(turnRubyCodeOrRubyFileIntoRubyCode(scriptlet)); } else if (cli.hasOption("headless")) { atlasShellRun.runHeadless(""); } else { atlasShellRun.runHeaded(); } } catch (ParseException e) { System.out.println("Invalid command line - " + e.getMessage()); // (authorized) new HelpFormatter().printHelp("atlasDBShell", options, true); System.exit(1); // (authorized) } catch (HeadlessException e) { System.out.println( "Error: The graphics enviroment is set to headless. Try running with the --headless option."); // (authorized) new HelpFormatter().printHelp("atlasDBShell", options, true); System.exit(1); // (authorized) } }
From source file:cc.wikitools.lucene.hadoop.ScoreWikipediaArticleHdfs.java
@SuppressWarnings("static-access") @Override//w w w . ja va 2 s .c o m public int run(String[] args) throws Exception { Options options = new Options(); options.addOption( OptionBuilder.withArgName("path").hasArg().withDescription("index location").create(INDEX_OPTION)); options.addOption(OptionBuilder.withArgName("num").hasArg().withDescription("id").create(ID_OPTION)); options.addOption( OptionBuilder.withArgName("string").hasArg().withDescription("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(ScoreWikipediaArticleHdfs.class.getName(), options); System.exit(-1); } String indexLocation = cmdline.getOptionValue(INDEX_OPTION); String queryText = cmdline.getOptionValue(QUERY_OPTION); HdfsWikipediaSearcher searcher = new HdfsWikipediaSearcher(new Path(indexLocation), getConf()); 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(); return 0; }
From source file:fr.ens.biologie.genomique.eoulsan.actions.CreateHadoopJarAction.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 . ja v a 2s .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); } } catch (ParseException e) { Common.errorExit(e, "Error while parsing command line arguments: " + e.getMessage()); } if (arguments.size() != argsOptions) { help(options); } // Write log entries Main.getInstance().flushLog(); try { HadoopJarRepackager.repack(); } catch (IOException e) { Common.errorExit(e, "Error while executing " + Globals.APP_NAME_LOWER_CASE + ": " + e.getMessage()); } }
From source file:com.nubits.nubot.launch.CLIOptions.java
/** * Apply Apache Commons CLI GnuParser to command-line arguments. * * @param commandLineArguments Command-line arguments to be processed with * Gnu-style parser. *//* ww w . j ava 2 s .co m*/ public CommandLine parseCommandLineArguments(final String[] commandLineArguments, Options gnuOptions) { final CommandLineParser cmdLineGnuParser = new GnuParser(); CommandLine commandLine = null; try { commandLine = cmdLineGnuParser.parse(gnuOptions, commandLineArguments); } catch (ParseException parseException) // checked exception { LOG.error("Encountered exception while parsing using GnuParser:\n" + parseException.getMessage()); MainLaunch.exitWithNotice("run nubot with \n" + USAGE_STRING); } return commandLine; }
From source file:com.tc.util.ProductInfo.java
public static void main(String[] args) { Options options = new Options(); options.addOption("v", "verbose", false, bundleHelper.getString("option.verbose")); options.addOption("r", "raw", false, bundleHelper.getString("option.raw")); options.addOption("h", "help", false, bundleHelper.getString("option.help")); CommandLineParser parser = new GnuParser(); try {//w w w .j a va2 s . c om CommandLine cli = parser.parse(options, args); if (cli.hasOption("h")) { printHelp(options); System.exit(0); } if (cli.hasOption("v")) { System.out.println(getInstance().toLongString()); if (getInstance().isPatched()) System.out.println(getInstance().toLongPatchString()); System.exit(0); } if (cli.hasOption("r")) { printRawData(); System.exit(0); } System.out.println(getInstance().toShortString()); } catch (ParseException e) { System.out.println(e.getMessage()); System.out.println(); printHelp(options); System.exit(1); } }
From source file:com.digitalpebble.behemoth.util.CorpusReader.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("c", "displayContent", false, "display binary content in output"); options.addOption("t", "displayText", false, "display text in output"); options.addOption("a", "displayAnnotations", false, "display annotations in output"); options.addOption("m", "displayMetadata", false, "display metadata in output"); // parse the command line arguments CommandLine line = null;//from w w w . jav a2s.c om try { line = parser.parse(options, args); String input = line.getOptionValue("i"); if (line.hasOption("help")) { formatter.printHelp("CorpusReader", options); return 0; } if (input == null) { formatter.printHelp("CorpusReader", options); return -1; } } catch (ParseException e) { formatter.printHelp("CorpusReader", options); return -1; } boolean showBinaryContent = line.hasOption("displayContent"); boolean showText = line.hasOption("displayText"); boolean showAnnotations = line.hasOption("displayAnnotations"); boolean showMD = line.hasOption("displayMetadata"); Path inputPath = new Path(line.getOptionValue("i")); Configuration conf = getConf(); FileSystem fs = inputPath.getFileSystem(conf); // filter input DocumentFilter filters = DocumentFilter.getFilters(conf); boolean doFilter = DocumentFilter.isRequired(conf); FileStatus[] fss = fs.listStatus(inputPath); for (FileStatus status : fss) { Path path = status.getPath(); // skips the _log or _SUCCESS files if (!path.getName().startsWith("part-") && !path.getName().equals(inputPath.getName())) continue; SequenceFile.Reader reader = new SequenceFile.Reader(fs, path, conf); Text key = new Text(); BehemothDocument value = new BehemothDocument(); while (reader.next(key, value)) { // skip this document? if (doFilter && filters.keep(value) == false) continue; System.out.println(value.toString(showBinaryContent, showAnnotations, showText, showMD)); } reader.close(); } return 0; }
From source file:hmp.HMPRunRemover.java
private CommandLine getOptions(String[] args) throws ParseException { Options o = new Options(); o.addOption("date", true, "date in YYYY_MM_DD"); o.addOption("db", true, "database"); o.addOption("dbUser", true, "db user"); o.addOption("dbPassword", true, "db user password"); if (args.length == 0) { printHelp(o);//ww w . j a v a2s .c om } return new GnuParser().parse(o, args); }
From source file:com.ery.dimport.daemon.DImportMasterStart.java
public int run(String args[]) throws Exception { Options opt = new Options();// ?? --param opt.addOption("debug", true, "debug type");// EStormConstant.DebugType CommandLine cmd;//from w ww .ja v a2s .c om try { cmd = new GnuParser().parse(opt, args); } catch (ParseException e) { LOG.error("Could not parse: ", e); usage(null); return 1; } // if (test() > 0) // return 1; String debug = cmd.getOptionValue("debug"); if (debug != null) { DImportConstant.DebugType = Integer.parseInt(debug); } List<String> remainingArgs = cmd.getArgList(); if (remainingArgs.size() < 1) { usage(null); return 1; } String command = remainingArgs.get(0); if (command.equals("start")) { return startMaster(); } else if (command.equals("stop")) { if (remainingArgs.size() == 1) { LOG.error("need taskId for stop order"); usage("need taskId for stop order"); } usage("??task"); String taskId = remainingArgs.get(1); return 0; } return 0; }