Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Simple Toolkit To Read And Parse The Argument From Console

DZone's Guide to

Simple Toolkit To Read And Parse The Argument From Console

·
Free Resource
*. 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
  
    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
   
     def,String[] arg){
		ArrayList
    
      argList = new ArrayList
     
      (); //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
      
       ();
		Set
       
        > s = def.entrySet();
		Iterator
        
         > iter = s.iterator();
		while(iter.hasNext()){
			Entry
         
           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
          
            getSettingArgu(){
		ArrayList
           
             tmp = new ArrayList
            
             ();
		Set
             
              > s = argsSet.entrySet();
		Iterator
              
               > iter = s.iterator();
		while(iter.hasNext()){
			Entry
               
                 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
                
                  defineOfArgu = new HashMap
                 
                  ();
		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
                  
                    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());
                        }
		}
	}		
}


                  
                 
                
               
              
             
            
           
          
         
        
       
      
     
    
   
  
Topics:

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}