1 61 62 package org.apache.commons.cli; 63 64 73 public class OptionBuilder { 74 75 76 private static String longopt; 77 78 private static String description; 79 80 private static String argName; 81 82 private static boolean required; 83 84 private static int numberOfArgs = Option.UNINITIALIZED; 85 86 private static Object type; 87 88 private static boolean optionalArg; 89 90 private static char valuesep; 91 92 93 private static OptionBuilder instance = new OptionBuilder(); 94 95 private OptionBuilder() { 97 } 98 99 102 private static void reset() { 103 description = null; 104 argName = null; 105 longopt = null; 106 type = null; 107 required = false; 108 numberOfArgs = Option.UNINITIALIZED; 109 110 optionalArg = false; 112 valuesep = (char) 0; 113 } 114 115 121 public static OptionBuilder withLongOpt( String longopt ) { 122 instance.longopt = longopt; 123 return instance; 124 } 125 126 131 public static OptionBuilder hasArg( ) { 132 instance.numberOfArgs = 1; 133 return instance; 134 } 135 136 143 public static OptionBuilder hasArg( boolean hasArg ) { 144 instance.numberOfArgs = ( hasArg == true ) ? 1 : Option.UNINITIALIZED; 145 return instance; 146 } 147 148 155 public static OptionBuilder withArgName( String name ) { 156 instance.argName = name; 157 return instance; 158 } 159 160 165 public static OptionBuilder isRequired( ) { 166 instance.required = true; 167 return instance; 168 } 169 170 186 public static OptionBuilder withValueSeparator( char sep ) { 187 instance.valuesep = sep; 188 return instance; 189 } 190 191 207 public static OptionBuilder withValueSeparator( ) { 208 instance.valuesep = '='; 209 return instance; 210 } 211 212 219 public static OptionBuilder isRequired( boolean required ) { 220 instance.required = required; 221 return instance; 222 } 223 224 229 public static OptionBuilder hasArgs( ) { 230 instance.numberOfArgs = Option.UNLIMITED_VALUES; 231 return instance; 232 } 233 234 241 public static OptionBuilder hasArgs( int num ) { 242 instance.numberOfArgs = num; 243 return instance; 244 } 245 246 251 public static OptionBuilder hasOptionalArg( ) { 252 instance.numberOfArgs = 1; 253 instance.optionalArg = true; 254 return instance; 255 } 256 257 263 public static OptionBuilder hasOptionalArgs( ) { 264 instance.numberOfArgs = Option.UNLIMITED_VALUES; 265 instance.optionalArg = true; 266 return instance; 267 } 268 269 277 public static OptionBuilder hasOptionalArgs( int numArgs ) { 278 instance.numberOfArgs = numArgs; 279 instance.optionalArg = true; 280 return instance; 281 } 282 283 290 public static OptionBuilder withType( Object type ) { 291 instance.type = type; 292 return instance; 293 } 294 295 301 public static OptionBuilder withDescription( String description ) { 302 instance.description = description; 303 return instance; 304 } 305 306 315 public static Option create( char opt ) 316 throws IllegalArgumentException 317 { 318 return create( String.valueOf( opt ) ); 319 } 320 321 328 public static Option create() 329 throws IllegalArgumentException 330 { 331 if( longopt == null ) { 332 throw new IllegalArgumentException ( "must specify longopt" ); 333 } 334 335 return create( " " ); 336 } 337 338 348 public static Option create( String opt ) 349 throws IllegalArgumentException 350 { 351 Option option = new Option( opt, description ); 353 354 option.setLongOpt( longopt ); 356 option.setRequired( required ); 357 option.setOptionalArg( optionalArg ); 358 option.setArgs( numberOfArgs ); 359 option.setType( type ); 360 option.setValueSeparator( valuesep ); 361 option.setArgName( argName ); 362 instance.reset(); 364 365 return option; 367 } 368 } | Popular Tags |