DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Simple Toolkit To Read And Parse The Argument From Console

01.19.2010
| 2051 views |
  • submit to reddit
        *. It has dependancy to another class Argument.

package gays.tools;

import java.util.*;
import java.util.Map.Entry;

/**
 * BD : Simple tool kit to parse the argument from console
 * @author John-Lee
 */
public class ArguParser {
	private HashMap<String,Argument> argsSet;  //Defination of argument
	private boolean empty = true;

        /**
         * BD : Constructor to receive argument defination and actual argument from console.
         * @param def : Defination of argument
         * @param arg : Argument from console
         */
	public ArguParser(HashMap<String,String> def,String[] arg){
		ArrayList<String> argList = new ArrayList<String>(); //Console argument list
		if(arg!=null&&arg.length>0){
			empty = false;
			for(String a:arg){
                                //System.out.println("Add "+a);
				argList.add(a);
			}
		}
		argsSet = new HashMap<String,Argument>();
		Set<Entry<String,String>> s = def.entrySet();
		Iterator<Entry<String,String>> iter = s.iterator();
		while(iter.hasNext()){
			Entry<String,String> e = iter.next(); //defination of argument
			Argument argStruct = new Argument(e.getKey(),e.getValue());
			argsSet.put(argStruct.getKey(), argStruct);
			for(String a:argList){
				if(argStruct.checkArgu(a)){                                        
					argList.remove(a);
					break;
				}
			}
		}				
	}
	
	/**
	 * BD : Getting all given argument from console.
	 * @return ArrayList of Object:Argument which contains argument info.
	 */
	public ArrayList<Argument> getSettingArgu(){
		ArrayList<Argument> tmp = new ArrayList<Argument>();
		Set<Entry<String,Argument>> s = argsSet.entrySet();
		Iterator<Entry<String,Argument>> iter = s.iterator();
		while(iter.hasNext()){
			Entry<String,Argument> e = iter.next();
			if(e.getValue().isSet()){
				tmp.add(e.getValue());
			}
		}		
		return tmp;
	}
	
	/**
	 * BD : Getting description of specific argument. 
	 * @param key : argument key
	 * @return
	 */
	public String getArguDescrip(String key){
		Argument argu = argsSet.get(key);
		if(argu!=null){
			return argu.getDescript();
		}
		return "Unknown Argument!";
	}
	
	/**
	 * BD : Check if specific argument is set. If the argument is given from console, return true. Vice versa.
	 * @param key : argument key
	 * @return
	 */
	public boolean isSet(String key){
		Argument argu = argsSet.get(key);
		if(argu!=null){
			return argu.isSet();
		}
		return false;
	}

	/**
	 * BD : Check if the given argument is empty. If empty, return true. Vice versa.
	 * @return boolean
	 */
	public boolean isEmpty() {
		return empty;
	}
	
	public static void main(String args[]){
		/*The first mark of argument will be the unique key for searching*/
                /*Step1 : Define argument meaning*/
                String fargs[] = {"-a=test","-b","-c"};
		HashMap<String,String> defineOfArgu = new HashMap<String,String>();
		defineOfArgu.put("-a,--auto", "Auto Setup.");
		defineOfArgu.put("-b,--block", "Block Until...");
		defineOfArgu.put("-d,-D,--decode", "Decode");
		defineOfArgu.put("-c,-C,--Check,-3", "Check Mail...");
		defineOfArgu.put("-e,-E,--eg", "Show Example");
                /*Step2 : Pass argument defination and actualy argu read from console into Class:ArguParser*/
		ArguParser parser = new ArguParser(defineOfArgu,fargs);
                /*Step3 : Fetch argument and analysing*/
		ArrayList<Argument> argSet = parser.getSettingArgu();
		for(Argument a:argSet){
			System.out.print("You have give "+a.getKey()+" which means: "+a.getDescript());
                        if(a.getValue().isEmpty()) {
                            System.out.println();
                        } else {
                            System.out.println(" with values="+a.getValue());
                        }
		}
	}		
}