Java命令行参数解析工具包:CmdOption

jopen 11年前

CmdOption 是一个用于Java5应用程序的注解驱动的简单命令行参数解析工具包,你所需要做的就是简单配置对象,每个字段和方法通过注解来定义。

示例代码:

package org.example;    import java.util.*;  import de.tototec.cmdoption.*;    public class Main {      public static class Config {      @CmdOption(names = {"--help", "-h"}, description = "Show this help.", isHelp = true)      public boolean help;        @CmdOption(names = {"--verbose", "-v"}, description = "Be more verbose.")      private boolean verbose;        @CmdOption(names = {"--options", "-o"}, args = {"name", "value"}, maxCount = -1, description = "Additional options when processing names.")      private final Map<String, String> options = new LinkedHashMap<String, String>();        @CmdOption(args = {"file"}, description = "Names to process.", minCount = 1, maxCount = -1)      private final List<String> names = new LinkedList<String>();    }      public static void main(String[] args) {      Config config = new Config();      CmdlineParser cp = new CmdlineParser(config);      cp.setResourceBundle(Main.class.getPackage().getName() + ".Messages", Main.class.getClassLoader());      cp.setProgramName("myprogram");      cp.setAboutLine("Example names processor v1.0");        try {        cp.parse(args);      } catch (CmdlineParserException e) {        System.err.println("Error: " + e.getLocalizedMessage() + "\nRun myprogram --help for help.");        System.exit(1);      }        if (config.help) {        cp.usage();        System.exit(0);      }        // ...    }  }

项目主页:http://www.open-open.com/lib/view/home/1353905551481