1 26 27 package org.objectweb.util.cmdline.lib; 28 29 import java.util.Iterator ; 30 31 import org.objectweb.util.cmdline.api.Option; 32 import org.objectweb.util.cmdline.api.Console; 33 import org.objectweb.util.cmdline.api.CommandLine; 34 35 43 44 public class DefaultCommandLine 45 extends DefaultUsage 46 implements CommandLine 47 { 48 54 55 private Console console_; 56 57 58 private java.util.Vector options_; 59 60 61 private int maxOptionLength_; 62 63 64 private boolean checkingArguments_; 65 66 72 78 public 79 DefaultCommandLine() 80 { 81 this("", "", (String [])null, false); 82 } 83 84 92 public 93 DefaultCommandLine(boolean with_help) 94 { 95 this("", "", (String [])null, with_help); 96 } 97 98 107 public 108 DefaultCommandLine(String label, 109 String arguments, 110 String description, 111 boolean with_help) 112 { 113 this(label, arguments, new String [] { description }, with_help); 114 } 115 116 125 public 126 DefaultCommandLine(String [] labels, 127 String arguments, 128 String description, 129 boolean with_help) 130 { 131 this(labels, arguments, new String [] { description }, with_help); 132 } 133 134 143 public 144 DefaultCommandLine(String label, 145 String arguments, 146 String [] description, 147 boolean with_help) 148 { 149 this(new String [] { label }, arguments, description, with_help); 150 } 151 152 161 public 162 DefaultCommandLine(String [] labels, 163 String arguments, 164 String [] description, 165 boolean with_help) 166 { 167 this(labels, new String [] { arguments }, description, with_help); 168 } 169 170 179 public 180 DefaultCommandLine(String [] labels, 181 String [] arguments, 182 String [] description, 183 boolean with_help) 184 { 185 this(labels,arguments,false,description,with_help); 186 } 187 188 198 public DefaultCommandLine(String [] labels, 199 String [] arguments, 200 boolean additional, 201 String [] description, 202 boolean with_help) 203 { 204 super(labels, arguments, additional, description); 205 206 options_ = new java.util.Vector (); 207 maxOptionLength_ = 0; 208 209 if(with_help) { 210 addOption(new DefaultOptionHelp()); 211 } 212 console_ = null; 213 214 checkingArguments_ = true; 216 } 217 218 219 229 protected void 230 exit(String message) { 231 console_.getErrorStream().println(getLabels()[0] + ": " + message + '!'); 232 print(console_.getErrorStream()); 233 System.exit(-1); 234 } 235 236 243 static protected int 244 sumStringLength(String [] strings) { 245 int result = 0; 246 for(int i=0; i<strings.length; i++) 247 result += strings[i].length(); 248 return result; 249 } 250 251 259 static protected void 260 print(java.io.PrintStream stream, 261 String [] strings, 262 String sep_first, 263 String sep_next) 264 { 265 if(strings.length > 0) 266 { 267 stream.print(sep_first); 268 stream.print(strings[0]); 269 for(int i=1; i<strings.length; i++) 270 { 271 stream.print(sep_next); 272 stream.print(strings[i]); 273 } 274 } 275 } 276 277 283 288 public void 289 print(java.io.PrintStream stream) { 290 stream.print("Usage: "); 294 stream.print(getLabels()[0]); 295 296 for(Iterator iterator=options_.iterator();iterator.hasNext();) 298 { 299 stream.print(' '); 300 301 Option option = (Option)iterator.next(); 302 303 if(!option.isMandatory()) 304 stream.print('['); 305 306 print(stream, option.getLabels(), "", "|"); 307 308 print(stream, option.getArguments(), " ", " "); 309 310 if(!option.isMandatory()) 311 stream.print(']'); 312 } 313 314 if (getAdditionalArguments()) 315 print(stream, new String []{"[Additional]"}, " ", " "); 316 317 print(stream, getArguments(), " ", " "); 318 319 stream.println("\n\nOptions:"); 323 324 for(Iterator iterator=options_.iterator();iterator.hasNext();) 326 { 327 int nb_characters = 0; 328 329 Option option = (Option)iterator.next(); 330 331 String [] labels = option.getLabels(); 332 print(stream, labels, " ", ", "); 333 nb_characters = nb_characters + 334 sumStringLength(labels) + 335 labels.length * 2; 336 337 String [] arguments = option.getArguments(); 338 print(stream, arguments, " ", " "); 339 nb_characters = nb_characters + 340 sumStringLength(arguments) + 341 arguments.length; 342 343 String [] description = option.getDescription(); 344 for(int i=0; i<description.length; i++) 345 { 346 for(int j = nb_characters; 347 j <= maxOptionLength_; j++) 348 { 349 stream.print(' '); 350 } 351 stream.println(description[i]); 352 nb_characters = 0; 353 } 354 } 355 stream.println(""); 356 357 stream.println("Description: "); 361 print(stream, getDescription(), " ", "\n "); 362 363 stream.println("\n"); 364 } 365 366 372 377 public boolean 378 isCheckingArguments() { 379 return checkingArguments_; 380 } 381 382 387 public void 388 setCheckingArguments(boolean checkingArguments){ 389 checkingArguments_ = checkingArguments; 390 } 391 392 397 public Option[] 398 getOptions() 399 { 400 return ((Option[])(options_.toArray(new Option[0]))); 401 } 402 403 408 public void 409 addOption(Option option) 410 { 411 options_.addElement(option); 412 413 int size = sumStringLength(option.getLabels()) + 414 option.getLabels().length * 2 + 415 sumStringLength(option.getArguments()) + 416 option.getArguments().length; 417 418 if(size > maxOptionLength_) { 419 maxOptionLength_ = size; 420 } 421 } 422 423 431 public String [] 432 parse(String [] args) { 433 DefaultIterator argumentsIterator = new DefaultIterator(args, this); 435 436 for(;argumentsIterator.hasNext();) { 438 String currentArgument = argumentsIterator.next(); 439 440 for(Iterator iterator=options_.iterator();iterator.hasNext();) { 442 Option option = (Option)iterator.next(); 443 if(option.check(currentArgument)) { 444 argumentsIterator.remove(); 445 try { 446 option.consume(argumentsIterator); 447 } catch(Error exc) { 448 exit(exc.getMessage() + " for option " + OptionHelper.toString(option)); 449 } 450 break; 451 } 452 } 453 } 454 455 for(Iterator iterator=options_.iterator();iterator.hasNext();) { 460 Option option = (Option)iterator.next(); 461 462 if(option.isMandatory() && !option.isSet()) { 464 exit("Missed mandatory option " + 465 OptionHelper.toString(option)); 466 } 467 } 468 469 String [] arguments = argumentsIterator.getUnparsedArguments(); 471 472 if (isCheckingArguments()) { 474 if(arguments.length < getArguments().length) 476 exit("Not enough arguments"); 477 478 if((!getAdditionalArguments())&&(arguments.length > getArguments().length)) 479 exit("Too many arguments"); 480 } 481 return arguments; 483 } 484 485 491 496 public Console 497 getConsole() 498 { 499 return console_; 500 } 501 502 507 public void 508 setConsole(Console console) 509 { 510 console_ = console; 511 } 512 513 519 } 520 | Popular Tags |