1 17 package org.apache.commons.cli.avalon; 18 20 import java.util.Arrays ; 21 22 27 public final class CLOption 28 { 29 32 public static final int TEXT_ARGUMENT = 0; 33 34 37 private static final CLOptionDescriptor TEXT_ARGUMENT_DESCRIPTOR = 38 new CLOptionDescriptor( null, CLOptionDescriptor.ARGUMENT_OPTIONAL, TEXT_ARGUMENT, 39 null ); 40 41 private String [] m_arguments; 42 private CLOptionDescriptor m_descriptor = TEXT_ARGUMENT_DESCRIPTOR; 43 44 49 public final String getArgument() 50 { 51 return getArgument( 0 ); 52 } 53 54 61 public final String getArgument( final int index ) 62 { 63 if( null == m_arguments || index < 0 || index >= m_arguments.length ) 64 { 65 return null; 66 } 67 else 68 { 69 return m_arguments[index]; 70 } 71 } 72 73 81 public final int getId() 82 { 83 return m_descriptor == null ? TEXT_ARGUMENT : m_descriptor.getId(); 84 } 85 86 public final CLOptionDescriptor getDescriptor() 87 { 88 return m_descriptor; 89 } 90 91 96 public CLOption( final CLOptionDescriptor descriptor ) 97 { 98 if( descriptor != null ) 99 { 100 m_descriptor = descriptor; 101 } 102 } 103 104 109 public CLOption( final String argument ) 110 { 111 this( (CLOptionDescriptor)null ); 112 addArgument( argument ); 113 } 114 115 120 public final void addArgument( final String argument ) 121 { 122 if( null == m_arguments ) 123 { 124 m_arguments = new String []{argument}; 125 } 126 else 127 { 128 final String [] arguments = new String [m_arguments.length + 1]; 129 System.arraycopy( m_arguments, 0, arguments, 0, m_arguments.length ); 130 arguments[m_arguments.length] = argument; 131 m_arguments = arguments; 132 } 133 } 134 135 140 public final int getArgumentCount() 141 { 142 if( null == m_arguments ) 143 { 144 return 0; 145 } 146 else 147 { 148 return m_arguments.length; 149 } 150 } 151 152 157 public final String toString() 158 { 159 final StringBuffer sb = new StringBuffer (); 160 sb.append( "[Option " ); 161 sb.append( (char)m_descriptor.getId() ); 162 163 if( null != m_arguments ) 164 { 165 sb.append( ", " ); 166 sb.append( Arrays.asList( m_arguments ) ); 167 } 168 169 sb.append( " ]" ); 170 171 return sb.toString(); 172 } 173 } 174 | Popular Tags |