package ch.tkuhn.memetools;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.GnuParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;

public class CalculateMemeScores {

	private static Options options;

	static {
		options = new Options();
		options.addOption("g", true, "Use <arg>-grams");
		options.addOption("y", true, "Calculate scores for year <arg>");
		options.addOption("n", true, "Set n parameter to <arg>");
	}

	public static final void main(String[] args) {
		CommandLine cmd = null;
		try {
			cmd = (new GnuParser()).parse(options, args);
		} catch (ParseException ex) {
			System.err.println("ERROR: " + ex.getMessage());
			printHelp();
			System.exit(1);
		}
		if (cmd.getArgList().size() == 0) {
			System.err.println("ERROR: Specify input file");
			printHelp();
			System.exit(1);
		}
		if (cmd.getArgList().size() > 1) {
			System.err.println("ERROR: Too many arguments");
			printHelp();
			System.exit(1);
		}
		// ...
	}

	public static void printHelp() {
		HelpFormatter formatter = new HelpFormatter();
		formatter.printHelp("CalculateMemeScores <options> <inputfile>", options);
	}

}

@@ -1043,178 +1043,1378 @@
 %7D%0A%09%09
-// ...%0A%09%7D%0A%0A%09public static void printHelp() %7B%0A%09%09HelpFormatter formatter = new HelpFormatter();%0A%09%09formatter.printHelp(%22CalculateMemeScores %3Coptions%3E %3Cinputfile%3E%22, options);
+String gramString = cmd.getOptionValue(%22g%22, %221%22);%0A%09%09int gram = 1;%0A%09%09if (gramString.matches(%22%5B1-9%5D%22)) %7B%0A%09%09%09gram = Integer.parseInt(gramString);%0A%09%09%7D else %7B%0A%09%09%09System.err.println(%22ERROR: -g has to be an integer between 1 and 9%22);%0A%09%09%09printHelp();%0A%09%09%09System.exit(1);%0A%09%09%7D%0A%09%09String nString = cmd.getOptionValue(%22g%22, %221%22);%0A%09%09int n = 1;%0A%09%09if (nString.matches(%22%5B0-9%5D%7B1,3%7D%22)) %7B%0A%09%09%09n = Integer.parseInt(nString);%0A%09%09%7D else %7B%0A%09%09%09System.err.println(%22ERROR: -g has to be an integer between 0 and 999%22);%0A%09%09%09printHelp();%0A%09%09%09System.exit(1);%0A%09%09%7D%0A%09%09String yearString = cmd.getOptionValue(%22y%22);%0A%09%09Integer year = null;%0A%09%09if (yearString != null) %7B%0A%09%09%09if (yearString.matches(%22%5B0-9%5D%7B1,4%7D%22)) %7B%0A%09%09%09%09year = Integer.parseInt(yearString);%0A%09%09%09%7D else %7B%0A%09%09%09%09System.err.println(%22ERROR: -y has to be an integer between 0 and 9999%22);%0A%09%09%09%09printHelp();%0A%09%09%09%09System.exit(1);%0A%09%09%09%7D%0A%09%09%7D%0A%0A%09%09CalculateMemeScores c = new CalculateMemeScores(gram, n, year);%0A%09%09c.run();%0A%09%7D%0A%0A%09public static void printHelp() %7B%0A%09%09HelpFormatter formatter = new HelpFormatter();%0A%09%09formatter.printHelp(%22CalculateMemeScores %3Coptions%3E %3Cinputfile%3E%22, options);%0A%09%7D%0A%0A%09private int gram;%0A%09private int n;%0A%09private Integer year;%0A%0A%09public CalculateMemeScores(int gram, int n, Integer year) %7B%0A%09%09this.gram = gram;%0A%09%09this.n = n;%0A%09%09this.year = year;%0A%09%7D%0A%0A%09public CalculateMemeScores(int gram, int n) %7B%0A%09%09this(gram, n, null);%0A%09%7D%0A%0A%09public void run() %7B%0A%09%09// ...
 %0A%09%7D%0A

Read and parse command line parameters