private static ParameterException create(CommandLine cmd, Exception ex, String arg, int i, String[] args) { String msg = ex.getClass().getSimpleName() + ": " + ex.getLocalizedMessage() + " while processing argument at or before arg[" + i + "] '" + arg + "' in " + Arrays.toString(args) + ": " + ex.toString(); return new ParameterException(cmd, msg, ex, null, arg); } }
private Object tryConvert(ArgSpec argSpec, int index, ITypeConverter<?> converter, String value, Class<?> type) throws ParameterException { try { return converter.convert(value); } catch (TypeConversionException ex) { String msg = String.format("Invalid value for %s: %s", optionDescription("", argSpec, index), ex.getMessage()); throw new ParameterException(CommandLine.this, msg, argSpec, value); } catch (Exception other) { String desc = optionDescription("", argSpec, index); String msg = String.format("Invalid value for %s: cannot convert '%s' to %s (%s)", desc, value, type.getSimpleName(), other); throw new ParameterException(CommandLine.this, msg, other, argSpec, value); } }
private int applyValuesToMapField(ArgSpec argSpec, LookBehind lookBehind, Range arity, Stack<String> args, Set<ArgSpec> initialized, String argDescription) throws Exception { Class<?>[] classes = argSpec.auxiliaryTypes(); if (classes.length < 2) { throw new ParameterException(CommandLine.this, argSpec.toString() + " needs two types (one for the map key, one for the value) but only has " + classes.length + " types configured.",argSpec, null); } ITypeConverter<?> keyConverter = getTypeConverter(classes[0], argSpec, 0); ITypeConverter<?> valueConverter = getTypeConverter(classes[1], argSpec, 1); @SuppressWarnings("unchecked") Map<Object, Object> map = (Map<Object, Object>) argSpec.getValue(); if (map == null || (!map.isEmpty() && !initialized.contains(argSpec))) { tracer.debug("Initializing binding for %s with empty %s%n", optionDescription("", argSpec, 0), argSpec.type().getSimpleName()); map = createMap(argSpec.type()); // map class argSpec.setValue(map); } initialized.add(argSpec); int originalSize = map.size(); consumeMapArguments(argSpec, lookBehind, arity, args, classes, keyConverter, valueConverter, map, argDescription); parseResult.add(argSpec, position); argSpec.setValue(map); return map.size() - originalSize; }
private ParameterException createParameterException(Object value, Throwable t) { CommandLine cmd = spec.commandLine() == null ? new CommandLine(spec) : spec.commandLine(); return new ParameterException(cmd, "Could not invoke " + method + " with " + value, t); } }
@Option(names = "--raptor") void setRaptorFences(final String value) { throw new ParameterException(spec.commandLine(), "RAPTOR error"); } }
private String genesisConfig() { try { return Resources.toString(genesisFile().toURI().toURL(), UTF_8); } catch (final IOException e) { throw new ParameterException( new CommandLine(this), String.format("Unable to load genesis file %s.", genesisFile()), e); } }
@Option(names = "--trex") void setTRexFences(final String value) { throw new ParameterException(spec.commandLine(), "TREX error"); } }
private static ParameterException create(CommandLine cmd, Exception ex, String arg, int i, String[] args) { String msg = ex.getClass().getSimpleName() + ": " + ex.getLocalizedMessage() + " while processing argument at or before arg[" + i + "] '" + arg + "' in " + Arrays.toString(args) + ": " + ex.toString(); return new ParameterException(cmd, msg, ex, null, arg); } }
public void run() { throw new ParameterException(new CommandLine(this), "exit code demo"); }
private String[] splitKeyValue(ArgSpec argSpec, String value) { String[] keyValue = ArgSpec.splitRespectingQuotedStrings(value, 2, config(), argSpec, "="); if (keyValue.length < 2) { String splitRegex = argSpec.splitRegex(); if (splitRegex.length() == 0) { throw new ParameterException(CommandLine.this, "Value for option " + optionDescription("", argSpec, 0) + " should be in KEY=VALUE format but was " + value, argSpec, value); } else { throw new ParameterException(CommandLine.this, "Value for option " + optionDescription("", argSpec, 0) + " should be in KEY=VALUE[" + splitRegex + "KEY=VALUE]... format but was " + value, argSpec, value); } } return keyValue; }
@Test public void testParameterExceptionDisallowsArgSpecAndValueBothNull() { CommandLine cmd = new CommandLine(CommandSpec.create()); try { new ParameterException(cmd, "", null, null); } catch (IllegalArgumentException ex) { assertEquals("ArgSpec and value cannot both be null", ex.getMessage()); } try { new ParameterException(cmd, "", new Throwable(), null, null); } catch (IllegalArgumentException ex) { assertEquals("ArgSpec and value cannot both be null", ex.getMessage()); } }
public <T> T set(T value) { throw new ParameterException(mixinSpec.commandLine(), "TREX error"); } };
/** Invokes {@link #usage(PrintStream, Help.Ansi) usage} for the specified command, or for the parent command. */ public void run() { CommandLine parent = self == null ? null : self.getParent(); if (parent == null) { return; } if (commands.length > 0) { CommandLine subcommand = parent.getSubcommands().get(commands[0]); if (subcommand != null) { subcommand.usage(out, ansi); } else { throw new ParameterException(parent, "Unknown subcommand '" + commands[0] + "'.", null, commands[0]); } } else { parent.usage(out, ansi); } } /** {@inheritDoc} */
public <T> T set(T value) { throw new ParameterException(commandSpec.commandLine(), "RAPTOR error"); } };
private ParameterException createParameterException(Object value, Throwable t) { CommandLine cmd = spec.commandLine() == null ? new CommandLine(spec) : spec.commandLine(); return new ParameterException(cmd, "Could not invoke " + method + " with " + value, t); } public String toString() {
@Option(names = "--raptor") void setRaptorFences(final String value) { throw new ParameterException(spec.commandLine(), "RAPTOR error"); } }
@Parameters(index = "1..*") public void setOtherFiles(List<File> otherFiles) { for (File f : otherFiles) { if (!f.exists()) { throw new ParameterException(spec.commandLine(), "File " + f.getAbsolutePath() + " must exist"); } } this.otherFiles = otherFiles; }
@Option(names = "--trex") void setTRexFences(final String value) { throw new ParameterException(spec.commandLine(), "TREX error"); } }
@Command public void explicit(@Option(names = "-v") boolean v) { CommandLine commandLine = spec.subcommands().get("explicit"); throw new CommandLine.ParameterException(commandLine, "Validation failed"); } }
@Option(names = "--minimum") public void setMinimum(int min) { if (min < 0) { throw new ParameterException(spec.commandLine(), "Minimum must be a positive integer"); } minimum = min; }