1818import java .util .ArrayList ;
1919import java .util .Collections ;
2020import java .util .HashMap ;
21- import java .util .HashSet ;
2221import java .util .Iterator ;
2322import java .util .List ;
2423import java .util .Map ;
2524import java .util .Properties ;
2625import java .util .Set ;
2726
2827import org .apache .commons .cli2 .Argument ;
29- import org .apache .commons .cli2 .CommandLine ;
3028import org .apache .commons .cli2 .Option ;
3129import org .apache .commons .cli2 .WriteableCommandLine ;
3230
@@ -41,12 +39,11 @@ public class WriteableCommandLineImpl extends CommandLineImpl implements Writeab
4139 private final Map nameToOption = new HashMap ();
4240 private final Map values = new HashMap ();
4341 private final Map switches = new HashMap ();
44- private final Map defaults = new HashMap ();
42+ private final Map defaultValues = new HashMap ();
43+ private final Map defaultSwitches = new HashMap ();
4544 private final List normalised ;
4645 private final Set prefixes ;
4746
48- private CommandLine defaultCommandLine = null ;
49-
5047 /**
5148 * Creates a new WriteableCommandLineImpl rooted on the specified Option, to
5249 * hold the parsed arguments.
@@ -91,38 +88,26 @@ public void addSwitch(final Option option, final boolean value) {
9188
9289 public boolean hasOption (final Option option ) {
9390 final boolean present = options .contains (option );
94- if (!present && defaultCommandLine != null ) {
95- return defaultCommandLine .hasOption (option );
96- }
97- else {
98- return present ;
99- }
91+ return present ;
10092 }
10193
10294 public Option getOption (final String trigger ) {
10395 return (Option )nameToOption .get (trigger );
10496 }
10597
106- //TODO Document the order of values and defaults
10798 public List getValues (final Option option , final List defaultValues ) {
10899
109100 // First grab the command line values
110101 List valueList = (List )values .get (option );
111102
112- // Secondly try alternate CommandLines
113- if ((valueList == null || valueList .isEmpty ())
114- && defaultCommandLine != null ) {
115- valueList = defaultCommandLine .getValues (option , null );
116- }
117-
118- // Thirdly try the defaults supplied to the method
103+ // Secondly try the defaults supplied to the method
119104 if (valueList == null || valueList .isEmpty ()) {
120105 valueList = defaultValues ;
121106 }
122107
123- // Fourthly try the option's default values
108+ // Thirdly try the option's default values
124109 if (valueList == null || valueList .isEmpty ()) {
125- valueList = (List )this .defaults .get (option );
110+ valueList = (List )this .defaultValues .get (option );
126111 }
127112
128113 // Finally use an empty list
@@ -137,18 +122,15 @@ public Boolean getSwitch(final Option option, final Boolean defaultValue) {
137122 // First grab the command line values
138123 Boolean bool = (Boolean )switches .get (option );
139124
140- // Secondly try alternate CommandLines
141- if (bool == null && defaultCommandLine != null ) {
142- bool = defaultCommandLine .getSwitch (option );
143- }
144-
145- // Thirdly try the defaults supplied to the method
125+ // Secondly try the defaults supplied to the method
146126 if (bool == null ) {
147127 bool = defaultValue ;
148128 }
149129
150- // Fourthly try the option's default values
151- //????
130+ // Thirdly try the option's default values
131+ if (bool == null ) {
132+ bool = (Boolean )this .defaultSwitches .get (option );
133+ }
152134
153135 return bool ;
154136 }
@@ -162,15 +144,7 @@ public String getProperty(final String property, final String defaultValue) {
162144 }
163145
164146 public Set getProperties () {
165- if (defaultCommandLine == null ) {
166- return Collections .unmodifiableSet (properties .keySet ());
167- }
168- else {
169- final Set props = new HashSet ();
170- props .addAll (properties .keySet ());
171- props .addAll (defaultCommandLine .getProperties ());
172- return Collections .unmodifiableSet (props );
173- }
147+ return Collections .unmodifiableSet (properties .keySet ());
174148 }
175149
176150 public boolean looksLikeOption (final String trigger ) {
@@ -212,4 +186,22 @@ public List getOptions() {
212186 public Set getOptionTriggers () {
213187 return Collections .unmodifiableSet (nameToOption .keySet ());
214188 }
189+
190+ public void setDefaultValues (final Option option , final List defaults ) {
191+ if (defaults ==null ) {
192+ defaultValues .remove (option );
193+ }
194+ else {
195+ defaultValues .put (option , defaults );
196+ }
197+ }
198+
199+ public void setDefaultSwitch (final Option option , final Boolean defaultSwitch ) {
200+ if (defaultSwitch ==null ) {
201+ defaultSwitches .remove (defaultSwitch );
202+ }
203+ else {
204+ defaultSwitches .put (option , defaultSwitch );
205+ }
206+ }
215207}
0 commit comments