| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
package joptsimple; |
| 7 | |
|
| 8 | |
import java.lang.reflect.Constructor; |
| 9 | |
import java.lang.reflect.Member; |
| 10 | |
import java.lang.reflect.Method; |
| 11 | |
import java.util.List; |
| 12 | |
import java.util.StringTokenizer; |
| 13 | |
|
| 14 | |
import joptsimple.internal.Reflection; |
| 15 | |
import joptsimple.internal.ReflectionException; |
| 16 | |
import joptsimple.internal.Strings; |
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | |
|
| 22 | |
|
| 23 | |
|
| 24 | |
|
| 25 | |
|
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | |
|
| 33 | |
|
| 34 | |
|
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
public abstract class ArgumentAcceptingOptionSpec extends OptionSpec { |
| 39 | |
private static final char NIL_VALUE_SEPARATOR = '\u0000'; |
| 40 | |
private final boolean argumentRequired; |
| 41 | |
private Member converter; |
| 42 | 496 | private String argumentDescription = ""; |
| 43 | 496 | private String valueSeparator = String.valueOf( NIL_VALUE_SEPARATOR ); |
| 44 | |
|
| 45 | |
ArgumentAcceptingOptionSpec( String option, boolean argumentRequired ) { |
| 46 | 194 | super( option ); |
| 47 | |
|
| 48 | 194 | this.argumentRequired = argumentRequired; |
| 49 | 194 | } |
| 50 | |
|
| 51 | |
ArgumentAcceptingOptionSpec( List options, boolean argumentRequired, |
| 52 | |
String description ) { |
| 53 | |
|
| 54 | 302 | super( options, description ); |
| 55 | |
|
| 56 | 302 | this.argumentRequired = argumentRequired; |
| 57 | 302 | } |
| 58 | |
|
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 62 | |
|
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | |
|
| 68 | |
|
| 69 | |
|
| 70 | |
|
| 71 | |
|
| 72 | |
|
| 73 | |
|
| 74 | |
|
| 75 | |
|
| 76 | |
|
| 77 | |
|
| 78 | |
|
| 79 | |
|
| 80 | |
|
| 81 | |
|
| 82 | |
|
| 83 | |
|
| 84 | |
|
| 85 | |
public ArgumentAcceptingOptionSpec ofType( Class argumentType ) { |
| 86 | 132 | converter = Reflection.findConverter( argumentType ); |
| 87 | 124 | return this; |
| 88 | |
} |
| 89 | |
|
| 90 | |
|
| 91 | |
|
| 92 | |
|
| 93 | |
|
| 94 | |
|
| 95 | |
|
| 96 | |
|
| 97 | |
|
| 98 | |
|
| 99 | |
public final ArgumentAcceptingOptionSpec describedAs( String description ) { |
| 100 | 34 | argumentDescription = description; |
| 101 | |
|
| 102 | 34 | return this; |
| 103 | |
} |
| 104 | |
|
| 105 | |
|
| 106 | |
|
| 107 | |
|
| 108 | |
|
| 109 | |
|
| 110 | |
|
| 111 | |
|
| 112 | |
|
| 113 | |
|
| 114 | |
|
| 115 | |
|
| 116 | |
|
| 117 | |
|
| 118 | |
|
| 119 | |
|
| 120 | |
|
| 121 | |
|
| 122 | |
|
| 123 | |
|
| 124 | |
|
| 125 | |
|
| 126 | |
|
| 127 | |
|
| 128 | |
public final ArgumentAcceptingOptionSpec withValuesSeparatedBy( char separator ) { |
| 129 | 24 | if ( separator == NIL_VALUE_SEPARATOR ) |
| 130 | 4 | throw new IllegalArgumentException( "cannot use U+0000 as separator" ); |
| 131 | |
|
| 132 | 20 | this.valueSeparator = String.valueOf( separator ); |
| 133 | 20 | return this; |
| 134 | |
} |
| 135 | |
|
| 136 | |
final void handleOption( OptionParser parser, ArgumentList arguments, |
| 137 | |
OptionSet detectedOptions, String detectedArgument ) { |
| 138 | |
|
| 139 | 274 | if ( Strings.isNullOrEmpty( detectedArgument ) ) |
| 140 | 202 | detectOptionArgument( parser, arguments, detectedOptions ); |
| 141 | |
else { |
| 142 | 72 | addArguments( detectedOptions, detectedArgument ); |
| 143 | |
} |
| 144 | 264 | } |
| 145 | |
|
| 146 | |
protected void addArguments( OptionSet detectedOptions, String detectedArgument ) { |
| 147 | 220 | StringTokenizer lexer = new StringTokenizer( detectedArgument, valueSeparator ); |
| 148 | 476 | while ( lexer.hasMoreTokens() ) |
| 149 | 260 | detectedOptions.addAllWithArgument( options(), convert( lexer.nextToken() ) ); |
| 150 | 216 | } |
| 151 | |
|
| 152 | |
protected abstract void detectOptionArgument( OptionParser parser, |
| 153 | |
ArgumentList arguments, OptionSet detectedOptions ); |
| 154 | |
|
| 155 | |
protected final Object convert( String argument ) { |
| 156 | 274 | if ( converter == null ) |
| 157 | 154 | return argument; |
| 158 | |
|
| 159 | 120 | Object[] args = new Object[] { argument }; |
| 160 | |
|
| 161 | |
try { |
| 162 | 120 | if ( converter instanceof Constructor ) |
| 163 | 40 | return Reflection.instantiate( (Constructor) converter, args ); |
| 164 | |
|
| 165 | 80 | return Reflection.invoke( (Method) converter, args ); |
| 166 | |
} |
| 167 | 8 | catch ( ReflectionException ignored ) { |
| 168 | 8 | throw new OptionArgumentConversionException( |
| 169 | |
options(), argument, converter.getDeclaringClass() ); |
| 170 | |
} |
| 171 | |
} |
| 172 | |
|
| 173 | |
protected boolean canConvertArgument( String argument ) { |
| 174 | 12 | StringTokenizer lexer = new StringTokenizer( argument, valueSeparator ); |
| 175 | |
|
| 176 | |
try { |
| 177 | 20 | while ( lexer.hasMoreTokens() ) |
| 178 | 12 | convert( lexer.nextToken() ); |
| 179 | 8 | return true; |
| 180 | |
} |
| 181 | 4 | catch ( OptionException ignored ) { |
| 182 | 4 | return false; |
| 183 | |
} |
| 184 | |
} |
| 185 | |
|
| 186 | |
protected boolean isArgumentOfNumberType() { |
| 187 | 24 | return converter != null |
| 188 | 4 | && Number.class.isAssignableFrom( converter.getDeclaringClass() ); |
| 189 | |
} |
| 190 | |
|
| 191 | |
boolean acceptsArguments() { |
| 192 | 112 | return true; |
| 193 | |
} |
| 194 | |
|
| 195 | |
boolean requiresArgument() { |
| 196 | 1746 | return argumentRequired; |
| 197 | |
} |
| 198 | |
|
| 199 | |
String argumentDescription() { |
| 200 | 40 | return argumentDescription; |
| 201 | |
} |
| 202 | |
|
| 203 | |
Class argumentType() { |
| 204 | 38 | return converter == null ? String.class : converter.getDeclaringClass(); |
| 205 | |
} |
| 206 | |
|
| 207 | |
|
| 208 | |
|
| 209 | |
|
| 210 | |
public boolean equals( Object that ) { |
| 211 | 1478 | if ( !super.equals( that ) ) |
| 212 | 614 | return false; |
| 213 | |
|
| 214 | 864 | ArgumentAcceptingOptionSpec other = (ArgumentAcceptingOptionSpec) that; |
| 215 | 864 | return requiresArgument() == other.requiresArgument(); |
| 216 | |
} |
| 217 | |
|
| 218 | |
|
| 219 | |
|
| 220 | |
|
| 221 | |
public int hashCode() { |
| 222 | 360 | return super.hashCode() ^ ( argumentRequired ? 0 : 1 ); |
| 223 | |
} |
| 224 | |
} |