@Test public void parseFormat() throws Exception { String format = "#address[s<(udp|tcp):.*[/[0-9]+]?>{=udp:127.0.0.1/161}] .."; ArgumentParser argumentParser = new ArgumentParser("", format); assertEquals("{address=ArgumentFormat[option=address,parameter=true,vararg=true,mandatatory=true,"+ "parameters=[ArgumentParameter[name=,type=2,patttern=(udp|tcp):.*[/[0-9]+]?,defaultValue=udp:127.0.0.1/161]]]}", argumentParser.getParameterFormat().toString()); Map<String,List<?>> commandLineParameters = argumentParser.parse(new String[] { }); assertTrue(commandLineParameters.containsKey("address")); }
protected static Map<String, ArgumentFormat> parseFormat(String format, boolean parameterFormat) { Map<String, ArgumentFormat> options = new LinkedHashMap<String, ArgumentFormat>(); ArgumentFormat last = null; StringTokenizer st = new StringTokenizer(format, " "); while (st.hasMoreTokens()) { String token = st.nextToken(); if ("..".equals(token)) { if (last != null) { last.vararg = true; break; } else { throw new IllegalArgumentException("'..' without parameter definition"); } } ArgumentFormat af = new ArgumentFormat(); last = af; af.parameter = parameterFormat; af.mandatory = (token.charAt(0) != '+'); token = token.substring(1); if (token.endsWith("]")) { af.option = token.substring(0, token.indexOf('[')); token = token.substring(af.option.length()+1, token.length()-1); StringTokenizer pt = new StringTokenizer(token, ",", true); List<ArgumentParameter> params = new ArrayList<ArgumentParameter>(); String inRegex = null; for (int i=1; (pt.hasMoreTokens()); i++) { String param = pt.nextToken(); if (inRegex != null) { inRegex += param; param = inRegex; } else if (",".equals(param)) { continue; } if (param.indexOf('<')>0 && param.indexOf('>') <0) { inRegex = param; } ArgumentParameter ap = new ArgumentParameter(); ap.name = ""+i; if (param.endsWith("}")) { param = parseParameterFormat(param, ap); } if (param.endsWith(">")) { inRegex = null; int regexPos = param.indexOf('<'); ap.pattern = Pattern.compile(param.substring(regexPos+1, param.length()-1)); param = param.substring(0, regexPos); if (param.endsWith("}")) { parseParameterFormat(param, ap); } else { ap.type = getType(param); } } else if (inRegex != null) { continue; } else { ap.type = getType(param); } params.add(ap); } af.params = params.toArray(new ArgumentParameter[params.size()]); } else { af.option = token; if (af.parameter) { throw new IllegalArgumentException("Parameter "+token+" has no type"); } } options.put(af.option, af); } return options; }
ArgumentParser { protected static Map<String, ArgumentFormat> parseFormat(String format, boolean parameterFormat) { Map<String, ArgumentFormat> options = new LinkedHashMap<String, ArgumentFormat>(); ArgumentFormat last = null; StringTokenizer st = new StringTokenizer(format, " "); while (st.hasMoreTokens()) { String token = st.nextToken(); if ("..".equals(token)) { if (last != null) { last.vararg = true; break; } else { throw new IllegalArgumentException("'..' without parameter definition"); } } ArgumentFormat af = new ArgumentFormat(); last = af; af.parameter = parameterFormat; af.mandatory = (token.charAt(0) != '+'); token = token.substring(1); if (token.endsWith("]")) { af.option = token.substring(0, token.indexOf('[')); token = token.substring(af.option.length()+1, token.length()-1); StringTokenizer pt = new StringTokenizer(token, ",", true); List<ArgumentParameter> params = new ArrayList<ArgumentParameter>(); String inRegex = null; for (int i=1; (pt.hasMoreTokens()); i++) { String param = pt.nextToken(); if (inRegex != null) { inRegex += param; param = inRegex; } else if (",".equals(param)) { continue; } if (param.indexOf('<')>0 && param.indexOf('>') <0) { inRegex = param; } ArgumentParameter ap = new ArgumentParameter(); ap.name = ""+i; if (param.endsWith("}")) { param = parseParameterFormat(param, ap); } if (param.endsWith(">")) { inRegex = null; int regexPos = param.indexOf('<'); ap.pattern = Pattern.compile(param.substring(regexPos+1, param.length()-1)); param = param.substring(0, regexPos); if (param.endsWith("}")) { parseParameterFormat(param, ap); } else { ap.type = getType(param); } } else if (inRegex != null) { continue; } else { ap.type = getType(param); } params.add(ap); } af.params = params.toArray(new ArgumentParameter[params.size()]); } else { af.option = token; if (af.parameter) { throw new IllegalArgumentException("Parameter "+token+" has no type"); } } options.put(af.option, af); } return options; } }
ArgumentParser { protected static Map<String, ArgumentFormat> parseFormat(String format, boolean parameterFormat) { Map<String, ArgumentFormat> options = new LinkedHashMap<String, ArgumentFormat>(); ArgumentFormat last = null; StringTokenizer st = new StringTokenizer(format, " "); while (st.hasMoreTokens()) { String token = st.nextToken(); if ("..".equals(token)) { if (last != null) { last.vararg = true; break; } else { throw new IllegalArgumentException("'..' without parameter definition"); } } ArgumentFormat af = new ArgumentFormat(); last = af; af.parameter = parameterFormat; af.mandatory = (token.charAt(0) != '+'); token = token.substring(1); if (token.endsWith("]")) { af.option = token.substring(0, token.indexOf('[')); token = token.substring(af.option.length()+1, token.length()-1); StringTokenizer pt = new StringTokenizer(token, ",", true); List<ArgumentParameter> params = new ArrayList<ArgumentParameter>(); String inRegex = null; for (int i=1; (pt.hasMoreTokens()); i++) { String param = pt.nextToken(); if (inRegex != null) { inRegex += param; param = inRegex; } else if (",".equals(param)) { continue; } if (param.indexOf('<')>0 && param.indexOf('>') <0) { inRegex = param; } ArgumentParameter ap = new ArgumentParameter(); ap.name = ""+i; if (param.endsWith("}")) { param = parseParameterFormat(param, ap); } if (param.endsWith(">")) { inRegex = null; int regexPos = param.indexOf('<'); ap.pattern = Pattern.compile(param.substring(regexPos+1, param.length()-1)); param = param.substring(0, regexPos); if (param.endsWith("}")) { parseParameterFormat(param, ap); } else { ap.type = getType(param); } } else if (inRegex != null) { continue; } else { ap.type = getType(param); } params.add(ap); } af.params = params.toArray(new ArgumentParameter[params.size()]); } else { af.option = token; if (af.parameter) { throw new IllegalArgumentException("Parameter "+token+" has no type"); } } options.put(af.option, af); } return options; } ArgumentParser(String optionFormat, String parameterFormat); }
ArgumentParser { protected static Map<String, ArgumentFormat> parseFormat(String format, boolean parameterFormat) { Map<String, ArgumentFormat> options = new LinkedHashMap<String, ArgumentFormat>(); ArgumentFormat last = null; StringTokenizer st = new StringTokenizer(format, " "); while (st.hasMoreTokens()) { String token = st.nextToken(); if ("..".equals(token)) { if (last != null) { last.vararg = true; break; } else { throw new IllegalArgumentException("'..' without parameter definition"); } } ArgumentFormat af = new ArgumentFormat(); last = af; af.parameter = parameterFormat; af.mandatory = (token.charAt(0) != '+'); token = token.substring(1); if (token.endsWith("]")) { af.option = token.substring(0, token.indexOf('[')); token = token.substring(af.option.length()+1, token.length()-1); StringTokenizer pt = new StringTokenizer(token, ",", true); List<ArgumentParameter> params = new ArrayList<ArgumentParameter>(); String inRegex = null; for (int i=1; (pt.hasMoreTokens()); i++) { String param = pt.nextToken(); if (inRegex != null) { inRegex += param; param = inRegex; } else if (",".equals(param)) { continue; } if (param.indexOf('<')>0 && param.indexOf('>') <0) { inRegex = param; } ArgumentParameter ap = new ArgumentParameter(); ap.name = ""+i; if (param.endsWith("}")) { param = parseParameterFormat(param, ap); } if (param.endsWith(">")) { inRegex = null; int regexPos = param.indexOf('<'); ap.pattern = Pattern.compile(param.substring(regexPos+1, param.length()-1)); param = param.substring(0, regexPos); if (param.endsWith("}")) { parseParameterFormat(param, ap); } else { ap.type = getType(param); } } else if (inRegex != null) { continue; } else { ap.type = getType(param); } params.add(ap); } af.params = params.toArray(new ArgumentParameter[params.size()]); } else { af.option = token; if (af.parameter) { throw new IllegalArgumentException("Parameter "+token+" has no type"); } } options.put(af.option, af); } return options; } ArgumentParser(String optionFormat, String parameterFormat); Map<String, ArgumentFormat> getOptionFormat(); Map<? extends String, ? extends ArgumentFormat> getParameterFormat(); @SuppressWarnings("unchecked") Map<String,List<?>> parse(String[] args); static Object getFirstValue(List<? extends Object> optionValues); static Object getValue(Map args, String name, int index); static void main(String[] args); static String[] selectCommand(String[] args, String optionFormat,
                                       String[][] commandSets); }
