Thursday, January 05, 2006

args4j - Command Line Argument with Annotaion

หนุ่มญี่ปุ่นคนนี้มี idea ในการเอา Annotaion มาใช้ได้เหมาะมาก
ลองดูตัวอย่าง code เขา
import org.kohsuke.args4j.Argument;
import org.kohsuke.args4j.CmdLineException;
import org.kohsuke.args4j.CmdLineParser;
import org.kohsuke.args4j.Option;

import java.util.ArrayList;
import java.util.List;

* Sample program that shows how you can use args4j.
* @author
* Kohsuke Kawaguchi (
public class SampleMain {

@Option(name="-r",usage="recursively run something")
private boolean recursive;

@Option(name="-o",usage="output to this file",metaVar="OUTPUT")
private File out = new File(".");

@Option(name="-str") // no usage
private String str = "(default value)";

@Option(name="-n",usage="repeat <n> times\nusage can have new lines in it an
d also it can be verrrrrrrrrrrrrrrrrry long")
private int num = -1;

// receives other command line parameters than options
private List<String> arguments = new ArrayList<String>();

public static void main(String[] args) throws IOException {
new SampleMain().doMain(args);

public void doMain(String[] args) throws IOException {
CmdLineParser parser = new CmdLineParser(this);

try {
// parse the arguments.

// you can parse additional arguments if you want.
// parser.parseArgument("more","args");

// after parsing arguments, you should check
// if enough arguments are given.
if( arguments.isEmpty() )
throw new CmdLineException("No argument is given");

} catch( CmdLineException e ) {
// if there's a problem in the command line,
// you'll get this exception. this will report
// an error message.
System.err.println("java SampleMain [options...] arguments...");

// this will redirect the output to the specified output

if( recursive )
System.out.println("-r flag is set");

System.out.println("-str was "+str);

if( num>=0 )
System.out.println("-n was "+num);

// access non-option arguments
System.out.println("other arguments are:");
for( String s : arguments )

เทียบกับ jakarta common CLI
// create the command line parser
CommandLineParser parser = new PosixParser();

// create the Options
Options options = new Options();
options.addOption( "a", "all", false, "do not hide entries starting with ." );
options.addOption( "A", "almost-all", false, "do not list implied . and .." );
options.addOption( "b", "escape", false, "print octal escapes for nongraphic "
+ "characters" );
options.addOption( OptionBuilder.withLongOpt( "block-size" )
.withDescription( "use SIZE-byte blocks" )
.withValueSeparator( '=' )
.create() );
options.addOption( "B", "ignore-backups", false, "do not list implied entried "
+ "ending with ~");
options.addOption( "c", false, "with -lt: sort by, and show, ctime (time of last
+ "modification of file status information) with
+ "-l:show ctime and sort by name otherwise: sort
+ "by ctime" );
options.addOption( "C", false, "list entries by columns" );

String[] args = new String[]{ "--block-size=10" };

try {
// parse the command line arguments
CommandLine line = parser.parse( options, args );

// validate that block-size has been set
if( line.hasOption( "block-size" ) ) {
// print the value of block-size
System.out.println( line.getOptionValue( "block-size" ) );
catch( ParseException exp ) {
System.out.println( "Unexpected exception:" + exp.getMessage() );

Related link from Roti

No comments: