public class CmdLineArgsTest {
private static void usage() {
System.out.println("CmdLineArgsTest [-h ]");
System.out.println(" [-i ] [-v ]");
System.exit(-1);
}
/* 命令行参数解析 */
private static void parseArgs(String args[]) {
for (int i = 0; i < args.length; ++i) {
if (args[i].startsWith("-")) {
switch (args[i].charAt(1)) {
case 'h':
System.out.println(args[++i]);
break;
case 'i':
System.out.println(args[++i]);
break;
case 'v':
System.out.println(args[++i]);
break;
default:
usage();
}
}
}
}
/**
* @param args
*/
public static void main(String[] args) {
parseArgs(args);
}
}
输出结果
Environment
Inventory
Vendor
解析:
args String[6]
[0] "-h"
[1] "Environment"
[2] "-i"
[3] "Inventory"
[4] "-v"
[5] "Vendor"
阅读(1504) | 评论(1) | 转发(0) |