File Source: CLIParser.java

         /* 
    P/P   *  Method: com.dmdirc.installer.cliparser.CLIParser__static_init
          */
     1  /*
     2   * Copyright (c) 2006-2009 Shane Mc Cormack
     3   *
     4   * Permission is hereby granted, free of charge, to any person obtaining a copy
     5   * of this software and associated documentation files (the "Software"), to deal
     6   * in the Software without restriction, including without limitation the rights
     7   * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     8   * copies of the Software, and to permit persons to whom the Software is
     9   * furnished to do so, subject to the following conditions:
    10   *
    11   * The above copyright notice and this permission notice shall be included in
    12   * all copies or substantial portions of the Software.
    13   *
    14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
    20   * SOFTWARE.
    21   */
    22  
    23  package com.dmdirc.installer.cliparser;
    24  
    25  import java.util.ArrayList;
    26  import java.util.Hashtable;
    27  import java.util.List;
    28  import java.util.Map;
    29  
    30  /**
    31   * Command Line argument parser.
    32   */
    33  public class CLIParser {
    34  	/** Singleton instance of CLIParser. */
    35  	private static CLIParser me;
    36  	
    37  	/** Singleton instance of CLIParser. */
    38  	CLIParam helpParam = null;
    39  	
    40  	/**
    41  	 * Known arguments.
    42  	 * This hashtable stores the arguments with their flags as the key.
    43  	 */
    44  	private final Map<String, CLIParam> params = new Hashtable<String, CLIParam>();
    45  	
    46  	/**
    47  	 * Known arguments.
    48  	 * This ArrayList stores every param type. (used for help)
    49  	 */
    50  	private final List<CLIParam> paramList = new ArrayList<CLIParam>();
    51  	
    52  	/**
    53  	 * Redundant Strings.
    54  	 * This ArrayList stores redundant strings found whilst parsing the params.
    55  	 */
    56  	private final List<String> redundant = new ArrayList<String>();
    57  	
    58  	/**
    59  	 * Get a reference to the CLIParser.
    60  	 */
    61  	public static synchronized CLIParser getCLIParser() {
        		 /* 
    P/P 		  *  Method: CLIParser getCLIParser()
        		  * 
        		  *  Preconditions:
        		  *    init'ed(me)
        		  * 
        		  *  Postconditions:
        		  *    me == One-of{old me, &amp;new CLIParser(getCLIParser#1)}
        		  *    me != null
        		  *    return_value == me
        		  *    new ArrayList(CLIParser#2) num objects <= 1
        		  *    new ArrayList(CLIParser#3) num objects <= 1
        		  *    new CLIParser(getCLIParser#1) num objects <= 1
        		  *    new CLIParser(getCLIParser#1).helpParam == null
        		  *    new CLIParser(getCLIParser#1).paramList == &amp;new ArrayList(CLIParser#2)
        		  *    new CLIParser(getCLIParser#1).params == &amp;new Hashtable(CLIParser#1)
        		  *    new CLIParser(getCLIParser#1).redundant == &amp;new ArrayList(CLIParser#3)
        		  *    ...
        		  * 
        		  *  Test Vectors:
        		  *    me: Inverse{null}, Addr_Set{null}
        		  */
    62  		if (me == null) { me = new CLIParser(); }
    63  		return me;
    64  	}
    65  	
    66  	/** Private constructor for CLIParser to prevent non-singleton instance. */
        	 /* 
    P/P 	  *  Method: void com.dmdirc.installer.cliparser.CLIParser()
        	  * 
        	  *  Postconditions:
        	  *    this.helpParam == null
        	  *    this.paramList == &amp;new ArrayList(CLIParser#2)
        	  *    this.params == &amp;new Hashtable(CLIParser#1)
        	  *    this.redundant == &amp;new ArrayList(CLIParser#3)
        	  *    new ArrayList(CLIParser#2) num objects == 1
        	  *    new ArrayList(CLIParser#3) num objects == 1
        	  *    new Hashtable(CLIParser#1) num objects == 1
        	  */
    67  	private CLIParser() { }
    68  	
    69  	/** Clear known params from the hashtable. */
    70  	public void clear() {
        		 /* 
    P/P 		  *  Method: void clear()
        		  * 
        		  *  Preconditions:
        		  *    this.paramList != null
        		  *    this.params != null
        		  *    this.redundant != null
        		  */
    71  		params.clear();
    72  		paramList.clear();
    73  		redundant.clear();
    74  	}
    75  	
    76  	/**
    77  	 * Add a CLIParam to the cliparser.
    78  	 *
    79  	 * @param param CLIParam sub-class to use as a parameter.
    80  	 * @return true if added, false if already exists.
    81  	 */
    82  	public boolean add(final CLIParam param) {
        		 /* 
    P/P 		  *  Method: bool add(CLIParam)
        		  * 
        		  *  Preconditions:
        		  *    param != null
        		  *    param.stringFlag != null
        		  *    (soft) this.paramList != null
        		  *    (soft) this.params != null
        		  * 
        		  *  Postconditions:
        		  *    init'ed(return_value)
        		  * 
        		  *  Test Vectors:
        		  *    param.charFlag: {1..216-1}, {0}
        		  *    java.lang.String:isEmpty(...)@89: {1}, {0}
        		  */
    83  		final boolean validChar = param.getChr() == 0 || !params.containsKey(param.getChr());
    84  		final boolean validString = param.getString().isEmpty() || !params.containsKey("-"+param.getString());
    85  		if (validChar && validString) {
    86  			if (param.getChr() != 0) {
    87  				params.put(String.valueOf(param.getChr()), param);
    88  			}
    89  			if (!param.getString().isEmpty()) {
    90  				params.put("-"+param.getString(), param);
    91  			}
    92  			paramList.add(param);
    93  			return true;
    94  		} else {
    95  			return false;
    96  		}
    97  	}
    98  	
    99  	/**
   100  	 * Get the number of times a param was given.
   101  	 * In the case of params with both a char and string value, this number is
   102  	 * the total for both.
   103  	 *
   104  	 * @param flag Flag to get count for
   105  	 * @return number, or -1 if the param is invalud
   106  	 */
   107  	public int getParamNumber(final String flag) {
        		 /* 
    P/P 		  *  Method: int getParamNumber(String)
        		  * 
        		  *  Preconditions:
        		  *    this.params != null
        		  * 
        		  *  Presumptions:
        		  *    java.util.Map:get(...)@109 != null
        		  * 
        		  *  Postconditions:
        		  *    init'ed(return_value)
        		  * 
        		  *  Test Vectors:
        		  *    java.util.Map:containsKey(...)@108: {0}, {1}
        		  */
   108  		if (params.containsKey(flag)) {
   109  			return params.get(flag).getNumber();
   110  		} else {
   111  			return -1;
   112  		}
   113  	}
   114  	
   115  	/**
   116  	 * Get a CLIParam object for a given flag.
   117  	 *
   118  	 * @param flag Flag to get param for
   119  	 * @return CLIParam object, or null if there is none.
   120  	 */
   121  	public CLIParam getParam(final String flag) {
        		 /* 
    P/P 		  *  Method: CLIParam getParam(String)
        		  * 
        		  *  Preconditions:
        		  *    this.params != null
        		  * 
        		  *  Postconditions:
        		  *    init'ed(return_value)
        		  * 
        		  *  Test Vectors:
        		  *    java.util.Map:containsKey(...)@122: {0}, {1}
        		  */
   122  		if (params.containsKey(flag)) {
   123  			return params.get(flag);
   124  		} else {
   125  			return null;
   126  		}
   127  	}
   128  	
   129  	/**
   130  	 * Get the list of params.
   131  	 *
   132  	 * @return list of params.
   133  	 */
   134  	public List<CLIParam> getParamList() {
        		 /* 
    P/P 		  *  Method: List getParamList()
        		  * 
        		  *  Postconditions:
        		  *    return_value == this.paramList
        		  *    init'ed(return_value)
        		  */
   135  		return paramList;
   136  	}
   137  	
   138  	/**
   139  	 * Get the list of redundant strings.
   140  	 *
   141  	 * @return list of redundant strings.
   142  	 */
   143  	public List<String> getRedundant() {
        		 /* 
    P/P 		  *  Method: List getRedundant()
        		  * 
        		  *  Preconditions:
        		  *    this.redundant != null
        		  * 
        		  *  Postconditions:
        		  *    return_value == &amp;new ArrayList(getRedundant#1)
        		  *    new ArrayList(getRedundant#1) num objects == 1
        		  * 
        		  *  Test Vectors:
        		  *    java.util.Iterator:hasNext(...)@145: {0}, {1}
        		  */
   144  		final List<String> result = new ArrayList<String>();
   145  		for (String item : redundant) {
   146  			result.add(item);
   147  		}
   148  		return result;
   149  	}
   150  	
   151  	/**
   152  	 * Set the "help" command.
   153  	 *
   154  	 * @param param Param to look for in wantsHelp.
   155  	 */
   156  	public void setHelp(final CLIParam param) {
        		 /* 
    P/P 		  *  Method: void setHelp(CLIParam)
        		  * 
        		  *  Postconditions:
        		  *    this.helpParam == param
        		  *    init'ed(this.helpParam)
        		  */
   157  		helpParam = param;
   158  	}
   159  	
   160  	/**
   161  	 * Check if the help parameter has been passed to the CLI.
   162  	 */
   163  	public boolean wantsHelp(final String[] args) {
        		 /* 
    P/P 		  *  Method: bool wantsHelp(String[])
        		  * 
        		  *  Preconditions:
        		  *    init'ed(this.helpParam)
        		  *    (soft) args != null
        		  *    (soft) args.length <= 232-1
        		  *    (soft) args[...] != null
        		  *    (soft) this.params != null
        		  * 
        		  *  Postconditions:
        		  *    init'ed(return_value)
        		  * 
        		  *  Test Vectors:
        		  *    this.helpParam: Inverse{null}, Addr_Set{null}
        		  *    java.lang.String:charAt(...)@166: {0..44, 46..216-1}, {45}
        		  *    java.lang.String:equals(...)@168: {0}, {1}
        		  *    java.lang.String:length(...)@166: {0,1}, {2..232-1}
        		  */
   164  		if (helpParam == null) { return false; }
   165  		for (String arg : args) {
   166  			if (arg.length() > 1 && arg.charAt(0) == '-') {
   167  				final String name = arg.substring(1);
   168  				if (name.equals("-")) {
   169  					return false;
   170  				} else {
   171  					final CLIParam param = getParam(name);
   172  					if (param == helpParam) {
   173  						return true;
   174  					}
   175  				}
   176  			}
   177  		}
   178  		return false;
   179  	}
   180  	
   181  	/**
   182  	 * Show the help
   183  	 */
   184  	public void showHelp(final String title, final String usage) {
        		 /* 
    P/P 		  *  Method: void showHelp(String, String)
        		  * 
        		  *  Preconditions:
        		  *    this.paramList != null
        		  * 
        		  *  Presumptions:
        		  *    java.lang.System.out != null
        		  *    java.util.Iterator:next(...)@189 != null
        		  *    param.stringFlag@189 != null
        		  * 
        		  *  Test Vectors:
        		  *    java.lang.String:isEmpty(...)@195: {0}, {1}
        		  *    java.util.Iterator:hasNext(...)@189: {0}, {1}
        		  *    param.charFlag@189: {1..216-1}, {0}
        		  */
   185  		System.out.println(title);
   186  		System.out.println("------------------");
   187  		System.out.println(usage);
   188  		System.out.println(" ");
   189  		for (CLIParam param : this.getParamList()) {
   190  			if (param.getChr() == 0) {
   191  				System.out.print("   ");
   192  			} else {
   193  				System.out.print("-"+param.getChr()+" ");
   194  			}
   195  			if (param.getString().isEmpty()) {
   196  				System.out.print("\t\t");
   197  			} else {
   198  				System.out.print("--"+param.getString()+" ");
   199  			}
   200  			System.out.println("\t"+param.getDescription());
   201  		}
   202  	}
   203  	
   204  	/**
   205  	 * Given a string array of arguments, parse as CLI Params.
   206  	 *
   207  	 * @param args Arguments to pass
   208  	 * @param strict if True, will terminate if a given param is invalid.
   209  	 */
   210  	public void parseArgs(final String[] args, final boolean strict) {
        		 /* 
    P/P 		  *  Method: void parseArgs(String[], bool)
        		  * 
        		  *  Preconditions:
        		  *    args != null
        		  *    args.length <= 232-1
        		  *    (soft) args[...] != null
        		  *    (soft) init'ed(this.helpParam)
        		  *    (soft) this.helpParam.stringFlag != null
        		  *    (soft) this.params != null
        		  *    (soft) this.redundant != null
        		  * 
        		  *  Presumptions:
        		  *    java.lang.System.out != null
        		  *    lastParam.number@220 <= 232-2
        		  * 
        		  *  Postconditions:
        		  *    init'ed(java.lang.String:substring(...)._tainted)
        		  * 
        		  *  Test Vectors:
        		  *    this.helpParam: Addr_Set{null}, Inverse{null}
        		  *    this.helpParam.charFlag: {0}, {1..216-1}
        		  *    java.lang.String:charAt(...)@214: {0..44, 46..216-1}, {45}
        		  *    java.lang.String:charAt(...)@242: {0..91, 93..216-1}, {92}
        		  *    java.lang.String:equals(...)@217: {0}, {1}
        		  *    java.lang.String:isEmpty(...)@225: {0}, {1}
        		  *    java.lang.String:isEmpty(...)@230: {1}, {0}
        		  *    java.lang.String:length(...)@214: {0,1}, {2..232-1}
        		  *    java.lang.String:length(...)@242: {0,1}, {2..232-1}
        		  */
   211  		CLIParam lastParam = null;
   212  		boolean allRedundant = false;
   213  		for (String arg : args) {
   214  			if (arg.length() > 1 && arg.charAt(0) == '-' && !allRedundant) {
   215  				if (lastParam != null) { lastParam.setValue(""); }
   216  				final String name = arg.substring(1);
   217  				if (name.equals("-")) {
   218  					allRedundant = true;
   219  				} else {
   220  					lastParam = getParam(name);
   221  					if (lastParam == null) {
   222  						System.out.println("Unknown Param: -"+name);
   223  						if (helpParam != null) {
   224  							String command = "";
   225  							if (helpParam.getString().isEmpty()) {
   226  								command = String.valueOf(helpParam.getChr());
   227  							} else if (helpParam.getChr() != 0) {
   228  								command = helpParam.getString();
   229  							}
   230  							if (!command.isEmpty()) {
   231  								System.out.println("Use "+command+" to get help.");
   232  							}
   233  						}
   234  						if (strict) {
   235  							System.exit(1);
   236  						}
   237  					} else {
   238  						lastParam.incNumber();
   239  					}
   240  				}
   241  			} else {
   242  				if (arg.charAt(0) == '\\' && arg.length() > 1) { arg = arg.substring(1); }
   243  				if (lastParam == null || allRedundant || !lastParam.setValue(arg)) {
   244  					redundant.add(arg);
   245  				}
   246  			}
   247  		}
   248  	}
   249  }








SofCheck Inspector Build Version : 2.17854
CLIParser.java 2009-Jun-25 01:54:24
CLIParser.class 2009-Sep-02 17:04:16