public <T> T set(T value) throws PicocliException { try { @SuppressWarnings("unchecked") T result = (T) field.get(scope); field.set(scope, value); return result; } catch (Exception ex) { throw new PicocliException("Could not set value for field " + field + " to " + value, ex); } } public String toString() {
public <T> T get() throws PicocliException { try { @SuppressWarnings("unchecked") T result = (T) field.get(scope); return result; } catch (Exception ex) { throw new PicocliException("Could not get value for field " + field, ex); } } public <T> T set(T value) throws PicocliException {
void addAll(String[] unmatched) { if (setter != null) { try { setter.set(unmatched); } catch (Exception ex) { throw new PicocliException(String.format("Could not invoke setter (%s) with unmatched argument array '%s': %s", setter, Arrays.toString(unmatched), ex), ex); } } if (getter != null) { try { Collection<String> collection = getter.get(); Assert.notNull(collection, "getter returned null Collection"); collection.addAll(Arrays.asList(unmatched)); } catch (Exception ex) { throw new PicocliException(String.format("Could not add unmatched argument array '%s' to collection returned by getter (%s): %s", Arrays.toString(unmatched), getter, ex), ex); } } } }
void addAll(String[] unmatched) { if (setter != null) { try { setter.set(unmatched); } catch (Exception ex) { throw new PicocliException(String.format("Could not invoke setter (%s) with unmatched argument array '%s': %s", setter, Arrays.toString(unmatched), ex), ex); } } if (getter != null) { try { Collection<String> collection = getter.get(); Assert.notNull(collection, "getter returned null Collection"); collection.addAll(Arrays.asList(unmatched)); } catch (Exception ex) { throw new PicocliException(String.format("Could not add unmatched argument array '%s' to collection returned by getter (%s): %s", Arrays.toString(unmatched), getter, ex), ex); } } } }
@Test public void testArgSpecGetterRethrowsPicocliException() { final CommandLine.PicocliException expected = new CommandLine.PicocliException("boom"); IGetter getter = new IGetter() { public <T> T get() { throw expected; } }; PositionalParamSpec positional = PositionalParamSpec.builder().getter(getter).build(); try { positional.getValue(); } catch (CommandLine.PicocliException ex) { assertSame(expected, ex); } }
@Test public void testArgSpecSetterRethrowsPicocliException() { final CommandLine.PicocliException expected = new CommandLine.PicocliException("boom"); ISetter setter = new ISetter() { public <T> T set(T value) throws Exception { throw expected; } }; PositionalParamSpec positional = PositionalParamSpec.builder().setter(setter).build(); try { positional.setValue("abc"); } catch (CommandLine.PicocliException ex) { assertSame(expected, ex); } }
public <T> T get() throws PicocliException { try { @SuppressWarnings("unchecked") T result = (T) field.get(scope); return result; } catch (Exception ex) { throw new PicocliException("Could not get value for field " + field, ex); } } public <T> T set(T value) throws PicocliException {
/** Returns the current value of this argument. Delegates to the current {@link #getter()}. */ public <T> T getValue() throws PicocliException { try { return getter.get(); } catch (PicocliException ex) { throw ex; } catch (Exception ex) { throw new PicocliException("Could not get value for " + this + ": " + ex, ex); } } /** Sets the value of this argument to the specified value and returns the previous value. Delegates to the current {@link #setter()}. */
public <T> T set(T value) throws PicocliException { try { @SuppressWarnings("unchecked") T result = (T) field.get(scope); field.set(scope, value); return result; } catch (Exception ex) { throw new PicocliException("Could not set value for field " + field + " to " + value, ex); } } }
/** Sets the value of this argument to the specified value and returns the previous value. Delegates to the current {@link #setter()}. */ public <T> T setValue(T newValue) throws PicocliException { try { return setter.set(newValue); } catch (PicocliException ex) { throw ex; } catch (Exception ex) { throw new PicocliException("Could not set value (" + newValue + ") for " + this + ": " + ex, ex); } } /** Sets the value of this argument to the specified value and returns the previous value. Delegates to the current {@link #setter()}.
@Test public void testArgSpecSetterRethrowsPicocliException() { final CommandLine.PicocliException expected = new CommandLine.PicocliException("boom"); ISetter setter = new ISetter() { public <T> T set(T value) throws Exception { throw expected; } }; PositionalParamSpec positional = PositionalParamSpec.builder().setter(setter).build(); try { positional.setValue("abc"); } catch (CommandLine.PicocliException ex) { assertSame(expected, ex); } }
/** Sets the value of this argument to the specified value and returns the previous value. Delegates to the current {@link #setter()}. */ public <T> T setValue(T newValue) throws PicocliException { try { return setter.set(newValue); } catch (PicocliException ex) { throw ex; } catch (Exception ex) { throw new PicocliException("Could not set value (" + newValue + ") for " + this + ": " + ex, ex); } } /** Sets the value of this argument to the specified value and returns the previous value. Delegates to the current {@link #setter()}.
/** Returns the current value of this argument. Delegates to the current {@link #getter()}. */ public <T> T getValue() throws PicocliException { try { return getter.get(); } catch (PicocliException ex) { throw ex; } catch (Exception ex) { throw new PicocliException("Could not get value for " + this + ": " + ex, ex); } } /** Sets the value of this argument to the specified value and returns the previous value. Delegates to the current {@link #setter()}. */
@Test public void testArgSpecGetterRethrowsPicocliException() { final CommandLine.PicocliException expected = new CommandLine.PicocliException("boom"); IGetter getter = new IGetter() { public <T> T get() { throw expected; } }; PositionalParamSpec positional = PositionalParamSpec.builder().getter(getter).build(); try { positional.getValue(); } catch (CommandLine.PicocliException ex) { assertSame(expected, ex); } }