private void printArg(ArgSpec arg, PrintWriter pw, String indent) { pw.printf("%sdescription: %s%n", indent, Arrays.toString(arg.description())); pw.printf("%sdescriptionKey: '%s'%n", indent, arg.descriptionKey()); pw.printf("%stypeInfo: %s%n", indent, arg.typeInfo()); pw.printf("%sarity: %s%n", indent, arg.arity()); pw.printf("%ssplitRegex: '%s'%n", indent, arg.splitRegex()); pw.printf("%sinteractive: %s%n", indent, arg.interactive()); pw.printf("%srequired: %s%n", indent, arg.required()); pw.printf("%shidden: %s%n", indent, arg.hidden()); pw.printf("%shideParamSyntax: %s%n", indent, arg.hideParamSyntax()); pw.printf("%sdefaultValue: '%s'%n", indent, arg.defaultValue()); pw.printf("%sshowDefaultValue: %s%n", indent, arg.showDefaultValue()); pw.printf("%shasInitialValue: %s%n", indent, arg.hasInitialValue()); pw.printf("%sinitialValue: '%s'%n", indent, arg.initialValue()); pw.printf("%sparamLabel: '%s'%n", indent, arg.paramLabel()); pw.printf("%sconverters: %s%n", indent, Arrays.toString(arg.converters())); pw.printf("%scompletionCandidates: %s%n", indent, iter(arg.completionCandidates())); pw.printf("%sgetter: %s%n", indent, arg.getter()); pw.printf("%ssetter: %s%n", indent, arg.setter()); }
private ITypeConverter<?> getTypeConverter(final Class<?> type, ArgSpec argSpec, int index) { if (argSpec.converters().length > index) { return argSpec.converters()[index]; } if (converterRegistry.containsKey(type)) { return converterRegistry.get(type); } if (type.isEnum()) { return new ITypeConverter<Object>() { @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)); } } }; } throw new MissingTypeConverterException(CommandLine.this, "No TypeConverter registered for " + type.getName() + " of " + argSpec); }
private ITypeConverter<?> getTypeConverter(final Class<?> type, ArgSpec argSpec, int index) { if (argSpec.converters().length > index) { return argSpec.converters()[index]; } if (converterRegistry.containsKey(type)) { return converterRegistry.get(type); } if (type.isEnum()) { return new ITypeConverter<Object>() { @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)); } } }; } throw new MissingTypeConverterException(CommandLine.this, "No TypeConverter registered for " + type.getName() + " of " + argSpec); }
private void visitArgSpec(ArgSpec argSpec) throws NoSuchFieldException, IllegalAccessException { visitGetter(argSpec.getter()); visitSetter(argSpec.setter()); visitType(argSpec.type()); visitTypes(argSpec.auxiliaryTypes()); visitObjectType(argSpec.completionCandidates()); visitObjectTypes(argSpec.converters()); }