java/sql-dk/src/info/globalcode/sql/dk/CLIParser.java
branchv_0
changeset 9 2ec52027b97f
parent 8 4507cb9a0cf1
child 10 f528406f33f4
equal deleted inserted replaced
8:4507cb9a0cf1 9:2ec52027b97f
    25 		 * TODO: more types
    25 		 * TODO: more types
    26 		 */
    26 		 */
    27 		types = Collections.unmodifiableMap(m);
    27 		types = Collections.unmodifiableMap(m);
    28 	}
    28 	}
    29 
    29 
    30 	public CLIOptions parseOptions(String[] args) {
    30 	public CLIOptions parseOptions(String[] args) throws CLIParserException {
    31 		CLIOptions options = new CLIOptions();
    31 		CLIOptions options = new CLIOptions();
    32 
    32 
    33 		List<Integer> numberedTypes = new ArrayList<>();
    33 		List<Integer> numberedTypes = new ArrayList<>();
    34 		Map<String, Integer> namedTypes = new HashMap<>();
    34 		Map<String, Integer> namedTypes = new HashMap<>();
    35 
    35 
    84 								int paramIndex = options.getNumberedParameters().size();
    84 								int paramIndex = options.getNumberedParameters().size();
    85 								int paramType;
    85 								int paramType;
    86 								try {
    86 								try {
    87 									paramType = numberedTypes.get(paramIndex);
    87 									paramType = numberedTypes.get(paramIndex);
    88 								} catch (IndexOutOfBoundsException e) {
    88 								} catch (IndexOutOfBoundsException e) {
    89 									throw new IllegalArgumentException("Missing type for parameter #" + paramIndex, e);
    89 									throw new CLIParserException("Missing type for parameter #" + paramIndex, e);
    90 								} catch (NullPointerException e) {
    90 								} catch (NullPointerException e) {
    91 									throw new IllegalArgumentException("Invalid type definition for parameter #" + paramIndex, e);
    91 									throw new CLIParserException("Invalid type definition for parameter #" + paramIndex, e);
    92 								}
    92 								}
    93 								parameter = new Parameter(arg, paramType);
    93 								parameter = new Parameter(arg, paramType);
    94 							}
    94 							}
    95 							options.addNumberedParameter(parameter);
    95 							options.addNumberedParameter(parameter);
    96 						}
    96 						}
    97 					}
    97 					}
    98 					break;
    98 					break;
    99 				default:
    99 				default:
   100 					throw new IllegalArgumentException("Unknown option: " + arg);
   100 					throw new CLIParserException("Unknown option: " + arg);
   101 			}
   101 			}
   102 		}
   102 		}
   103 		return options;
   103 		return options;
   104 	}
   104 	}
   105 
   105 
   106 	private String fetchNext(String[] args, int index) {
   106 	private String fetchNext(String[] args, int index) throws CLIParserException {
   107 		if (index < args.length) {
   107 		if (index < args.length) {
   108 			return args[index];
   108 			return args[index];
   109 		} else {
   109 		} else {
   110 			throw new IllegalArgumentException("Expecting value for option: " + args[index - 1]);
   110 			throw new CLIParserException("Expecting value for option: " + args[index - 1]);
   111 		}
   111 		}
   112 	}
   112 	}
   113 
   113 
   114 	public static class Tokens {
   114 	public static class Tokens {
   115 
   115 
   124 
   124 
   125 		private Tokens() {
   125 		private Tokens() {
   126 		}
   126 		}
   127 	}
   127 	}
   128 
   128 
   129 	private int getType(String typeString) {
   129 	private int getType(String typeString) throws CLIParserException {
   130 		Integer type = types.get(typeString);
   130 		Integer type = types.get(typeString);
   131 		if (type == null) {
   131 		if (type == null) {
   132 			throw new IllegalArgumentException("Unsupported type: " + typeString);
   132 			throw new CLIParserException("Unsupported type: " + typeString);
   133 		} else {
   133 		} else {
   134 			return type;
   134 			return type;
   135 		}
   135 		}
   136 	}
   136 	}
   137 }
   137 }