|
import org.apache.commons.cli.BasicParser;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
/**
* 对命令行处理的一个开源包
*/
public class Test {
public static void main(String[] args) {
try {
Options opts = new Options();
opts.addOption("h", false, "Print help for this application");
opts.addOption("u", true, "The username to use");
opts.addOption("dsn", true, "The data source to use");
BasicParser parser = new BasicParser();
CommandLine cl = parser.parse(opts, args);
//可以在IDE中运行时,传 "-h"
if (cl.hasOption('h')) {
HelpFormatter hf = new HelpFormatter();
hf.printHelp("OptionsTip", opts);
} else {
//可以在IDE中运行,传 "-u AAA -dsn BBB"
System.out.println(cl.getOptionValue("u"));
System.out.println(cl.getOptionValue("dsn"));
}
} catch (ParseException pe) {
pe.printStackTrace();
}
}
} |
|
|