1 package org.incava.jagol; 2 3 import java.io.*; 4 import java.util.*; 5 import org.incava.lang.StringExt; 6 7 8 11 public class ListOption extends Option 12 { 13 private List value; 14 15 18 public ListOption(String longName, String description) 19 { 20 this(longName, description, new ArrayList()); 21 } 22 23 26 public ListOption(String longName, String description, List value) 27 { 28 super(longName, description); 29 this.value = value; 30 } 31 32 35 public List getValue() 36 { 37 return value; 38 } 39 40 43 public void setValue(List value) 44 { 45 this.value = value; 46 } 47 48 52 public void setValue(String value) throws InvalidTypeException 53 { 54 tr.Ace.log("value: '" + value + "'"); 55 parse(value); 56 } 57 58 63 public boolean set(String arg, List args) throws OptionException 64 { 65 tr.Ace.log("arg: " + arg + "; args: " + args); 66 67 if (arg.equals("--" + longName)) { 68 tr.Ace.log("matched long name"); 69 70 if (args.size() == 0) { 71 throw new InvalidTypeException(longName + " expects following argument"); 72 } 73 else { 74 String value = (String )args.remove(0); 75 setValue(value); 76 } 77 } 78 else if (arg.startsWith("--" + longName + "=")) { 79 tr.Ace.log("matched long name + equals"); 80 81 int pos = ("--" + longName + "=").length(); 83 tr.Ace.log("position: " + pos); 84 if (pos >= arg.length()) { 85 throw new InvalidTypeException(longName + " expects argument"); 86 } 87 else { 88 String value = arg.substring(pos); 89 setValue(value); 90 } 91 } 92 else if (shortName != 0 && arg.equals("-" + shortName)) { 93 tr.Ace.log("matched short name"); 94 95 if (args.size() == 0) { 96 throw new InvalidTypeException(shortName + " expects following argument"); 97 } 98 else { 99 String value = (String )args.remove(0); 100 setValue(value); 101 } 102 } 103 else { 104 tr.Ace.log("not a match"); 105 return false; 106 } 107 return true; 108 } 109 110 116 protected void parse(String str) throws InvalidTypeException 117 { 118 List list = StringExt.listify(str); 119 Iterator it = list.iterator(); 120 while (it.hasNext()) { 121 String s = (String )it.next(); 122 if (!s.equals("+=")) { 123 value.add(convert(s)); 124 } 125 } 126 } 127 128 132 protected Object convert(String str) throws InvalidTypeException 133 { 134 return str; 135 } 136 137 public String toString() 138 { 139 StringBuffer buf = new StringBuffer (); 140 Iterator it = value.iterator(); 141 boolean isFirst = true; 142 while (it.hasNext()) { 143 if (isFirst) { 144 isFirst = false; 145 } 146 else { 147 buf.append(", "); 148 } 149 buf.append(it.next()); 150 } 151 return buf.toString(); 152 } 153 154 } 155 | Popular Tags |