ArgumentParser { protected static Map<String, ArgumentFormat> parseFormat(String format, boolean parameterFormat) { Map<String, ArgumentFormat> options = new LinkedHashMap<String, ArgumentFormat>(); ArgumentFormat last = null; StringTokenizer st = new StringTokenizer(format, " "); while (st.hasMoreTokens()) { String token = st.nextToken(); if ("..".equals(token)) { if (last != null) { last.vararg = true; break; } else { throw new IllegalArgumentException("'..' without parameter definition"); } } ArgumentFormat af = new ArgumentFormat(); last = af; af.parameter = parameterFormat; af.mandatory = (token.charAt(0) != '+'); token = token.substring(1); if (token.endsWith("]")) { af.option = token.substring(0, token.indexOf('[')); token = token.substring(af.option.length()+1, token.length()-1); StringTokenizer pt = new StringTokenizer(token, ",", true); List<ArgumentParameter> params = new ArrayList<ArgumentParameter>(); String inRegex = null; for (int i=1; (pt.hasMoreTokens()); i++) { String param = pt.nextToken(); if (inRegex != null) { inRegex += param; param = inRegex; } else if (",".equals(param)) { continue; } if (param.indexOf('<')>0 && param.indexOf('>') <0) { inRegex = param; } ArgumentParameter ap = new ArgumentParameter(); ap.name = ""+i; if (param.endsWith("}")) { param = parseParameterFormat(param, ap); } if (param.endsWith(">")) { inRegex = null; int regexPos = param.indexOf('<'); ap.pattern = Pattern.compile(param.substring(regexPos+1, param.length()-1)); param = param.substring(0, regexPos); if (param.endsWith("}")) { parseParameterFormat(param, ap); } else { ap.type = getType(param); } } else if (inRegex != null) { continue; } else { ap.type = getType(param); } params.add(ap); } af.params = params.toArray(new ArgumentParameter[params.size()]); } else { af.option = token; if (af.parameter) { throw new IllegalArgumentException("Parameter "+token+" has no type"); } } options.put(af.option, af); } return options; } ArgumentParser(String optionFormat, String parameterFormat); Map<String, ArgumentFormat> getOptionFormat(); Map<? extends String, ? extends ArgumentFormat> getParameterFormat(); @SuppressWarnings("unchecked") Map<String,List<?>> parse(String[] args); static Object getFirstValue(List<? extends Object> optionValues); static Object getValue(Map args, String name, int index); static void main(String[] args); static String[] selectCommand(String[] args, String optionFormat,
                                       String[][] commandSets); static final String[] TYPES; static final int TYPE_INTEGER; static final int TYPE_LONG; static final int TYPE_STRING; static final int TYPE_OCTET_STRING; }