C的getopt的就不说了,java中也有类似的东西。需要一个JAR包
http://www.urbanophile.com/arenn/hacking/download.html
简单例程
- public static void main(String[] args) {
- args = new String[]{ "-a", "1" };
- gnu.getopt.Getopt getopt = new Getopt("app", args, "a:");
- int c = getopt.getopt();
- switch (c){
- case 'a':
- System.out.println("a:" + getopt.getOptarg());
- break;
- default:
- System.out.println(c);
- }
- }
输出:
a:1