When Brains Fly

An Awesome Blog About Neuroscience, Computer Science, and Technology

args4j is Awesome

[ May 28th, 2009 ]

Today, I got frustrated writing a command-line input parser for my Java program for lab, so I looked for one on the web. Surely a task so common must have some good libraries that take care of it easily. I was not disappointed.

Apache Commons has one, but it was kind of annoying. It left a lot for me to do. I kept looking and found args4j and thought it looked really neat. It makes good use of Java 5 annotations, which I have seen in action when writing a class JSF project. I tried it out, and after a little trouble learning what the annotation parameters meant (hint: see the javadoc for @Option), and with the help of Eclipse’s automatic getter generator, I got my command line parser up and running in minutes! Ah, the magic.

Here is the code, simple and sweet:

CommandLineValues.java:

import java.util.Random;

import org.kohsuke.args4j.Argument;
import org.kohsuke.args4j.Option;

public class CommandLineValues {

	/** Max value for the random seed (minimum is 0). */
	public static final int MAX_RANDOM_SEED = 1000;

	/** Valid options for the experiment type */
	public static enum ExptOption {
		DEGREES,
		FEATURES
	};

	@Argument(required = true, index = 0, usage = "type of experiment")
	private ExptOption exptOption;

	@Argument(required = true, index = 1, metaVar = "FILE",
			usage = "the config file for the experiment")
	private String configFileName;

	@Option(name = "-r", aliases = { "--resultsDir" }, metaVar = "DIR",
			usage = "directory to place the results file")
	private String resultsDir = "results/";

	@Option(name = "-s", aliases = { "--seed" }, metaVar = "VALUE",
			usage = "seed to use for the program's random number"
			 + generator")
	private int seed = (new Random()).nextInt(MAX_RANDOM_SEED + 1);

	// getters for all of the fields
}

The first and second arguments (exptOption and configFileName) are required; their order is set by the index parameter. The other two arguments are optional and have both a short single-dash flag (the name parameter) and a longer double-dash flag (the aliases parameter, which can contain any number of strings). The metaVar parameter describes the type of the argument in short, and the usage parameter describes the value of the argument.

Here is code showing how CommandLineValues.java is used.

Main.java:

public static void main(String[] args) {

	// parse the command line arguments and options
	CommandLineValues values = new CommandLineValues();
	CmdLineParser parser = new CmdLineParser(values);
	parser.setUsageWidth(80); // width of the error display area

	try {
		parser.parseArgument(args);
	} catch (CmdLineException e) {
		System.err.println(e.getMessage());
		System.err.println("java DotsMain [options...] arguments...");
		// print the list of available options
		parser.printUsage(System.err);
		System.err.println();
		System.exit(1);
	}
	// use the getters
}

Awesome! =)

Leave a Comment!

Proudly powered by WordPress. Theme developed with WordPress Theme Generator.
Copyright © When Brains Fly. All rights reserved.