package me.chaosdefinition.hypercube;

import java.util.List;

import org.kohsuke.args4j.CmdLineException;
import org.kohsuke.args4j.CmdLineParser;
import org.kohsuke.args4j.Option;

/**
 * A command line argument parser for hypercube.
 *
 * @author Chaos Shen
 */
public class Options {

	@Option(name = "-d",
			aliases = "--dimension",
			metaVar = "DIM",
			usage = "specify dimension",
			required = true)
	private int dimension;

	@Option(name = "-i",
			aliases = "--initial",
			metaVar = "LIST",
			usage = "specify initial mapping",
			handler = IntegerArrayOptionHandler.class)
	private List<Integer> initial;

	@Option(name = "-s",
			aliases = "--start",
			metaVar = "START",
			usage = "set starting position")
	private Integer start;

	@Option(name = "-e",
			aliases = "--end",
			metaVar = "END",
			usage = "set ending position")
	private Integer end;

	@Option(name = "-v",
			aliases = "--verbose",
			usage = "verbose mode")
	private boolean verbose = false;

	@Option(name = "-h",
			aliases = "--help",
			usage = "show this message and exit",
			help = true)
	private boolean help;

	/**
	 * Parses the command line arguments and stores the results in its fields.
	 *
	 * @param args
	 *            the original command line arguments
	 */
	public void parse(String[] args) {
		CmdLineParser parser = new CmdLineParser(this);

		try {
			parser.parseArgument(args);
			if (help) {
				System.out.println("Available options:");
				System.out.println();
				parser.printUsage(System.out);
				System.exit(0);
			}
		} catch (CmdLineException e) {
			throw new HypercubeException("Failed to parse cmd line!", e);
		}
	}

	public int getDimension() {
		return dimension;
	}

	public List<Integer> getInitial() {
		return initial;
	}

	public Integer getStart() {
		return start;
	}

	public Integer getEnd() {
		return end;
	}

	public boolean isVerbose() {
		return verbose;
	}
}

@@ -32,16 +32,82 @@
 rcube;%0A%0A
+import java.io.FileNotFoundException;%0Aimport java.io.PrintStream;%0A
 import j
@@ -121,16 +121,16 @@
 l.List;%0A
-
 %0Aimport 
@@ -931,16 +931,143 @@
 r end;%0A%0A
+%09@Option(name = %22-o%22,%0A%09%09%09aliases = %22--output%22,%0A%09%09%09metaVar = %22FILE%22,%0A%09%09%09usage = %22specify output file%22)%0A%09private String output;%0A%0A
 %09@Option
@@ -1699,16 +1699,16 @@
 m.out);%0A
-
 %09%09%09%09Syst
@@ -1724,16 +1724,203 @@
 );%0A%09%09%09%7D%0A
+%09%09%09if (output != null) %7B%0A%09%09%09%09try %7B%0A%09%09%09%09%09System.setOut(new PrintStream(output));%0A%09%09%09%09%7D catch (FileNotFoundException e) %7B%0A%09%09%09%09%09throw new HypercubeException(%22File not found%22, e);%0A%09%09%09%09%7D%0A%09%09%09%7D%0A
 %09%09%7D catc

Add a new option '-o' / '--output'.