1:
17: package ;
18:
19: import ;
20: import ;
21: import ;
22: import ;
23: import ;
24: import ;
25: import ;
26: import ;
27:
28: import ;
29: import ;
30: import ;
31: import ;
32: import ;
33: import ;
34:
35:
41: public class DefaultOption extends ParentImpl
42: {
43:
46: public static final String DEFAULT_SHORT_PREFIX = "-";
47:
48:
51: public static final String DEFAULT_LONG_PREFIX = "--";
52:
53:
56: public static final boolean DEFAULT_BURST_ENABLED = true;
57:
58: private final String m_preferredName;
59: private final Set m_aliases;
60: private final Set m_burstAliases;
61: private final Set m_triggers;
62: private final Set m_prefixes;
63: private final String m_shortPrefix;
64: private final boolean m_burstEnabled;
65: private final int m_burstLength;
66:
67:
85: public DefaultOption(
86: final String shortPrefix, final String longPrefix, final boolean burstEnabled,
87: final String preferredName, final String description, final Set aliases,
88: final Set burstAliases, final boolean required, final Argument argument,
89: final Group children, final int id )
90: throws IllegalArgumentException
91: {
92: super( argument, children, description, id, required );
93:
94: m_shortPrefix = shortPrefix;
95: m_burstEnabled = burstEnabled;
96: m_burstLength = shortPrefix.length() + 1;
97: m_preferredName = preferredName;
98:
99: if( aliases == null )
100: {
101: m_aliases = Collections.EMPTY_SET;
102: }
103: else
104: {
105: m_aliases = Collections.unmodifiableSet( new HashSet( aliases ) );
106: }
107:
108: if( burstAliases == null )
109: {
110: m_burstAliases = Collections.EMPTY_SET;
111: }
112: else
113: {
114: m_burstAliases = Collections.unmodifiableSet( new HashSet( burstAliases ) );
115: }
116:
117: final Set newTriggers = new HashSet();
118: newTriggers.add( m_preferredName );
119: newTriggers.addAll( m_aliases );
120: newTriggers.addAll( m_burstAliases );
121: m_triggers = Collections.unmodifiableSet( newTriggers );
122:
123: final Set newPrefixes = new HashSet( super.getPrefixes() );
124: newPrefixes.add( m_shortPrefix );
125: newPrefixes.add( longPrefix );
126: m_prefixes = Collections.unmodifiableSet( newPrefixes );
127:
128: checkPrefixes( newPrefixes );
129: }
130:
131:
139: public boolean canProcess(
140: final WriteableCommandLine commandLine, final String argument )
141: {
142: return
143: ( argument != null )
144: && (
145: super.canProcess( commandLine, argument )
146: || (
147: ( argument.length() >= m_burstLength )
148: && m_burstAliases.contains(
149: argument.substring( 0, m_burstLength ) )
150: )
151: );
152: }
153:
154:
160: public void processParent( WriteableCommandLine commandLine, ListIterator arguments )
161: throws OptionException
162: {
163: final String argument = (String) arguments.next();
164:
165: if( m_triggers.contains( argument ) )
166: {
167: commandLine.addOption( this );
168: arguments.set( m_preferredName );
169: }
170: else if( m_burstEnabled && ( argument.length() >= m_burstLength ) )
171: {
172: final String burst = argument.substring( 0, m_burstLength );
173: if( m_burstAliases.contains( burst ) )
174: {
175: commandLine.addOption( this );
176:
177: arguments.set( m_preferredName );
178:
179: if( getArgument() == null )
180: {
181: arguments.add( m_shortPrefix + argument.substring( m_burstLength ) );
182: }
183: else
184: {
185: arguments.add( argument.substring( m_burstLength ) );
186: }
187: arguments.previous();
188: }
189: else
190: {
191: throw new OptionException(
192: this, ResourceConstants.CANNOT_BURST, argument );
193: }
194: }
195: else
196: {
197: throw new OptionException(
198: this,
199: ResourceConstants.UNEXPECTED_TOKEN,
200: argument );
201: }
202: }
203:
204:
213: public Set getTriggers()
214: {
215: return m_triggers;
216: }
217:
218:
228: public Set getPrefixes()
229: {
230: return m_prefixes;
231: }
232:
233:
240: public void validate( WriteableCommandLine commandLine )
241: throws OptionException
242: {
243: if( isRequired() && !commandLine.hasOption( this ) )
244: {
245: throw new OptionException(
246: this,
247: ResourceConstants.OPTION_MISSING_REQUIRED,
248: getPreferredName() );
249: }
250: super.validate( commandLine );
251: }
252:
253:
260: public void appendUsage(
261: final StringBuffer buffer, final Set helpSettings, final Comparator comp )
262: {
263:
264: final boolean optional =
265: !isRequired()
266: && helpSettings.contains( DisplaySetting.DISPLAY_OPTIONAL );
267:
268: final boolean displayAliases =
269: helpSettings.contains( DisplaySetting.DISPLAY_ALIASES );
270:
271: if( optional )
272: {
273: buffer.append( '[' );
274: }
275:
276: buffer.append( m_preferredName );
277:
278: if( displayAliases && !m_aliases.isEmpty() )
279: {
280: buffer.append( " (" );
281:
282: final List list = new ArrayList( m_aliases );
283: Collections.sort( list );
284: for( final Iterator i = list.iterator(); i.hasNext();)
285: {
286: final String alias = (String) i.next();
287: buffer.append( alias );
288: if( i.hasNext() )
289: {
290: buffer.append( ',' );
291: }
292: }
293: buffer.append( ')' );
294: }
295:
296: super.appendUsage( buffer, helpSettings, comp );
297:
298: if( optional )
299: {
300: buffer.append( ']' );
301: }
302: }
303:
304:
310: public String getPreferredName()
311: {
312: return m_preferredName;
313: }
314: }