List of usage examples for org.apache.commons.cli HelpFormatter HelpFormatter
HelpFormatter
From source file:edu.umd.cloud9.example.bigram.AnalyzeBigramCount.java
@SuppressWarnings({ "static-access" }) public static void main(String[] args) { Options options = new Options(); options.addOption(OptionBuilder.withArgName("path").hasArg().withDescription("input path").create(INPUT)); CommandLine cmdline = null;/*from www . ja va2s . c o m*/ 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(INPUT)) { System.out.println("args: " + Arrays.toString(args)); HelpFormatter formatter = new HelpFormatter(); formatter.setWidth(120); formatter.printHelp(AnalyzeBigramCount.class.getName(), options); ToolRunner.printGenericCommandUsage(System.out); System.exit(-1); } String inputPath = cmdline.getOptionValue(INPUT); System.out.println("input path: " + inputPath); List<PairOfWritables<Text, IntWritable>> bigrams = SequenceFileUtils.readDirectory(new Path(inputPath)); Collections.sort(bigrams, new Comparator<PairOfWritables<Text, IntWritable>>() { public int compare(PairOfWritables<Text, IntWritable> e1, PairOfWritables<Text, IntWritable> e2) { if (e2.getRightElement().compareTo(e1.getRightElement()) == 0) { return e1.getLeftElement().compareTo(e2.getLeftElement()); } return e2.getRightElement().compareTo(e1.getRightElement()); } }); int singletons = 0; int sum = 0; for (PairOfWritables<Text, IntWritable> bigram : bigrams) { sum += bigram.getRightElement().get(); if (bigram.getRightElement().get() == 1) { singletons++; } } System.out.println("total number of unique bigrams: " + bigrams.size()); System.out.println("total number of bigrams: " + sum); System.out.println("number of bigrams that appear only once: " + singletons); System.out.println("\nten most frequent bigrams: "); Iterator<PairOfWritables<Text, IntWritable>> iter = Iterators.limit(bigrams.iterator(), 10); while (iter.hasNext()) { PairOfWritables<Text, IntWritable> bigram = iter.next(); System.out.println(bigram.getLeftElement() + "\t" + bigram.getRightElement()); } }
From source file:com.yahoo.gondola.cli.GondolaAgent.java
public static void main(String[] args) throws ParseException, IOException { PropertyConfigurator.configure("conf/log4j.properties"); CommandLineParser parser = new DefaultParser(); Options options = new Options(); options.addOption("port", true, "Listening port"); options.addOption("config", true, "config file"); options.addOption("h", false, "help"); CommandLine commandLine = parser.parse(options, args); if (commandLine.hasOption("help")) { new HelpFormatter().printHelp("GondolaAgent", options); return;// w w w . j av a 2 s .c o m } if (commandLine.hasOption("port")) { port = Integer.parseInt(commandLine.getOptionValue("port")); } else { port = 1200; } if (commandLine.hasOption("config")) { configFile = commandLine.getOptionValue("config"); } config = new Config(new File(configFile)); logger.info("Initialize system, kill all gondola processes"); new DefaultExecutor().execute(org.apache.commons.exec.CommandLine.parse("bin/gondola-local-test.sh stop")); new GondolaAgent(port); }
From source file:com.tbodt.trp.TheRapidPermuter.java
/** * Main method for The Rapid Permuter./*from w w w.j av a 2 s. co m*/ * * @param args * @throws IOException */ public static void main(String[] args) throws IOException { CommandProcessor.processCommand("\"\": remove(\"\") in([words])").ifPresent(x -> x.forEach(y -> { })); // to load all the classes we need try { cmd = new BasicParser().parse(options, args); } catch (ParseException ex) { System.err.println(ex.getMessage()); return; } if (Arrays.asList(cmd.getOptions()).contains(help)) { // another way commons cli is severely broken new HelpFormatter().printHelp("trp", options); return; } if (cmd.hasOption('c')) doCommand(cmd.getOptionValue('c')); else { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); System.out.print("trp> "); String input; while ((input = in.readLine()) != null) { if (input.equals("exit")) return; // exit doCommand(input); System.out.print("trp> "); } } }
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 w w w . j av a2 s .co 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:com.subakva.formicid.Main.java
public static void main(final String[] args) { Options options = new Options(); options.addOption("p", "projecthelp", false, "print project help information"); options.addOption("f", "file", true, "use given buildfile"); options.addOption("h", "help", false, "print this message"); options.addOption("d", "debug", false, "print debugging information"); options.addOption("v", "verbose", false, "be extra verbose"); options.addOption("q", "quiet", false, "be extra quiet"); CommandLine cli;/*from w w w.j a v a2 s . c om*/ try { cli = new GnuParser().parse(options, args); } catch (ParseException e) { System.out.println("Error: " + e.getMessage()); new HelpFormatter().printHelp(CLI_SYNTAX, options); return; } String scriptName = cli.getOptionValue("f", "build.js"); if (cli.hasOption("h")) { new HelpFormatter().printHelp(CLI_SYNTAX, options); return; } else if (cli.hasOption("p")) { runScript(scriptName, "projecthelp", null); } else { runScript(scriptName, "build", cli); } }
From source file:list.Main.java
public static void main(String[] args) throws Exception { Options options = getOptions();//from w w w . j a v a2s . com try { CommandLineParser parser = new GnuParser(); CommandLine line = parser.parse(options, args); File dir = new File(line.getOptionValue("dir", ".")); Collection files = ListFile.list(dir); System.out.println("listing files in " + dir); for (Iterator it = files.iterator(); it.hasNext();) { System.out.println("\t" + it.next() + "\n"); } } catch (ParseException exp) { // oops, something went wrong System.err.println("Parsing failed. Reason: " + exp.getMessage()); HelpFormatter formatter = new HelpFormatter(); formatter.printHelp("list", options); } }
From source file:edu.umd.cloud9.example.bigram.AnalyzeBigramRelativeFrequencyJson.java
@SuppressWarnings({ "static-access" }) public static void main(String[] args) { Options options = new Options(); options.addOption(OptionBuilder.withArgName("path").hasArg().withDescription("input path").create(INPUT)); CommandLine cmdline = null;//from ww w .ja v a2 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(INPUT)) { System.out.println("args: " + Arrays.toString(args)); HelpFormatter formatter = new HelpFormatter(); formatter.setWidth(120); formatter.printHelp(AnalyzeBigramRelativeFrequencyJson.class.getName(), options); ToolRunner.printGenericCommandUsage(System.out); System.exit(-1); } String inputPath = cmdline.getOptionValue(INPUT); System.out.println("input path: " + inputPath); List<PairOfWritables<BigramRelativeFrequencyJson.MyTuple, FloatWritable>> pairs = SequenceFileUtils .readDirectory(new Path(inputPath)); List<PairOfWritables<BigramRelativeFrequencyJson.MyTuple, FloatWritable>> list1 = Lists.newArrayList(); List<PairOfWritables<BigramRelativeFrequencyJson.MyTuple, FloatWritable>> list2 = Lists.newArrayList(); for (PairOfWritables<BigramRelativeFrequencyJson.MyTuple, FloatWritable> p : pairs) { BigramRelativeFrequencyJson.MyTuple bigram = p.getLeftElement(); if (bigram.getJsonObject().get("Left").getAsString().equals("light")) { list1.add(p); } if (bigram.getJsonObject().get("Left").getAsString().equals("contain")) { list2.add(p); } } Collections.sort(list1, new Comparator<PairOfWritables<BigramRelativeFrequencyJson.MyTuple, FloatWritable>>() { public int compare(PairOfWritables<BigramRelativeFrequencyJson.MyTuple, FloatWritable> e1, PairOfWritables<BigramRelativeFrequencyJson.MyTuple, FloatWritable> e2) { if (e1.getRightElement().compareTo(e2.getRightElement()) == 0) { return e1.getLeftElement().compareTo(e2.getLeftElement()); } return e2.getRightElement().compareTo(e1.getRightElement()); } }); Iterator<PairOfWritables<BigramRelativeFrequencyJson.MyTuple, FloatWritable>> iter1 = Iterators .limit(list1.iterator(), 10); while (iter1.hasNext()) { PairOfWritables<BigramRelativeFrequencyJson.MyTuple, FloatWritable> p = iter1.next(); BigramRelativeFrequencyJson.MyTuple bigram = p.getLeftElement(); System.out.println(bigram + "\t" + p.getRightElement()); } Collections.sort(list2, new Comparator<PairOfWritables<BigramRelativeFrequencyJson.MyTuple, FloatWritable>>() { public int compare(PairOfWritables<BigramRelativeFrequencyJson.MyTuple, FloatWritable> e1, PairOfWritables<BigramRelativeFrequencyJson.MyTuple, FloatWritable> e2) { if (e1.getRightElement().compareTo(e2.getRightElement()) == 0) { return e1.getLeftElement().compareTo(e2.getLeftElement()); } return e2.getRightElement().compareTo(e1.getRightElement()); } }); Iterator<PairOfWritables<BigramRelativeFrequencyJson.MyTuple, FloatWritable>> iter2 = Iterators .limit(list2.iterator(), 10); while (iter2.hasNext()) { PairOfWritables<BigramRelativeFrequencyJson.MyTuple, FloatWritable> p = iter2.next(); BigramRelativeFrequencyJson.MyTuple bigram = p.getLeftElement(); System.out.println(bigram + "\t" + p.getRightElement()); } }
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;// w w w. j a va 2 s .c o m 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:edu.umd.cloud9.example.bigram.AnalyzeBigramRelativeFrequency.java
@SuppressWarnings({ "static-access" }) public static void main(String[] args) { Options options = new Options(); options.addOption(OptionBuilder.withArgName("path").hasArg().withDescription("input path").create(INPUT)); CommandLine cmdline = null;/*from w w w . j av a2 s. c o m*/ 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(INPUT)) { System.out.println("args: " + Arrays.toString(args)); HelpFormatter formatter = new HelpFormatter(); formatter.setWidth(120); formatter.printHelp(AnalyzeBigramRelativeFrequency.class.getName(), options); ToolRunner.printGenericCommandUsage(System.out); System.exit(-1); } String inputPath = cmdline.getOptionValue(INPUT); System.out.println("input path: " + inputPath); List<PairOfWritables<PairOfStrings, FloatWritable>> pairs = SequenceFileUtils .readDirectory(new Path(inputPath)); List<PairOfWritables<PairOfStrings, FloatWritable>> list1 = Lists.newArrayList(); List<PairOfWritables<PairOfStrings, FloatWritable>> list2 = Lists.newArrayList(); for (PairOfWritables<PairOfStrings, FloatWritable> p : pairs) { PairOfStrings bigram = p.getLeftElement(); if (bigram.getLeftElement().equals("light")) { list1.add(p); } if (bigram.getLeftElement().equals("contain")) { list2.add(p); } } Collections.sort(list1, new Comparator<PairOfWritables<PairOfStrings, FloatWritable>>() { public int compare(PairOfWritables<PairOfStrings, FloatWritable> e1, PairOfWritables<PairOfStrings, FloatWritable> e2) { if (e1.getRightElement().compareTo(e2.getRightElement()) == 0) { return e1.getLeftElement().compareTo(e2.getLeftElement()); } return e2.getRightElement().compareTo(e1.getRightElement()); } }); Iterator<PairOfWritables<PairOfStrings, FloatWritable>> iter1 = Iterators.limit(list1.iterator(), 10); while (iter1.hasNext()) { PairOfWritables<PairOfStrings, FloatWritable> p = iter1.next(); PairOfStrings bigram = p.getLeftElement(); System.out.println(bigram + "\t" + p.getRightElement()); } Collections.sort(list2, new Comparator<PairOfWritables<PairOfStrings, FloatWritable>>() { public int compare(PairOfWritables<PairOfStrings, FloatWritable> e1, PairOfWritables<PairOfStrings, FloatWritable> e2) { if (e1.getRightElement().compareTo(e2.getRightElement()) == 0) { return e1.getLeftElement().compareTo(e2.getLeftElement()); } return e2.getRightElement().compareTo(e1.getRightElement()); } }); Iterator<PairOfWritables<PairOfStrings, FloatWritable>> iter2 = Iterators.limit(list2.iterator(), 10); while (iter2.hasNext()) { PairOfWritables<PairOfStrings, FloatWritable> p = iter2.next(); PairOfStrings bigram = p.getLeftElement(); System.out.println(bigram + "\t" + p.getRightElement()); } }
From source file:io.wcm.devops.conga.tooling.cli.CongaCli.java
/** * CLI entry point//from ww w .j a va 2s . c o m * @param args Command line arguments * @throws Exception */ //CHECKSTYLE:OFF public static void main(String[] args) throws Exception { //CHECKSTYLE:ON CommandLine commandLine = new DefaultParser().parse(CLI_OPTIONS, args); if (commandLine.hasOption("?")) { HelpFormatter formatter = new HelpFormatter(); formatter.setWidth(150); formatter.printHelp("java -cp io.wcm.devops.conga.tooling.cli-<version>.jar " + "io.wcm.devops.conga.tooling.cli.CongaCli <arguments>", CLI_OPTIONS); return; } String templateDir = commandLine.getOptionValue("templateDir", "templates"); String roleDir = commandLine.getOptionValue("roleDir", "roles"); String environmentDir = commandLine.getOptionValue("environmentDir", "environments"); String targetDir = commandLine.getOptionValue("target", "target"); String[] environments = StringUtils.split(commandLine.getOptionValue("environments", null), ","); ResourceLoader resourceLoader = new ResourceLoader(); List<ResourceCollection> roleDirs = ImmutableList .of(resourceLoader.getResourceCollection(ResourceLoader.FILE_PREFIX + roleDir)); List<ResourceCollection> templateDirs = ImmutableList .of(resourceLoader.getResourceCollection(ResourceLoader.FILE_PREFIX + templateDir)); List<ResourceCollection> environmentDirs = ImmutableList .of(resourceLoader.getResourceCollection(ResourceLoader.FILE_PREFIX + environmentDir)); File targetDirecotry = new File(targetDir); Generator generator = new Generator(roleDirs, templateDirs, environmentDirs, targetDirecotry); generator.setDeleteBeforeGenerate(true); generator.generate(environments); }