@SuppressWarnings("unchecked") public Object convert(String value) throws Exception { String sensitivity = "case-sensitive"; if (commandSpec.parser().caseInsensitiveEnumValuesAllowed()) { String upper = value.toUpperCase(); for (Object enumConstant : type.getEnumConstants()) { if (upper.equals(String.valueOf(enumConstant).toUpperCase())) { return enumConstant; } } sensitivity = "case-insensitive"; } try { return Enum.valueOf((Class<Enum>) type, value); } catch (Exception ex) { Enum<?>[] constants = ((Class<Enum<?>>) type).getEnumConstants(); String[] names = new String[constants.length]; for (int i = 0; i < names.length; i++) { names[i] = constants[i].name(); } throw new TypeConversionException( String.format("expected one of %s (%s) but was '%s'", Arrays.asList(names), sensitivity, value)); } } };
private void printParser(ParserSpec parser, PrintWriter pw, String indent) { pw.printf("%sParserSpec:%n", indent); indent += " "; pw.printf("%sseparator: '%s'%n", indent, parser.separator()); pw.printf("%sendOfOptionsDelimiter: '%s'%n", indent, parser.endOfOptionsDelimiter()); pw.printf("%sexpandAtFiles: %s%n", indent, parser.expandAtFiles()); pw.printf("%satFileCommentChar: '%s'%n", indent, parser.atFileCommentChar()); pw.printf("%soverwrittenOptionsAllowed: %s%n", indent, parser.overwrittenOptionsAllowed()); pw.printf("%sunmatchedArgumentsAllowed: %s%n", indent, parser.unmatchedArgumentsAllowed()); pw.printf("%sunmatchedOptionsArePositionalParams: %s%n", indent, parser.unmatchedOptionsArePositionalParams()); pw.printf("%sstopAtUnmatched: %s%n", indent, parser.stopAtUnmatched()); pw.printf("%sstopAtPositional: %s%n", indent, parser.stopAtPositional()); pw.printf("%sposixClusteredShortOptionsAllowed: %s%n", indent, parser.posixClusteredShortOptionsAllowed()); pw.printf("%saritySatisfiedByAttachedOptionParam: %s%n", indent, parser.aritySatisfiedByAttachedOptionParam()); pw.printf("%scaseInsensitiveEnumValuesAllowed: %s%n", indent, parser.caseInsensitiveEnumValuesAllowed()); pw.printf("%scollectErrors: %s%n", indent, parser.collectErrors()); pw.printf("%slimitSplit: %s%n", indent, parser.limitSplit()); pw.printf("%stoggleBooleanFlags: %s%n", indent, parser.toggleBooleanFlags()); }
/** Sets whether the parser should ignore case when converting arguments to {@code enum} values. The default is {@code false}. * When set to true, for example, for an option of type <a href="https://docs.oracle.com/javase/8/docs/api/java/time/DayOfWeek.html">java.time.DayOfWeek</a>, * values {@code MonDaY}, {@code monday} and {@code MONDAY} are all recognized if {@code true}. * <p>The specified setting will be registered with this {@code CommandLine} and the full hierarchy of its * subcommands and nested sub-subcommands <em>at the moment this method is called</em>. Subcommands added * later will have the default setting. To ensure a setting is applied to all * subcommands, call the setter last, after adding subcommands.</p> * @param newValue the new setting * @return this {@code CommandLine} object, to allow method chaining * @since 3.4 */ public CommandLine setCaseInsensitiveEnumValuesAllowed(boolean newValue) { getCommandSpec().parser().caseInsensitiveEnumValuesAllowed(newValue); for (CommandLine command : getCommandSpec().subcommands().values()) { command.setCaseInsensitiveEnumValuesAllowed(newValue); } return this; }
/** Sets whether the parser should ignore case when converting arguments to {@code enum} values. The default is {@code false}. * When set to true, for example, for an option of type <a href="https://docs.oracle.com/javase/8/docs/api/java/time/DayOfWeek.html">java.time.DayOfWeek</a>, * values {@code MonDaY}, {@code monday} and {@code MONDAY} are all recognized if {@code true}. * <p>The specified setting will be registered with this {@code CommandLine} and the full hierarchy of its * subcommands and nested sub-subcommands <em>at the moment this method is called</em>. Subcommands added * later will have the default setting. To ensure a setting is applied to all * subcommands, call the setter last, after adding subcommands.</p> * @param newValue the new setting * @return this {@code CommandLine} object, to allow method chaining * @since 3.4 */ public CommandLine setCaseInsensitiveEnumValuesAllowed(boolean newValue) { getCommandSpec().parser().caseInsensitiveEnumValuesAllowed(newValue); for (CommandLine command : getCommandSpec().subcommands().values()) { command.setCaseInsensitiveEnumValuesAllowed(newValue); } return this; }
@SuppressWarnings("unchecked") public Object convert(String value) throws Exception { if (commandSpec.parser().caseInsensitiveEnumValuesAllowed()) { String upper = value.toUpperCase(); for (Object enumConstant : type.getEnumConstants()) { if (upper.equals(String.valueOf(enumConstant).toUpperCase())) { return enumConstant; } } } try { return Enum.valueOf((Class<Enum>) type, value); } catch (Exception ex) { throw new TypeConversionException( String.format("expected one of %s but was '%s'", Arrays.asList(type.getEnumConstants()), value)); } } };
@Test public void testCommandSpecParserSetter() { CommandSpec spec = CommandSpec.wrapWithoutInspection(null); ParserSpec old = spec.parser(); assertSame(old, spec.parser()); assertFalse(spec.parser().collectErrors()); assertFalse(spec.parser().caseInsensitiveEnumValuesAllowed()); ParserSpec update = new ParserSpec().collectErrors(true).caseInsensitiveEnumValuesAllowed(true); spec.parser(update); assertSame(old, spec.parser()); assertTrue(spec.parser().collectErrors()); assertTrue(spec.parser().caseInsensitiveEnumValuesAllowed()); }
/** Returns whether the parser should ignore case when converting arguments to {@code enum} values. The default is {@code false}. * @return {@code true} if enum values can be specified that don't match the {@code toString()} value of the enum constant, {@code false} otherwise; * e.g., for an option of type <a href="https://docs.oracle.com/javase/8/docs/api/java/time/DayOfWeek.html">java.time.DayOfWeek</a>, * values {@code MonDaY}, {@code monday} and {@code MONDAY} are all recognized if {@code true}. * @since 3.4 */ public boolean isCaseInsensitiveEnumValuesAllowed() { return getCommandSpec().parser().caseInsensitiveEnumValuesAllowed(); }
@Test public void testCommandSpecParserSetter() { CommandSpec spec = CommandSpec.wrapWithoutInspection(null); ParserSpec old = spec.parser(); assertSame(old, spec.parser()); assertFalse(spec.parser().collectErrors()); assertFalse(spec.parser().caseInsensitiveEnumValuesAllowed()); ParserSpec update = new ParserSpec().collectErrors(true).caseInsensitiveEnumValuesAllowed(true); spec.parser(update); assertSame(old, spec.parser()); assertTrue(spec.parser().collectErrors()); assertTrue(spec.parser().caseInsensitiveEnumValuesAllowed()); }
/** Returns whether the parser should ignore case when converting arguments to {@code enum} values. The default is {@code false}. * @return {@code true} if enum values can be specified that don't match the {@code toString()} value of the enum constant, {@code false} otherwise; * e.g., for an option of type <a href="https://docs.oracle.com/javase/8/docs/api/java/time/DayOfWeek.html">java.time.DayOfWeek</a>, * values {@code MonDaY}, {@code monday} and {@code MONDAY} are all recognized if {@code true}. * @since 3.4 */ public boolean isCaseInsensitiveEnumValuesAllowed() { return getCommandSpec().parser().caseInsensitiveEnumValuesAllowed(); }