Java构建命令行启动模式CommandLineParser/Options

匿名 (未验证) 提交于 2019-12-02 21:53:52
public void parseArgs(String[] args) throws ParseException {     // Create a Parser     CommandLineParser parser = new BasicParser( );     Options options = new Options( );     options.addOption("h", "help", false, "Print this usage information");     options.addOption("c", "cfg", true, "config Absolute Path");     options.addOption("l", "log", true, "log configuration");      // Parse the program arguments     CommandLine commandLine = parser.parse( options, args );     // Set the appropriate variables based on supplied options      if( commandLine.hasOption('h') ) {         printHelpMessage();         System.exit(0);     }     if( commandLine.hasOption('c') ) {         cfg = new File(commandLine.getOptionValue('c'));     } else {         printHelpMessage();         System.exit(0);     }     if( commandLine.hasOption('l') ) {         log = new File(commandLine.getOptionValue('l'));     } else {         printHelpMessage();         System.exit(0);     } } public void printHelpMessage() {     System.out.println( "Change the xml File and Log.XML Path to the right Absolute Path base on your project Location in your computor");     System.out.println("Usage example: ");     System.out.println( "java -cfg D:\\MyProject\\face2face\\logic\\src\\main\\resources\\logic.xml  -log D:\\MyProject\\face2face\\logic\\src\\main\\resources\\log.xml");     System.exit(0); }
Options options = new Options();
options.addOption("t",false,"display current time");
options.addOption("c",true,"country code");
CommandLineParser parser = new DefaultParser();
CommandLine cmd = parser.parse( options, args);
if(cmd.hasOption("t")) {
}else{
System.out.println((new SimpleDateFormat("yyyy-MM-dd")).format(new Date()));
}}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!