1 61 62 package org.apache.commons.cli; 63 64 import java.util.ArrayList ; 65 import java.util.Collection ; 66 import java.util.Collections ; 67 import java.util.HashMap ; 68 import java.util.Iterator ; 69 import java.util.List ; 70 import java.util.Map ; 71 import java.util.*; 72 73 88 public class Options { 89 90 91 private Map shortOpts = new HashMap (); 92 93 94 private Map longOpts = new HashMap (); 95 96 97 private List requiredOpts = new ArrayList (); 98 99 100 private Map optionGroups = new HashMap (); 101 102 104 public Options() { 105 } 106 107 113 public Options addOptionGroup( OptionGroup group ) { 114 Iterator options = group.getOptions().iterator(); 115 116 if( group.isRequired() ) { 117 requiredOpts.add( group ); 118 } 119 120 while( options.hasNext() ) { 121 Option option = (Option)options.next(); 122 option.setRequired( false ); 126 addOption( option ); 127 128 optionGroups.put( option.getOpt(), group ); 129 } 130 131 return this; 132 } 133 134 142 public Options addOption(String opt, boolean hasArg, String description) { 143 addOption( opt, null, hasArg, description ); 144 return this; 145 } 146 147 156 public Options addOption(String opt, String longOpt, boolean hasArg, String description) { 157 addOption( new Option( opt, longOpt, hasArg, description ) ); 158 return this; 159 } 160 161 167 public Options addOption(Option opt) { 168 String shortOpt = "-" + opt.getOpt(); 169 170 if ( opt.hasLongOpt() ) { 172 longOpts.put( "--" + opt.getLongOpt(), opt ); 173 } 174 175 if ( opt.isRequired() ) { 177 requiredOpts.add( shortOpt ); 178 } 179 180 shortOpts.put( shortOpt, opt ); 181 182 return this; 183 } 184 185 189 public Collection getOptions() { 190 List opts = new ArrayList ( shortOpts.values() ); 191 192 Iterator iter = longOpts.values().iterator(); 195 while (iter.hasNext()) 196 { 197 Object item = iter.next(); 198 if (!opts.contains(item)) 199 { 200 opts.add(item); 201 } 202 } 203 return Collections.unmodifiableCollection( opts ); 204 } 205 206 211 List helpOptions() { 212 return new ArrayList ( shortOpts.values() ); 213 } 214 215 220 public List getRequiredOptions() { 221 return requiredOpts; 222 } 223 224 229 public Option getOption( String opt ) { 230 231 Option option = null; 232 233 if( opt.length() == 1 ) { 235 option = (Option)shortOpts.get( "-" + opt ); 236 } 237 else if( opt.startsWith( "--" ) ) { 239 option = (Option)longOpts.get( opt ); 240 } 241 else { 243 option = (Option)shortOpts.get( opt ); 244 } 245 246 return (option == null) ? null : (Option)option.clone(); 247 } 248 249 257 public boolean hasOption( String opt ) { 258 259 if( opt.length() == 1 ) { 261 return shortOpts.containsKey( "-" + opt ); 262 } 263 else if( opt.startsWith( "--" ) ) { 265 return longOpts.containsKey( opt ); 266 } 267 else { 269 return shortOpts.containsKey( opt ); 270 } 271 } 272 273 280 public OptionGroup getOptionGroup( Option opt ) { 281 return (OptionGroup)optionGroups.get( opt.getOpt() ); 282 } 283 284 288 public String toString() { 289 StringBuffer buf = new StringBuffer (); 290 291 buf.append("[ Options: [ short "); 292 buf.append( shortOpts.toString() ); 293 buf.append( " ] [ long " ); 294 buf.append( longOpts ); 295 buf.append( " ]"); 296 297 return buf.toString(); 298 } 299 } 300 | Popular Tags |