package utils.cli_utils;

import java.util.HashMap;

import org.apache.commons.cli.BasicParser;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import org.apache.commons.logging.LogFactory;

/**
 * 命令行参数类
 * @author chao
 *
 */
public class CliUtils {
	private static Options opts = new Options();
	private static HashMap<String, CliOptionHandler> handlers = new HashMap<String, CliOptionHandler>();
	
	public static void setOption(String opt, Boolean hasArg, String description, CliOptionHandler handler){
		opts.addOption(opt, hasArg, description);
		handlers.put(opt, handler);
	}
	
	public static void parser(String[] args){
		BasicParser parser = new BasicParser();
		try {
			CommandLine cl = parser.parse(opts, args);
			Option[] options = cl.getOptions();
			for (Option option : options) {
				CliOptionHandler handler = handlers.get(option.getOpt());
				if(handler!=null){
					try {
						handler.call(option.getValues());
					} catch (CliException e) {
						LogFactory.getLog(CliUtils.class).error("!!!", e);
					}
				}
			}
		} catch (ParseException e) {
			LogFactory.getLog(CliUtils.class).error("!!!", e);
		}
		
	}
	
	public static void help(){
		HelpFormatter hf = new HelpFormatter();
		hf.printHelp("Options", opts);
	}
}

@@ -23,35 +23,8 @@
 s;%0A%0A
-import java.util.HashMap;%0A%0A
 impo
@@ -49,27 +49,17 @@
 ons.cli.
-BasicParser
+*
 ;%0Aimport
@@ -67,252 +67,79 @@
 org.
-apache.commons.cli.CommandLine;%0Aimport org.apache.commons.cli.HelpFormatter;%0Aimport org.apache.commons.cli.Option;%0Aimport org.apache.commons.cli.Options;%0Aimport org.apache.commons.cli.ParseException;%0Aimport org.apache.commons.logging.LogFactory
+slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;%0A%0Aimport java.util.HashMap
 ;%0A%0A/
@@ -151,16 +151,19 @@
  %E5%91%BD%E4%BB%A4%E8%A1%8C%E5%8F%82%E6%95%B0%E7%B1%BB%0A
+ *%0A
  * @auth
@@ -170,19 +170,16 @@
 or chao%0A
- *%0A
  */%0Apubl
@@ -198,17 +198,104 @@
 Utils %7B%0A
-%09
+%0A    private static final Logger logger = LoggerFactory.getLogger(CliUtils.class);%0A%0A    
 private 
@@ -331,17 +331,20 @@
 ions();%0A
-%09
+    
 private 
@@ -436,19 +436,21 @@
 ler%3E();%0A
-%09%0A%09
+%0A    
 public s
@@ -543,20 +543,27 @@
 handler)
+ 
 %7B%0A
-%09%09
+        
 opts.add
@@ -596,18 +596,24 @@
 ption);%0A
-%09%09
+        
 handlers
@@ -632,22 +632,27 @@
 ndler);%0A
-%09%7D%0A%09%0A%09
+    %7D%0A%0A    
 public s
@@ -687,17 +687,30 @@
 rgs)
+ 
 %7B%0A
-%09%09Basic
+        CommandLine
 Pars
@@ -729,13 +729,15 @@
 new 
-Basic
+Default
 Pars
@@ -742,27 +742,42 @@
 rser();%0A
-%09%09try %7B%0A%09%09%09
+        try %7B%0A            
 CommandL
@@ -811,19 +811,28 @@
  args);%0A
-%09%09%09
+            
 Option%5B%5D
@@ -859,19 +859,28 @@
 ions();%0A
-%09%09%09
+            
 for (Opt
@@ -903,20 +903,32 @@
 ions) %7B%0A
-%09%09%09%09
+                
 CliOptio
@@ -981,22 +981,35 @@
 ));%0A
-%09%09%09%09
+                
 if
+ 
 (handler
 !=nu
@@ -1008,34 +1008,70 @@
 dler
+ 
 !=
+ 
 null)
+ 
 %7B%0A
-%09%09%09%09%09try %7B%0A%09%09%09%09%09%09
+                    try %7B%0A                        
 hand
@@ -1100,21 +1100,36 @@
 ues());%0A
-%09%09%09%09%09
+                    
 %7D catch 
@@ -1151,181 +1151,205 @@
 ) %7B%0A
-%09%09%09%09%09%09LogFactory.getLog(CliUtils.class).error(%22!!!%22, e);%0A%09%09%09%09%09%7D%0A%09%09%09%09%7D%0A%09%09%09%7D%0A%09%09%7D catch (ParseException e) %7B%0A%09%09%09LogFactory.getLog(CliUtils.class).error(%22!!!%22, e);%0A%09%09%7D%0A%09%09%0A%09%7D%0A%09%0A%09
+                        logger.error(%22!!!%22, e);%0A                    %7D%0A                %7D%0A            %7D%0A        %7D catch (ParseException e) %7B%0A            logger.error(%22!!!%22, e);%0A        %7D%0A%0A    %7D%0A%0A    
 publ
@@ -1373,12 +1373,19 @@
 lp()
+ 
 %7B%0A
-%09%09
+        
 Help
@@ -1424,10 +1424,16 @@
 ();%0A
-%09%09
+        
 hf.p
@@ -1463,9 +1463,12 @@
 s);%0A
-%09
+    
 %7D%0A%7D%0A

upgrade to commons cli 1.3.1