ApacheCommons CLi 를 사용 하여 명령 행 도 구 를 개발 합 니 다.

13435 단어 command명령 행line
ApacheCommons CLi 도 구 를 체험 해 보 니 매우 좋 고 명령 행 을 만 들 수 있 습 니 다.
쓸데없는 말 하지 말고 예 를 들 어 라.
 

  
  
  
  

  1. /** 
  2.  * @author charles.wang 
  3.  * @created Jan 10, 2012 4:30:22 PM 
  4.  * Description: 
  5.  */ 
  6. public class RMDataSource { 
  7.  
  8.      
  9.     //  
  10.     public static int simpleTest(String[] args){ 
  11.          
  12.         ResourceBundle resourceBundle = ResourceBundle.getBundle("message",Locale.getDefault()); 
  13.          
  14.         // Option Options  
  15.         Options opts = new Options();  
  16.         // 3 , 
  17.         // , 
  18.         // , 
  19.         //  
  20.         opts.addOption("h"false, resourceBundle.getString("HELP_DESCRIPTION")); 
  21.         opts.addOption("i"true, resourceBundle.getString("HELP_IPADDRESS"));  
  22.         opts.addOption("p"true, resourceBundle.getString("HELP_PORT"));  
  23.         opts.addOption("t"true, resourceBundle.getString("HELP_PROTOCOL"));  
  24.          
  25.         //  
  26.         BasicParser parser = new BasicParser();  
  27.         // , Options  
  28.         CommandLine cl;  
  29.         try {  
  30.             // Options , CommandLine  
  31.             cl = parser.parse(opts, args);  
  32.             // Option ,  
  33.             if (cl.getOptions().length > 0) {  
  34.                  
  35.                 // , -h , ,  
  36.                 // h , HelpFormatter  
  37.                 if (cl.hasOption('h')) {  
  38.                     // HelpFormatter 
  39.                     HelpFormatter hf = new HelpFormatter();  
  40.                     hf.printHelp("Options", opts);  
  41.                     return 0
  42.                 }  
  43.                 // , ,  
  44.                 else {  
  45.                     //  
  46.                     String ip = cl.getOptionValue("i");  
  47.                     String port = cl.getOptionValue("p");  
  48.                     String protocol = cl.getOptionValue("t");  
  49.                     System.out.println("ip: "+ip); 
  50.                     System.out.println("port: "+port); 
  51.                     System.out.println("protocol:   "+protocol); 
  52.                     return 0
  53.                 }  
  54.             }  
  55.             // , ,  
  56.             else {  
  57.                System.err.println(resourceBundle.getString("ERROR_NOARGS"));  
  58.                return 1
  59.             }  
  60.         } catch (ParseException e) {  
  61.             e.printStackTrace();  
  62.             return 1
  63.         }  
  64.     }  
  65.      
  66.  

 
유닛 테스트:
 

  
  
  
  
  1. public class RMDataSourceTest { 
  2.  
  3.     @Before 
  4.     public void setUp() throws Exception { 
  5.     } 
  6.  
  7.     @After 
  8.     public void tearDown() throws Exception { 
  9.     } 
  10.  
  11.     //   –h  , ,  
  12.     @Test 
  13.     public void testHelp() {  
  14.         System.out.println("***TC:Test -h option***"); 
  15.         String args[]={"-h"};  
  16.         assertEquals(0, RMDataSource.simpleTest(args));  
  17.         System.out.println("***********************"); 
  18.     }  
  19.  
  20.     //   –h   ,   ,  
  21.     @Test 
  22.     public void testNoArgs() {  
  23.         System.out.println("***TC:Test no argument options***"); 
  24.         String args[] = new String[0];  
  25.         assertEquals(1, RMDataSource.simpleTest(args));  
  26.         System.out.println("***********************"); 
  27.     }  
  28.  
  29.     //  ,  
  30.     @Test 
  31.     public void testRMDataSource() {  
  32.       /**  
  33.        *   java rmdatasource -i 192.168.0.2 -p 5988 -t http  
  34.         */  
  35.         System.out.println("***TC:Test normal arguments options***"); 
  36.         String args[] = new String[]{"-i","192.168.0.2","-p","5988","-t","http"};  
  37.         assertEquals(0, RMDataSource.simpleTest(args));  
  38.         System.out.println("***********************"); 
  39.     }  
  40.  

 
본 고 는'평행선 의 응집'블 로그 에서 나 왔 으 니 작가 와 연락 하 세 요!

좋은 웹페이지 즐겨찾기