public static <T> List<T> createFromList(String raw, TypedOption<T> option) { if (raw == null) { return Collections.emptyList(); } final String[] segments = raw.split(option.getListSeparator()); return Arrays.stream(segments).map(s -> create(s.trim(), option)).collect(Collectors.toList()); }
@Test public void testHelloCLIFromClass() { CLI command = CLIConfigurator.define(HelloClI.class); assertThat(command.getOptions()).hasSize(1); TypedOption option = (TypedOption) find(command.getOptions(), "name"); assertThat(option.getLongName()).isEqualToIgnoringCase("name"); assertThat(option.getShortName()).isEqualToIgnoringCase("n"); assertThat(option.getType()).isEqualTo(String.class); assertThat(option.getArgName()).isEqualTo("name"); assertThat(option.getDescription()).isEqualToIgnoringCase("your name"); assertThat(option.getDefaultValue()).isNull(); assertThat(option.acceptValue()).isTrue(); assertThat(option.isMultiValued()).isFalse(); assertThat(option.isRequired()).isTrue(); }
/** * Creates the value for the given option from the given raw value. * * @param value the value * @return the created value */ public static <T> T create(String value, TypedOption<T> option) { Objects.requireNonNull(option); if (value == null) { value = option.getDefaultValue(); } if (value == null) { return null; } try { if (option.getConverter() != null) { return Converters.create(value, option.getConverter()); } else { return Converters.create(option.getType(), value); } } catch (Exception e) { throw new InvalidValueException(option, value, e); } }
@Override @SuppressWarnings("unchecked") public <T> List<T> getOptionValues(String name) { Option option = cli.getOption(name); if (option == null) { return null; } if (option instanceof TypedOption) { TypedOption<T> typed = (TypedOption<T>) option; if (typed.isParsedAsList()) { return createFromList(getRawValueForOption(option), typed); } else { return getRawValuesForOption(option) .stream() .map(s -> create(s, typed)) .collect(Collectors.toList()); } } else { return (List<T>) getRawValuesForOption(option); } }
private <T> T getValue(TypedOption<T> option) { if (isOptionAssigned(option)) { return create(getRawValueForOption(option), option); } else { if (option.getDefaultValue() != null) { return create(getRawValueForOption(option), option); } if (option.isFlag() || isBoolean(option)) { try { if (isSeenInCommandLine(option)) { return (T) Boolean.TRUE; } else { return (T) Boolean.FALSE; } } catch (InvalidValueException e) { throw new IllegalArgumentException(e); } } } return null; }
private boolean isBoolean(TypedOption option) { Class type = option.getType(); return type == Boolean.TYPE || type == Boolean.class; }
@Test public void testTypeExtraction() { CLI command = CLIConfigurator.define(CommandForTypeExtractTest.class); assertThat(command.getOptions()).hasSize(6); TypedOption model = (TypedOption) find(command.getOptions(), "list"); assertThat(model.getType()).isEqualTo(String.class); assertThat(model.isMultiValued()).isTrue(); model = (TypedOption) find(command.getOptions(), "set"); assertThat(model.getType()).isEqualTo(Character.class); assertThat(model.isMultiValued()).isTrue(); model = (TypedOption) find(command.getOptions(), "collection"); assertThat(model.getType()).isEqualTo(Integer.class); assertThat(model.isMultiValued()).isTrue(); model = (TypedOption) find(command.getOptions(), "tree"); assertThat(model.getType()).isEqualTo(String.class); assertThat(model.isMultiValued()).isTrue(); model = (TypedOption) find(command.getOptions(), "al"); assertThat(model.getType()).isEqualTo(String.class); assertThat(model.isMultiValued()).isTrue(); model = (TypedOption) find(command.getOptions(), "array"); assertThat(model.getType()).isEqualTo(Integer.TYPE); assertThat(model.isMultiValued()).isTrue(); }