1 5 6 package jena.cmdline ; 7 8 import java.io.* ; 9 import java.util.* ; 10 11 42 43 44 public class CommandLine 45 { 46 49 protected ArgHandler argHook = null ; 50 protected String usage = null ; 51 protected Map argMap = new HashMap() ; 52 protected Set argValue = new HashSet() ; 53 protected PrintStream out = System.err ; 54 55 protected List args = new ArrayList() ; 57 protected List items = new ArrayList() ; 59 60 61 62 public CommandLine() 63 { 64 } 65 66 69 public void setHook(ArgHandler argHandler) { argHook = argHandler ; } 70 71 74 public void setOutput(PrintStream out) { this.out = out ; } 75 public PrintStream getOutput() { return out ; } 76 public void setUsage(String usageMessage) { usage = usageMessage ; } 77 78 public List args() { return args ; } 79 public List items() { return items ; } 80 81 82 86 public void process(String [] argv) throws java.lang.IllegalArgumentException 87 { 88 try { 89 int i = 0 ; 90 for ( ; i < argv.length ; i++ ) 91 { 92 String argStr = argv[i] ; 93 if ( ! argStr.startsWith("-") || argStr.equals("--") || argStr.equals("-") ) 94 break ; 95 96 argStr = ArgDecl.canonicalForm(argStr) ; 97 String val = null ; 98 99 if ( argMap.containsKey(argStr) ) 100 { 101 Arg arg = new Arg(argStr) ; 102 ArgDecl argDecl = (ArgDecl)argMap.get(argStr) ; 103 104 if ( argDecl.takesValue() ) 105 { 106 if ( i == (argv.length-1) ) 107 throw new IllegalArgumentException ("No value for argument: "+arg.getName()) ; 108 val = argv[++i] ; 109 arg.setValue(val) ; 110 } 111 112 if ( argHook != null ) 114 argHook.action(argStr, val) ; 115 116 argDecl.trigger(arg) ; 117 args.add(arg) ; 118 } 119 else 120 throw new IllegalArgumentException ("Unknown argument: "+argStr) ; 122 } 123 124 if ( i < argv.length ) 126 { 127 if ( argv[i].equals("-") || argv[i].equals("--") ) 128 i++ ; 129 for ( ; i < argv.length ; i++ ) 130 items.add(argv[i]) ; 131 } 132 } catch (IllegalArgumentException ex) 133 { 134 if ( out != null ) 135 { 136 if ( usage != null ) out.println(usage) ; 137 out.println(ex.getMessage()) ; 138 } 139 throw ex ; 140 } 141 } 142 143 145 146 public boolean contains(ArgDecl argDecl) { return getArg(argDecl) != null ; } 147 148 150 151 public boolean contains(String s) { return getArg(s) != null ; } 152 153 154 159 160 public Arg getArg(ArgDecl argDecl) 161 { 162 Arg arg = null ; 163 for ( Iterator iter = args.iterator() ; iter.hasNext() ; ) 164 { 165 Arg a = (Arg)iter.next() ; 166 if ( argDecl.matches(a) ) 167 arg = a ; 168 } 169 return arg ; 170 } 171 172 public Arg getArg(String s) 173 { 174 s = ArgDecl.canonicalForm(s) ; 175 Arg arg = null ; 176 for ( Iterator iter = args.iterator() ; iter.hasNext() ; ) 177 { 178 Arg a = (Arg)iter.next() ; 179 if ( a.getName().equals(s) ) 180 arg = a ; 181 } 182 return arg ; 183 } 184 185 186 190 191 public CommandLine add(String argName, boolean hasValue) 192 { 193 return add(new ArgDecl(hasValue, argName)) ; 194 } 195 196 200 201 public CommandLine add(ArgDecl arg) 202 { 203 for ( Iterator iter = arg.getNames() ; iter.hasNext() ; ) 204 argMap.put(iter.next(), arg) ; 205 return this ; 206 } 207 208 public ArgHandler trace() 209 { 210 final PrintStream _out = out ; 211 return new ArgHandler() 212 { 213 public void action (String arg, String val) { 215 if ( _out != null ) 216 _out.println("Seen: "+arg+((val!=null)?" = "+val:"")) ; 217 } 218 } ; 219 } 220 221 222 public static void main(String [] argv) 223 { 224 CommandLine cl = new CommandLine() ; 225 cl.setOutput(System.out) ; 226 ArgDecl argA = new ArgDecl(false, "-a") ; 227 cl.add(argA) ; 228 cl.add("-b", false) ; 229 cl.add("-file", true) ; 230 231 ArgDecl argFile = new ArgDecl(false, "-v", "--verbose") ; 232 argFile.addHook(cl.trace()) ; 233 cl.add(argFile) ; 234 235 237 String [] a = new String []{"-a", "--b", "--a", "--file", "value1", "--file", "value2", "--v", "rest"} ; 238 try { 239 cl.process(a) ; 240 System.out.println("PROCESSED") ; 241 242 if ( cl.getArg("file") == null ) 244 System.out.println("No --file seen") ; 245 else 246 System.out.println("--file => "+cl.getArg("file").getValue()) ; 247 248 if ( cl.getArg(argA) == null ) 250 System.out.println("No --a seen") ; 251 else 252 System.out.println("--a seen "+cl.getArg(argFile).getValue()) ; 253 254 System.out.println("DUMP") ; 255 for ( Iterator iter = cl.args().iterator() ; iter.hasNext() ; ) 256 { 257 Arg arg = (Arg)iter.next() ; 258 String v = (arg.hasValue()? (" = "+arg.getValue()) : "") ; 259 System.out.println("Arg: "+arg.getName()+v) ; 260 } 261 for ( Iterator iter = cl.items().iterator() ; iter.hasNext() ; ) 262 System.out.println("Item: "+(String )iter.next()) ; 263 264 } catch (IllegalArgumentException ex) 265 { 266 System.err.println("Illegal argument: "+ex.getMessage() ) ; 267 } 268 } 269 } 270 271 297 | Popular Tags |