ApacheCommons CLi 를 사용 하여 명령 행 도 구 를 개발 합 니 다.
쓸데없는 말 하지 말고 예 를 들 어 라.
- /**
- * @author charles.wang
- * @created Jan 10, 2012 4:30:22 PM
- * Description:
- */
- public class RMDataSource {
-
-
- //
- public static int simpleTest(String[] args){
-
- ResourceBundle resourceBundle = ResourceBundle.getBundle("message",Locale.getDefault());
-
- // Option Options
- Options opts = new Options();
- // 3 ,
- // ,
- // ,
- //
- opts.addOption("h", false, resourceBundle.getString("HELP_DESCRIPTION"));
- opts.addOption("i", true, resourceBundle.getString("HELP_IPADDRESS"));
- opts.addOption("p", true, resourceBundle.getString("HELP_PORT"));
- opts.addOption("t", true, resourceBundle.getString("HELP_PROTOCOL"));
-
- //
- BasicParser parser = new BasicParser();
- // , Options
- CommandLine cl;
- try {
- // Options , CommandLine
- cl = parser.parse(opts, args);
- // Option ,
- if (cl.getOptions().length > 0) {
-
- // , -h , ,
- // h , HelpFormatter
- if (cl.hasOption('h')) {
- // HelpFormatter
- HelpFormatter hf = new HelpFormatter();
- hf.printHelp("Options", opts);
- return 0;
- }
- // , ,
- else {
- //
- String ip = cl.getOptionValue("i");
- String port = cl.getOptionValue("p");
- String protocol = cl.getOptionValue("t");
- System.out.println("ip: "+ip);
- System.out.println("port: "+port);
- System.out.println("protocol: "+protocol);
- return 0;
- }
- }
- // , ,
- else {
- System.err.println(resourceBundle.getString("ERROR_NOARGS"));
- return 1;
- }
- } catch (ParseException e) {
- e.printStackTrace();
- return 1;
- }
- }
-
-
- }
유닛 테스트:
- public class RMDataSourceTest {
-
- @Before
- public void setUp() throws Exception {
- }
-
- @After
- public void tearDown() throws Exception {
- }
-
- // –h , ,
- @Test
- public void testHelp() {
- System.out.println("***TC:Test -h option***");
- String args[]={"-h"};
- assertEquals(0, RMDataSource.simpleTest(args));
- System.out.println("***********************");
- }
-
- // –h , ,
- @Test
- public void testNoArgs() {
- System.out.println("***TC:Test no argument options***");
- String args[] = new String[0];
- assertEquals(1, RMDataSource.simpleTest(args));
- System.out.println("***********************");
- }
-
- // ,
- @Test
- public void testRMDataSource() {
- /**
- * java rmdatasource -i 192.168.0.2 -p 5988 -t http
- */
- System.out.println("***TC:Test normal arguments options***");
- String args[] = new String[]{"-i","192.168.0.2","-p","5988","-t","http"};
- assertEquals(0, RMDataSource.simpleTest(args));
- System.out.println("***********************");
- }
-
- }
본 고 는'평행선 의 응집'블 로그 에서 나 왔 으 니 작가 와 연락 하 세 요!
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
peco의 쉽고 편리한 사용법가자. | peco 를 사용하여 검색한 파일/디렉토리에 cd, ls, rm, cat, vim 한다. 각각 별칭으로 쉽게 사용할 수 있도록 한다. lsp, vimp, catp 등도 마찬가지로 검색해, 각각의 액션을 실...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.