介绍 命令行参数解析、应用程序配置和日志记录,作为一个应用程序的骨架,随处可见。因此,Apache软件组织开发出了一套通用的类库,用来帮助软件开发人员完成这些“骨架”的建立。其中: •Commons CLI用于命令行解析 •Commons Configuration用于读取properties格式或者XML格式的配置信息 •Commons Logging和Log4J用来提供日志支持。 这些通用的类库都在http://jakarta.apache.org/commons/index.html网址上提供下载 •使用Commons CLI解析简单的命令行 问题: 需要解析一段简单的命令行,包括可选和必选的命令行参数。 解决方案:
public static void main(String[] args) throws ParseException {//自己替换[]
Options options = new Options();
options.addOption(
"h", // short single-character name of the option
"help", // long multi-character name of the option
false, // flag signally if an argument is required after this option
"Print this usage information" // self-documenting description
);
options.addOption("v", "verbose", false, "Print out VERBOSE information");
options.addOption("f", "file", true, "File to save program output to");
// parse the program arguments
CommandLineParser parser = new BasicParser();
CommandLine commandLine = parser.parse(
options, // the specified Options
args // the command line arguments
);
if (commandLine.hasOption('h')) {
System.out.println("Help Message");
System.exit(0);
}
boolean verbose = false;
if (commandLine.hasOption('v')) {
verbose = true;
}
if (commandLine.hasOption("f")) {
System.out.println(commandLine.getOptionValue('f'));
}
}
创建Options对象,配置命令行格式信息,提供给CommandLineParser解析时参考。同时传递给CommandLineParser的还有String[]类型的命令行参数。 解析完成后,CommandLineParser会返回一个CommandLine对象,CommandLine对象会把提供的命令行参数封装起来,提供相应的检测和访问参数的方法。 其他: Commons CLI库提供了两个解析器,实现了CommandLineParser接口: •BasicParser 基于POSIX标准的命令行解析 •GNUParser 基于GNU标准的命令行解析 关于这两种解析格式的介绍,请参照相关文档。 •使用Commons CLI解析复杂命令行 问题: 需要解析有两个互斥参数的命令行。 解决方案:
public static void main(String[] args) throws ParseException {{//自己替换[]
Options options = new Options();
options.addOption(
"h", // short single-character name of the option
"help", // long multi-character name of the option
false, // flag signally if an argument is required after this option
"Print this usage information" // self-documenting description
);
options.addOption("v", "verbose", false, "Print out VERBOSE information");
OptionGroup group = new OptionGroup(); // A group of mutually exclusive options.
group.addOption(OptionBuilder.hasArg(
true // if true then the Option created will require an argument value
).create(
'f' // the character representation of the Option
));
group.addOption(OptionBuilder.hasArg(true).create('m'));
options.addOptionGroup(group);
CommandLineParser parser = new BasicParser();
CommandLine commandLine = parser.parse(options, args);
// ...
}
Options对象可以使用addOptionGroup方法添加一组由OptionGroup表示的互斥选项。OptionGroup是一组Option的容器,它们之间是互斥的,命令行里不可以同时存在。使用这种方式来管理互斥选项,比使用StringTokenizer来手动编码要轻便多了。 其他: OptionBuilder可以使用一系列方法调用的方式来创建一个Option,例如: OptionBuilder.hasArgs(true).isRequired(true).create('b') •当命令行格式出错时,显示帮助信息 问题: 需要提示给用户命令行可用选项的格式化列表。 解决方案: 可以借助HelpFormatter对象打印格式化的帮助信息。
public static void main(String[] args) {//自己替换[]
Options options = new Options();
options.addOption(
"h", // short single-character name of the option
"help", // long multi-character name of the option
false, // flag signally if an argument is required after this option
"Print this usage information" // self-documenting description
);
options.addOption("v", "verbose", false, "Print out VERBOSE information");
OptionGroup group = new OptionGroup(); // A group of mutually exclusive options.
group.addOption(
OptionBuilder.hasArg(
true // if true then the Option created will require an argument value
).withArgName(
"file" // the name for the argument value
).withLongOpt(
"file" // the long option value
).withDescription(
"" // a description of the Option's purpose
).create(
'f' // the character representation of the Option
)
);
group.addOption(OptionBuilder.hasArg(true).withArgName("email").withLongOpt("email").withDescription("").create('m'));
options.addOptionGroup(group);
CommandLineParser parser = new BasicParser();
try {
CommandLine commandLine = parser.parse(options, args);
if (commandLine.hasOption('h')) {
printUsage(options);
System.exit(0);
}
} catch (ParseException e) {
System.out.println("You provided bad program arguments!");
printUsage(options);
System.exit(1);
}
}
private static void printUsage(Options options) {
HelpFormatter helpFormatter = new HelpFormatter();
helpFormatter.printHelp(
"【-h】 【-v】 【-f <file> | -m <email>】", // usage: cmdLineSyntax,自己替换【】
"SomeApp - A fancy and expensive program, Copyright 2010 Blah.", // header
options, // options
"For more instructions, see our website at: http://www.blah123.org" // footer
);
}
其他: HelpFormatter可以接收四种参数: •命令行语法字符串 usage string,即对命令行选项用法的简短描述 •信息头 •Options对象 •信息尾 翻译作者: 与java共舞-稀饭 |