1 61 62 package org.apache.commons.cli; 63 64 import java.util.Collection ; 65 import java.util.HashMap ; 66 import java.util.Iterator ; 67 68 73 public class OptionGroup { 74 75 76 private HashMap optionMap = new HashMap (); 77 78 79 private String selected; 80 81 82 private boolean required; 83 84 90 public OptionGroup addOption(Option opt) { 91 optionMap.put( "-" + opt.getOpt(), opt ); 94 return this; 95 } 96 97 101 public Collection getNames() { 102 return optionMap.keySet(); 104 } 105 106 109 public Collection getOptions() { 110 return optionMap.values(); 112 } 113 114 120 public void setSelected(Option opt) throws AlreadySelectedException { 121 125 if ( this.selected == null || this.selected.equals( opt.getOpt() ) ) { 126 this.selected = opt.getOpt(); 127 } 128 else { 129 throw new AlreadySelectedException( "an option from this group has " + 130 "already been selected: '" + 131 selected + "'"); 132 } 133 } 134 135 138 public String getSelected() { 139 return selected; 140 } 141 142 145 public void setRequired( boolean required ) { 146 this.required = required; 147 } 148 149 154 public boolean isRequired() { 155 return this.required; 156 } 157 158 162 public String toString() { 163 StringBuffer buff = new StringBuffer (); 164 165 Iterator iter = getOptions().iterator(); 166 167 buff.append( "[" ); 168 while( iter.hasNext() ) { 169 Option option = (Option)iter.next(); 170 171 buff.append( "-" ); 172 buff.append( option.getOpt() ); 173 buff.append( " " ); 174 buff.append( option.getDescription( ) ); 175 176 if( iter.hasNext() ) { 177 buff.append( ", " ); 178 } 179 } 180 buff.append( "]" ); 181 182 return buff.toString(); 183 } 184 } 185 | Popular Tags |