import org.kohsuke.args4j.Argument; import org.kohsuke.args4j.CmdLineException; import org.kohsuke.args4j.CmdLineParser; import org.kohsuke.args4j.Option; import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.List; /** * Sample program that shows how you can use args4j. * * @author * Kohsuke Kawaguchi (kk@kohsuke.org) */ 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 and also it can be verrrrrrrrrrrrrrrrrry long") private int num = -1; // receives other command line parameters than options @Argument 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. parser.parseArgument(args); // 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(e.getMessage()); System.err.println("java SampleMain [options...] arguments..."); parser.printUsage(System.err); return; } // this will redirect the output to the specified output System.out.println(out); 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 ) System.out.println(s); } }