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); } } } }
@SuppressWarnings("unchecked") public <T> T get() throws Exception { List<String> result = (List<String>) member.getter().get(); if (result == null) { result = new ArrayList<String>(); member.setter().set(result); } return (T) result; } });
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); } } } }
@Override protected List<Object> handle(final ParseResult parseResult) throws ExecutionException { final CommandLine commandLine = parseResult.asCommandLineList().get(0); if (parseResult.hasMatchedOption(configFileOptionName)) { final OptionSpec configFileOption = parseResult.matchedOption(configFileOptionName); final File configFile; try { configFile = configFileOption.getter().get(); } catch (final Exception e) { throw new ExecutionException(commandLine, e.getMessage(), e); } final TomlConfigFileDefaultProvider tomlConfigFileDefaultProvider = new TomlConfigFileDefaultProvider(commandLine, configFile); commandLine.setDefaultValueProvider(tomlConfigFileDefaultProvider); } else if (isDocker) { final File configFile = new File(DOCKER_CONFIG_LOCATION); if (configFile.exists()) { final TomlConfigFileDefaultProvider tomlConfigFileDefaultProvider = new TomlConfigFileDefaultProvider(commandLine, configFile); commandLine.setDefaultValueProvider(tomlConfigFileDefaultProvider); } } commandLine.parseWithHandlers( resultHandler, exceptionHandler, parseResult.originalArgs().toArray(new String[0])); return new ArrayList<>(); }
private static CommandSpec buildMixinForField(TypedMember member, IFactory factory) { try { Object userObject = member.getter().get(); if (userObject == null) { userObject = factory.create(member.getType()); member.setter().set(userObject); } CommandSpec result = CommandSpec.forAnnotatedObject(userObject, factory); return result.withToString(abbreviate("mixin from member " + member.toGenericString())); } catch (InitializationException ex) { throw ex; } catch (Exception ex) { throw new InitializationException("Could not access or modify mixin member " + member + ": " + ex, ex); } } private static UnmatchedArgsBinding buildUnmatchedForField(final TypedMember member) {
@Test public void handle() throws Exception { when(mockConfigOptionGetter.get()).thenReturn(temp.newFile()); final List<Object> result = configParsingHandler.handle(mockParseResult); verify(mockCommandLine).setDefaultValueProvider(any(IDefaultValueProvider.class)); verify(mockCommandLine).parseWithHandlers(eq(resultHandler), eq(exceptionHandler), anyString()); assertThat(result, is(empty())); }
private static void initCommon(ArgSpec.Builder<?> builder, TypedMember member) { builder.type(member.getType()); builder.withToString((member.accessible instanceof Field ? "field " : member.accessible instanceof Method ? "method " : member.accessible.getClass().getSimpleName() + " ") + abbreviate(member.toGenericString())); builder.getter(member.getter()).setter(member.setter()); builder.hasInitialValue(member.hasInitialValue); try { builder.initialValue(member.getter().get()); } catch (Exception ex) { builder.initialValue(null); } } static String abbreviate(String text) {
@SuppressWarnings("unchecked") public <T> T get() throws Exception { List<String> result = (List<String>) member.getter().get(); if (result == null) { result = new ArrayList<String>(); member.setter().set(result); } return (T) result; } });
/** 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 handleShouldRaiseExceptionIfNoFileParam() throws Exception { exceptionRule.expect(Exception.class); final String error_message = "an error occurred during get"; exceptionRule.expectMessage(error_message); when(mockConfigOptionGetter.get()).thenThrow(new Exception(error_message)); configParsingHandler.handle(mockParseResult); }
private static CommandSpec buildMixinForField(IAnnotatedElement member, IFactory factory) { try { Object userObject = member.getter().get(); if (userObject == null) { userObject = factory.create(member.getTypeInfo().getType()); member.setter().set(userObject); } CommandSpec result = CommandSpec.forAnnotatedObject(userObject, factory); return result.withToString(member.getToString()); } catch (InitializationException ex) { throw ex; } catch (Exception ex) { throw new InitializationException("Could not access or modify mixin member " + member + ": " + ex, ex); } } private static UnmatchedArgsBinding buildUnmatchedForField(final IAnnotatedElement member) {
/** 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()}. */
Builder(IAnnotatedElement source) { userObject = source.userObject(); setTypeInfo(source.getTypeInfo()); toString = source.getToString(); getter = source.getter(); setter = source.setter(); hasInitialValue = source.hasInitialValue(); try { initialValue = source.getter().get(); } catch (Exception ex) { initialValue = null; } } Builder(Option option, IAnnotatedElement source, IFactory factory) {