import org.apache.commons.cli.*;
import org.junit.Test;
public class Commons_cli {
public static void prepare(String[] args) throws Exception {
// commons-cli命令行參數,需要帶參數值
Options options = new Options();
// sql文件路徑
options.addOption("sql", true, "sql config");
// 表名稱
options.addOption("table", true, "table name");
options.addOption("where", true, "where");
// 解析命令行參數
CommandLineParser parser = new DefaultParser();
CommandLine cl = parser.parse(options, args);
String sql = cl.getOptionValue("sql");
String name = cl.getOptionValue("table");
String where =cl.getOptionValue("where");
System.out.println("sql : "+sql);
System.out.println("table : "+name);
System.out.println("where : "+where);
}
@Test
public void prepare() throws Exception {
//注,這裏的-sql不是負號的意思,命令行。
String[] args = {"-sql","select * from aa",
"-table","測試",
"-where","table.id>100"};
Commons_cli.prepare(args);
}
}
Connected to the target VM, address: '127.0.0.1:57286', transport: 'socket'
sql : select * from aa
table : 測試
where : table.id>100
Disconnected from the target VM, address: '127.0.0.1:57286', transport: 'socket'