List of usage examples for org.apache.commons.cli OptionBuilder withArgName
public static OptionBuilder withArgName(String name)
From source file:PlyBounder.java
public static void main(String[] args) { // Get the commandline arguments Options options = new Options(); // Available options Option plyPath = OptionBuilder.withArgName("dir").hasArg() .withDescription("directory containing input .ply files").create("plyPath"); Option boundingbox = OptionBuilder.withArgName("string").hasArg() .withDescription("bounding box in WKT notation").create("boundingbox"); Option outputPlyFile = OptionBuilder.withArgName("file").hasArg().withDescription("output PLY file name") .create("outputPlyFile"); options.addOption(plyPath);/*www . j a v a 2 s. c om*/ options.addOption(boundingbox); options.addOption(outputPlyFile); String plydir = "."; String boundingboxstr = ""; String outputfilename = ""; CommandLineParser parser = new DefaultParser(); try { // parse the command line arguments CommandLine line = parser.parse(options, args); boundingboxstr = line.getOptionValue("boundingbox"); outputfilename = line.getOptionValue("outputPlyFile"); if (line.hasOption("plyPath")) { // print the value of block-size plydir = line.getOptionValue("plyPath"); System.out.println("Using plyPath=" + plydir); } else { HelpFormatter formatter = new HelpFormatter(); formatter.printHelp("PlyBounder", options); } //System.out.println( "plyPath=" + line.getOptionValue( "plyPath" ) ); } catch (ParseException exp) { System.err.println("Error getting arguments: " + exp.getMessage()); } // input directory // Get list of files File dir = new File(plydir); //System.out.println("Getting all files in " + dir.getCanonicalPath()); List<File> files = (List<File>) FileUtils.listFiles(dir, new String[] { "ply", "PLY" }, false); for (File file : files) { try { System.out.println("file=" + file.getCanonicalPath()); } catch (IOException e) { e.printStackTrace(); } } String sometempfile = "magweg.wkt"; String s = null; // Loop through .ply files in directory for (File file : files) { try { String cmdl[] = { "./ply-tool.py", "intersection", file.getCanonicalPath(), boundingboxstr, sometempfile }; //System.out.println("Running: " + Arrays.toString(cmdl)); Process p = Runtime.getRuntime().exec(cmdl); BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream())); BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream())); // read the output from the command System.out.println("cmdout:\n"); while ((s = stdInput.readLine()) != null) { System.out.println(s); } // read any errors from the attempted command System.out.println("cmderr:\n"); while ((s = stdError.readLine()) != null) { System.out.println(s); } } catch (IOException e) { e.printStackTrace(); } } // Write new .ply file //ply-tool write setfile outputPlyFile try { String cmdl = "./ply-tool.py write " + sometempfile + " " + outputfilename; System.out.println("Running: " + cmdl); Process p = Runtime.getRuntime().exec(cmdl); BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream())); BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream())); // read the output from the command System.out.println("cmdout:\n"); while ((s = stdInput.readLine()) != null) { System.out.println(s); } // read any errors from the attempted command System.out.println("cmderr:\n"); while ((s = stdError.readLine()) != null) { System.out.println(s); } } catch (IOException e) { e.printStackTrace(); } // Done System.out.println("Done"); }
From source file:ant_ivy.Hello.java
public static void main(String[] args) throws Exception { Option msg = OptionBuilder.withArgName("msg").hasArg().withDescription("the message to capitalize") .create("message"); Options options = new Options(); options.addOption(msg);/*from w w w. j a v a 2 s. c o m*/ CommandLineParser parser = new GnuParser(); CommandLine line = parser.parse(options, args); String message = line.getOptionValue("message", "hello ivy !"); System.out.println("standard message : " + message); System.out.println( "capitalized by " + WordUtils.class.getName() + " : " + WordUtils.capitalizeFully(message)); }
From source file:cc.wikitools.lucene.FindWikipediaArticleId.java
@SuppressWarnings("static-access") public static void main(String[] args) throws Exception { Options options = new Options(); options.addOption(/*from w ww . j av a2 s .co m*/ OptionBuilder.withArgName("path").hasArg().withDescription("index location").create(INDEX_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(TITLE_OPTION) || !cmdline.hasOption(INDEX_OPTION)) { HelpFormatter formatter = new HelpFormatter(); formatter.printHelp(FindWikipediaArticleId.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 title = cmdline.getOptionValue(TITLE_OPTION); PrintStream out = new PrintStream(System.out, true, "UTF-8"); WikipediaSearcher searcher = new WikipediaSearcher(indexLocation); int id = searcher.getArticleId(title); out.println(title + ": id = " + id); searcher.close(); out.close(); }
From source file:cc.twittertools.index.ExtractTweetidsFromCollection.java
@SuppressWarnings("static-access") public static void main(String[] args) throws Exception { Options options = new Options(); options.addOption(OptionBuilder.withArgName("dir").hasArg().withDescription("source collection directory") .create(COLLECTION_OPTION)); CommandLine cmdline = null;/*ww w. ja v 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(COLLECTION_OPTION)) { HelpFormatter formatter = new HelpFormatter(); formatter.printHelp(ExtractTweetidsFromCollection.class.getName(), options); System.exit(-1); } String collectionPath = cmdline.getOptionValue(COLLECTION_OPTION); File file = new File(collectionPath); if (!file.exists()) { System.err.println("Error: " + file + " does not exist!"); System.exit(-1); } StatusStream stream = new JsonStatusCorpusReader(file); Status status; while ((status = stream.next()) != null) { System.out.println(status.getId() + "\t" + status.getScreenname()); } }
From source file:edu.upc.eetac.dsa.exercices.java.lang.exceptions.App.java
public static void main(String[] args) throws FileNotFoundException { String filename = null;//from www. j av a 2 s . 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: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 a va 2 s .com 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(); }
From source file:com.temenos.interaction.rimdsl.generator.launcher.Main.java
public static void main(String[] args) { // handle command line options final Options options = new Options(); OptionBuilder.withArgName("src"); OptionBuilder.withDescription("Model source"); OptionBuilder.hasArg();//from w ww . j a v a 2 s . c o m OptionBuilder.isRequired(); OptionBuilder.withValueSeparator(' '); Option optSrc = OptionBuilder.create("src"); OptionBuilder.withArgName("targetdir"); OptionBuilder.withDescription("Generator target directory"); OptionBuilder.hasArg(); OptionBuilder.isRequired(); OptionBuilder.withValueSeparator(' '); Option optTargetDir = OptionBuilder.create("targetdir"); options.addOption(optSrc); options.addOption(optTargetDir); // create the command line parser final CommandLineParser parser = new GnuParser(); CommandLine line = null; try { line = parser.parse(options, args); } catch (final ParseException exp) { System.err.println("Parsing arguments failed. Reason: " + exp); wrongCall(options); return; } // execute the generator Injector injector = new RIMDslStandaloneSetup().createInjectorAndDoEMFRegistration(); Generator generator = injector.getInstance(Generator.class); File srcFile = new File(line.getOptionValue(optSrc.getArgName())); if (srcFile.exists()) { boolean result = false; if (srcFile.isDirectory()) { result = generator.runGeneratorDir(srcFile.getPath(), line.getOptionValue(optTargetDir.getArgName())); } else { result = generator.runGenerator(srcFile.getPath(), line.getOptionValue(optTargetDir.getArgName())); } System.out.println("Code generation finished [" + result + "]"); } else { System.out.println("Src dir not found."); } }
From source file:com.temenos.interaction.rimdsl.generator.launcher.MainSpringPRD.java
public static void main(String[] args) { // handle command line options final Options options = new Options(); OptionBuilder.withArgName("src"); OptionBuilder.withDescription("Model source"); OptionBuilder.hasArg();// w w w . j av a2 s . c o m OptionBuilder.isRequired(); OptionBuilder.withValueSeparator(' '); Option optSrc = OptionBuilder.create("src"); OptionBuilder.withArgName("targetdir"); OptionBuilder.withDescription("Generator target directory"); OptionBuilder.hasArg(); OptionBuilder.isRequired(); OptionBuilder.withValueSeparator(' '); Option optTargetDir = OptionBuilder.create("targetdir"); options.addOption(optSrc); options.addOption(optTargetDir); // create the command line parser final CommandLineParser parser = new GnuParser(); CommandLine line = null; try { line = parser.parse(options, args); } catch (final ParseException exp) { System.err.println("Parsing arguments failed. Reason: " + exp); wrongCall(options); return; } // execute the generator Injector injector = new RIMDslStandaloneSetupSpringPRD().createInjectorAndDoEMFRegistration(); Generator generator = injector.getInstance(Generator.class); File srcFile = new File(line.getOptionValue(optSrc.getArgName())); if (srcFile.exists()) { boolean result = false; if (srcFile.isDirectory()) { result = generator.runGeneratorDir(srcFile.getPath(), line.getOptionValue(optTargetDir.getArgName())); } else { result = generator.runGenerator(srcFile.getPath(), line.getOptionValue(optTargetDir.getArgName())); } System.out.println("Code generation finished [" + result + "]"); } else { System.out.println("Src dir not found."); } }
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 .jav a 2 s . co 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)); 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:eu.fbk.dkm.sectionextractor.PageClassMerger.java
public static void main(String args[]) throws IOException { CommandLineWithLogger commandLineWithLogger = new CommandLineWithLogger(); commandLineWithLogger.addOption(OptionBuilder.withArgName("file").hasArg() .withDescription("WikiData ID file").isRequired().withLongOpt("wikidata-id").create("i")); commandLineWithLogger.addOption(OptionBuilder.withArgName("file").hasArg() .withDescription("Airpedia Person file").isRequired().withLongOpt("airpedia").create("a")); commandLineWithLogger.addOption(OptionBuilder.withArgName("file").hasArg().withDescription("Output file") .isRequired().withLongOpt("output").create("o")); CommandLine commandLine = null;/*w ww . j a v a 2 s .c o m*/ try { commandLine = commandLineWithLogger.getCommandLine(args); PropertyConfigurator.configure(commandLineWithLogger.getLoggerProps()); } catch (Exception e) { System.exit(1); } String wikiIDFileName = commandLine.getOptionValue("wikidata-id"); String airpediaFileName = commandLine.getOptionValue("airpedia"); String outputFileName = commandLine.getOptionValue("output"); HashMap<Integer, String> wikiIDs = new HashMap<>(); HashSet<Integer> airpediaClasses = new HashSet<>(); List<String> strings; logger.info("Loading file " + wikiIDFileName); strings = Files.readLines(new File(wikiIDFileName), Charsets.UTF_8); for (String line : strings) { line = line.trim(); if (line.length() == 0) { continue; } if (line.startsWith("#")) { continue; } String[] parts = line.split("\t"); if (parts.length < 2) { continue; } int id; try { id = Integer.parseInt(parts[0]); } catch (Exception e) { continue; } wikiIDs.put(id, parts[1]); } logger.info("Loading file " + airpediaFileName); strings = Files.readLines(new File(airpediaFileName), Charsets.UTF_8); for (String line : strings) { line = line.trim(); if (line.length() == 0) { continue; } if (line.startsWith("#")) { continue; } String[] parts = line.split("\t"); if (parts.length < 2) { continue; } int id; try { id = Integer.parseInt(parts[0]); } catch (Exception e) { continue; } airpediaClasses.add(id); } logger.info("Saving information"); BufferedWriter writer = new BufferedWriter(new FileWriter(outputFileName)); for (int i : wikiIDs.keySet()) { if (!airpediaClasses.contains(i)) { continue; } writer.append(wikiIDs.get(i)).append("\n"); } writer.close(); }