Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/main/java/org/apache/commons/cli/TypeHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,16 @@ public static FileInputStream openFile(final String string) throws ParseExceptio
map.put(Integer.class, Integer::parseInt);
map.put(Short.class, Short::parseShort);
map.put(Byte.class, Byte::parseByte);
map.put(Character.class, s -> s.startsWith("\\u") ? Character.toChars(Integer.parseInt(s.substring(2), HEX_RADIX))[0] : s.charAt(0));
map.put(Character.class, s -> {
if (s.startsWith("\\u")) {
final int codePoint = Integer.parseInt(s.substring(2), HEX_RADIX);
if (!Character.isBmpCodePoint(codePoint)) {
throw new IllegalArgumentException("Code point U+" + Integer.toHexString(codePoint) + " does not fit in a char");
}
return (char) codePoint;
}
return s.charAt(0);
});
map.put(Double.class, Double::parseDouble);
map.put(Float.class, Float::parseFloat);
map.put(BigInteger.class, BigInteger::new);
Expand Down
1 change: 1 addition & 0 deletions src/test/java/org/apache/commons/cli/TypeHandlerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ private static Stream<Arguments> createValueTestParameters() throws MalformedURL
list.add(Arguments.of("5", Character.class, '5'));
list.add(Arguments.of("5.5", Character.class, '5'));
list.add(Arguments.of("\\u0124", Character.class, Character.toChars(0x0124)[0]));
list.add(Arguments.of("\\u1F600", Character.class, ParseException.class));

list.add(Arguments.of("just-a-string", Double.class, ParseException.class));
list.add(Arguments.of("5", Double.class, 5d));
Expand Down