public <T> List<T> getArgs(final Class<T> type) { return convert(this.args, type); }
public <T> T getArg(final int index, final Class<T> type, final T defaultValue) { try { return convert(this.args.get(index), type); } catch (final Throwable ex) { return defaultValue; } }
public <T> T getArg(final int index, final Class<T> type) { return convert(this.args.get(index), type); }
public <T> List<T> getOptionValues(final String letterOrName, final Class<T> type) { final List<String> strings = MoreObjects.firstNonNull(this.optionValues.get(letterOrName), ImmutableList.<String>of()); return convert(strings, type); }
@SuppressWarnings("unchecked") private static <T> List<T> convert(final List<String> strings, final Class<T> type) { if (type == String.class) { return (List<T>) strings; } final List<T> list = Lists.newArrayList(); for (final String string : strings) { list.add(convert(string, type)); } return ImmutableList.copyOf(list); }
@Nullable public <T> T getOptionValue(final String letterOrName, final Class<T> type, @Nullable final T defaultValue) { final List<String> strings = this.optionValues.get(letterOrName); if (strings == null || strings.isEmpty() || strings.size() > 1) { return defaultValue; } try { return convert(strings.get(0), type); } catch (final Throwable ex) { return defaultValue; } }
@Nullable public <T> T getOptionValue(final String letterOrName, final Class<T> type) { final List<String> strings = this.optionValues.get(letterOrName); if (strings == null || strings.isEmpty()) { return null; } if (strings.size() > 1) { throw new Exception("Multiple values for option '" + letterOrName + "': " + Joiner.on(", ").join(strings), null); } try { return convert(strings.get(0), type); } catch (final Throwable ex) { throw new Exception("'" + strings.get(0) + "' is not a valid " + type.getSimpleName(), ex); } }