5858 * <http://www.apache.org/>.
5959 *
6060 */
61-
62- /*
63- * Copyright (C) The Apache Software Foundation. All rights reserved.
64- *
65- * This software is published under the terms of the Apache Software License
66- * version 1.1, a copy of which has been included with this distribution in
67- * the LICENSE file.
68- *
69- * $Id: Option.java,v 1.6 2002/06/06 22:50:14 bayard Exp $
70- */
71-
7261package org .apache .commons .cli ;
7362
7463import java .util .ArrayList ;
8877 * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
8978 * @version $Revision: 1.6 $
9079 */
91-
9280public class Option implements Cloneable {
9381
9482 /** constant that specifies the number of argument values has not been specified */
@@ -97,7 +85,7 @@ public class Option implements Cloneable {
9785 /** constant that specifies the number of argument values is infinite */
9886 public final static int UNLIMITED_VALUES = -2 ;
9987
100- /** opt the single character representation of the option */
88+ /** opt the name of the option */
10189 private String opt ;
10290
10391 /** longOpt is the long representation of the option */
@@ -130,85 +118,9 @@ public class Option implements Cloneable {
130118 /** the list of argument values **/
131119 private ArrayList values = new ArrayList ();
132120
133- /** option char (only valid for single character options) */
134- private char id ;
135-
136121 /** the character that is the value separator */
137122 private char valuesep ;
138123
139- /**
140- * <p>Validates whether <code>opt</code> is a permissable Option
141- * shortOpt. The rules that specify if the <code>opt</code>
142- * is valid are:</p>
143- * <ul>
144- * <li><code>opt</code> is not NULL</li>
145- * <li>a single character <code>opt</code> that is either
146- * ' '(special case), '?', '@' or a letter</li>
147- * <li>a multi character <code>opt</code> that only contains
148- * letters.</li>
149- * </ul>
150- *
151- * @param opt The option string to validate
152- * @throws IllegalArgumentException if the Option is not valid.
153- */
154- private void validateOption ( String opt )
155- throws IllegalArgumentException
156- {
157- // check that opt is not NULL
158- if ( opt == null ) {
159- throw new IllegalArgumentException ( "opt is null" );
160- }
161- // handle the single character opt
162- else if ( opt .length () == 1 ) {
163- char ch = opt .charAt ( 0 );
164- if ( !isValidOpt ( ch ) ) {
165- throw new IllegalArgumentException ( "illegal option value '"
166- + ch + "'" );
167- }
168- id = ch ;
169- }
170- // handle the multi character opt
171- else {
172- char [] chars = opt .toCharArray ();
173- for ( int i = 0 ; i < chars .length ; i ++ ) {
174- if ( !isValidChar ( chars [i ] ) ) {
175- throw new IllegalArgumentException ( "opt contains illegal character value '" + chars [i ] + "'" );
176- }
177- }
178- }
179- }
180-
181- /**
182- * <p>Returns whether the specified character is a valid Option.</p>
183- *
184- * @param c the option to validate
185- * @return true if <code>c</code> is a letter, ' ', '?' or '@', otherwise false.
186- */
187- private boolean isValidOpt ( char c ) {
188- return ( isValidChar ( c ) || c == ' ' || c == '?' || c == '@' );
189- }
190-
191- /**
192- * <p>Returns whether the specified character is a valid character.</p>
193- *
194- * @param c the character to validate
195- * @return true if <code>c</code> is a letter.
196- */
197- private boolean isValidChar ( char c ) {
198- return Character .isJavaIdentifierPart ( c );
199- }
200-
201- /**
202- * <p>Returns the id of this Option. This is only set when the
203- * Option shortOpt is a single character. This is used for switch
204- * statements.</p>
205- *
206- * @return the id of this Option
207- */
208- public int getId ( ) {
209- return id ;
210- }
211-
212124 /**
213125 * Creates an Option using the specified parameters.
214126 *
@@ -247,7 +159,7 @@ public Option( String opt, String longOpt, boolean hasArg, String description )
247159 throws IllegalArgumentException
248160 {
249161 // ensure that the option is valid
250- validateOption ( opt );
162+ OptionValidator . validateOption ( opt );
251163
252164 this .opt = opt ;
253165 this .longOpt = longOpt ;
@@ -261,6 +173,30 @@ public Option( String opt, String longOpt, boolean hasArg, String description )
261173 this .description = description ;
262174 }
263175
176+ /**
177+ * <p>Returns the id of this Option. This is only set when the
178+ * Option shortOpt is a single character. This is used for switch
179+ * statements.</p>
180+ *
181+ * @return the id of this Option
182+ */
183+ public int getId ( ) {
184+ return getKey ().charAt ( 0 );
185+ }
186+
187+ /**
188+ * <p>Returns the 'unique' Option identifier.</p>
189+ *
190+ * @return the 'unique' Option identifier
191+ */
192+ String getKey () {
193+ // if 'opt' is null, then it is a 'long' option
194+ if ( opt == null ) {
195+ return this .longOpt ;
196+ }
197+ return this .opt ;
198+ }
199+
264200 /** <p>Retrieve the name of this Option.</p>
265201 *
266202 * <p>It is this String which can be used with
@@ -444,37 +380,8 @@ public int getArgs( ) {
444380 return this .numberOfArgs ;
445381 }
446382
447- /**
448- * <p>Dump state, suitable for debugging.</p>
449- *
450- * @return Stringified form of this object
451- */
452- public String toString () {
453- StringBuffer buf = new StringBuffer ().append ("[ option: " );
454-
455- buf .append ( this .opt );
456-
457- if ( this .longOpt != null ) {
458- buf .append (" " )
459- .append (this .longOpt );
460- }
461-
462- buf .append (" " );
463-
464- if ( hasArg ) {
465- buf .append ( "+ARG" );
466- }
467-
468- buf .append (" :: " )
469- .append ( this .description );
470-
471- if ( this .type != null ) {
472- buf .append (" :: " )
473- .append ( this .type );
474- }
475-
476- buf .append (" ]" );
477- return buf .toString ();
383+ public void clearValues () {
384+ this .values .clear ();
478385 }
479386
480387 /**
@@ -572,4 +479,38 @@ public Object clone() {
572479 option .setValueSeparator ( getValueSeparator () );
573480 return option ;
574481 }
482+
483+ /**
484+ * <p>Dump state, suitable for debugging.</p>
485+ *
486+ * @return Stringified form of this object
487+ */
488+ public String toString () {
489+ StringBuffer buf = new StringBuffer ().append ("[ option: " );
490+
491+ buf .append ( this .opt );
492+
493+ if ( this .longOpt != null ) {
494+ buf .append (" " )
495+ .append (this .longOpt );
496+ }
497+
498+ buf .append (" " );
499+
500+ if ( hasArg ) {
501+ buf .append ( "+ARG" );
502+ }
503+
504+ buf .append (" :: " )
505+ .append ( this .description );
506+
507+ if ( this .type != null ) {
508+ buf .append (" :: " )
509+ .append ( this .type );
510+ }
511+
512+ buf .append (" ]" );
513+ return buf .toString ();
514+ }
515+
575516}
0 commit comments