1 50 package org.apache.avalon.excalibur.cli; 51 52 import java.util.Arrays ; 53 54 63 public final class CLOption 64 { 65 68 public static final int TEXT_ARGUMENT = 0; 69 70 73 private static final CLOptionDescriptor TEXT_ARGUMENT_DESCRIPTOR = 74 new CLOptionDescriptor( null, CLOptionDescriptor.ARGUMENT_OPTIONAL, TEXT_ARGUMENT, null ); 75 76 private String [] m_arguments; 77 private CLOptionDescriptor m_descriptor = TEXT_ARGUMENT_DESCRIPTOR; 78 79 84 public final String getArgument() 85 { 86 return getArgument( 0 ); 87 } 88 89 96 public final String getArgument( final int index ) 97 { 98 if( null == m_arguments || index < 0 || index >= m_arguments.length ) 99 { 100 return null; 101 } 102 else 103 { 104 return m_arguments[ index ]; 105 } 106 } 107 108 116 public final int getId() 117 { 118 return m_descriptor == null ? TEXT_ARGUMENT : m_descriptor.getId(); 119 } 120 121 public final CLOptionDescriptor getDescriptor() 122 { 123 return m_descriptor; 124 } 125 126 131 public CLOption( final CLOptionDescriptor descriptor ) 132 { 133 if( descriptor != null ) 134 { 135 m_descriptor = descriptor; 136 } 137 } 138 139 144 public CLOption( final String argument ) 145 { 146 this( (CLOptionDescriptor)null ); 147 addArgument( argument ); 148 } 149 150 155 public final void addArgument( final String argument ) 156 { 157 if( null == m_arguments ) 158 { 159 m_arguments = new String []{argument}; 160 } 161 else 162 { 163 final String [] arguments = new String [ m_arguments.length + 1 ]; 164 System.arraycopy( m_arguments, 0, arguments, 0, m_arguments.length ); 165 arguments[ m_arguments.length ] = argument; 166 m_arguments = arguments; 167 } 168 } 169 170 175 public final int getArgumentCount() 176 { 177 if( null == m_arguments ) 178 { 179 return 0; 180 } 181 else 182 { 183 return m_arguments.length; 184 } 185 } 186 187 192 public final String toString() 193 { 194 final StringBuffer sb = new StringBuffer (); 195 sb.append( "[Option " ); 196 sb.append( (char)m_descriptor.getId() ); 197 198 if( null != m_arguments ) 199 { 200 sb.append( ", " ); 201 sb.append( Arrays.asList( m_arguments ) ); 202 } 203 204 sb.append( " ]" ); 205 206 return sb.toString(); 207 } 208 } 209 | Popular Tags |