最近实现一个工具,Main函数会有很多参数,而且参数类型不同,为了统一解析,网上找到三方工具类Args4j,轻松搞定。
代码实例如下:
定义解析类:
1import java.io.File 2 3import org.kohsuke.args4j.Option 4import org.slf4j.LoggerFactory 5 6 7/** 8 * 数据库报表生成命令行参数定义 9 * 10 * @author BarryWang create at 2018/6/23 20:21 11 * @version 0.0.1 12 */ 13class ArgOptions { 14 val logger= LoggerFactory.getLogger(classOf[ArgOptions]) 15 16 /** 17 * 对象名及查询SQL脚本对,中间用英文分号":"隔开 18 */ 19 val query = new scala.collection.mutable.ListBuffer[(String, String, String)]; 20 @Option(name = "-q", 21 aliases = Array("-query"), 22 metaVar = "<db>:<objectName>:<sql>", 23 usage = "对象名及查询SQL脚本对,中间用英文分号“:”隔开, 例如database:objectName:sql。(String)") 24 def setProperty(property: String): Unit = { 25 var arr = property.split(":") 26 arr.length match { 27 case 3 => query.+=((arr(0), arr(1), arr(2))) 28 case _ => logger.info("-query 传入参数格式错误, 正确格式: <db>:<objectName>:<sql>") 29 } 30 } 31 32 /** 33 * JXLS Excel模板文件绝对路径 34 */ 35 @Option(name = "-t", 36 aliases = Array("-template"), 37 metaVar = "<template file>", 38 usage = "JXLS Excel模板文件绝对路径, 请参考:http://jxls.sourceforge.net/reference/simple_exporter.html。(File)" ) 39 var template: File = null 40 41 /** 42 * Scala脚本文件 43 */ 44 @Option(name = "-s", 45 aliases = Array("-script"), 46 metaVar = "<scala script file path>", 47 usage = "Scala脚本文件, 请参考:http://ammonite.io/#ScalaScripts。(String)") 48 var script: String = null 49 50 /** 51 * 输出Excel文件 52 */ 53 @Option(name = "-o", 54 aliases = Array("-output"), 55 /* required = true,handler = classOf[StringArrayOptionHandler],*/ 56 metaVar = "<output excel file>", 57 usage = "输出Excel文件绝对路径。(File)") 58 var output: String = null 59 60 /** 61 * 输出Excel文件 62 */ 63 @Option(name = "-m", 64 aliases = Array("-mailto"), 65 metaVar = "<email>", 66 usage = "生成报表发送邮箱,多个使用英文分号“;”分割。(String)") 67 var email: String = null 68 69 70 /** 71 * 邮件主题 72 */ 73 @Option(name = "-sub", 74 aliases = Array("-subject"), 75 metaVar = "<subject>", 76 usage = "邮件主题。(String)") 77 var subject: String = null 78} 79 80引用解析类如下: 81 82import java.io.File 83import java.util.Date 84 85import com.today.dbreport.action.impl.{GenReportByScriptAction, GenReportBySqlAction, GenReportByTemplateBySqlAction} 86import com.today.dbreport.dto.GenReportParam 87import com.today.dbreport.utils.EmailUtil 88import com.today.service.commons.util.DateTools 89import org.kohsuke.args4j.CmdLineParser 90import org.slf4j.LoggerFactory 91 92import scala.collection.JavaConverters._ 93 94/** 95 * 生成报表入口 96 * 97 * @author BarryWang create at 2018/6/1 11:02 98 * @version 0.0.1 99 */ 100object Main { 101 val logger= LoggerFactory.getLogger(Main.getClass) 102 103 def main(args: Array[String]): Unit = { 104 val options = new ArgOptions 105 val parser = new CmdLineParser(options) 106 // print usage 107 parser.printUsage(System.out) 108 parser.parseArgument(args.toList.asJava) 109 110 //输出文件或发送邮件必填一个 111 if(options.output == null && options.email == null){ 112 println("请传入参数-output 或 -mailto其中之一") 113 return 114 } 115 116 //生成报表地址 117 var utf8Output = "" 118 if (options.output != null) { 119 utf8Output = new String(options.output.getBytes("UTF-8"), "UTF-8") 120 } else {//本地临时文件 121 val currentTime = DateTools.format(new Date(), "yyyyMMddHHmmssSSS") 122 val outDir = s"${System.getProperty("user.dir")}${File.separator}output" 123 var outputDir = new File(outDir) 124 if(!outputDir.exists()){ 125 outputDir.mkdirs() 126 } 127 utf8Output = s"${outDir}${File.separator}${currentTime}.xlsx" 128 } 129 //带有Scala脚本 130 if (options.script != null) { 131 var templateOptional: Option[File] = None 132 if (options.template != null) { 133 templateOptional = Some(options.template) 134 } 135 val scriptOptional = Some(options.script) 136 var mailtoOptional: Option[String] = None 137 if (options.email != null) { 138 mailtoOptional = Some(options.email) 139 } 140 val genReportParam = new GenReportParam(options.query, utf8Output, templateOptional, scriptOptional, mailtoOptional) 141 //sql + script + jxls template 142 //script + jxls tempalte 143 new GenReportByScriptAction(genReportParam).execute 144 } else {//无Scala脚本 145 var templateOptional: Option[File] = None 146 if (options.template != null){ 147 templateOptional = Some(options.template) 148 } 149 var scriptOptional : Option[String] = None 150 if(options.script != null){ 151 scriptOptional = Some(options.script) 152 } 153 154 var mailtoOptional: Option[String] = None 155 if (options.email != null){ 156 mailtoOptional = Some(options.email) 157 } 158 159 val genReportParam = new GenReportParam(options.query, utf8Output, templateOptional, scriptOptional, mailtoOptional) 160 if (options.template != null) { //sql* + jxls template 161 new GenReportByTemplateBySqlAction(genReportParam).execute 162 } else { //no template + sql 163 new GenReportBySqlAction(genReportParam).execute 164 } 165 } 166 167 println(s"报表生成成功${utf8Output}!") 168 //发送邮件 169 if (options.email != null) { 170 var subject = "报表工具生成报表" 171 if(options.subject != null){ 172 subject = options.subject 173 } 174 EmailUtil.sendEmail(options.email.trim, subject, "生成报表请参考附件", utf8Output) 175 println("邮件发送成功,请邮件附件下载相关报表!") 176 //邮件发送成功, 删除本地临时文件 177 if (options.output == null) { 178 new File(utf8Output).deleteOnExit() 179 } 180 } 181 logger.info(s"报表生成成功${utf8Output}") 182 } 183} 184 185运行main函数会展示如下提示: 186 187-f (-from) <from> : 邮件发送者。(String) 188 -m (-mailto) <email> : 生成报表发送邮箱,多个使用英文分号“;”分割。(String) 189 -o (-output) <output excel file> : 输出Excel文件绝对路径。(File) 190 -q (-query) <db>:<objectName>:<sql> : 对象名及查询SQL脚本对,中间用英文分号“:”隔开, 191 例如database:objectName:sql。(String) 192 -s (-script) <scala script file path> : Scala脚本文件, 请参考:http://ammonite.io/#Scal 193 aScripts。(String) 194 -sub (-subject) <subject> : 邮件主题。(String) 195 -t (-template) <template file> : JXLS Excel模板文件绝对路径, 请参考:http://jxls.sou 196 rceforge.net/reference/simple_exporter. 197 html。(File) 198请传入参数-output 或 -mailto其中之一
是不是就看起来很直观了!