public DefaultCommandLine addRawValue(Option option, String value) { if (!acceptMoreValues(option) && !option.isFlag()) { throw new CLIException("The option " + option.getName() + " does not accept value or has " + "already been set"); } if (! option.getChoices().isEmpty() && ! option.getChoices().contains(value)) { throw new InvalidValueException(option, value); } List<String> list = optionValues.get(option); if (list == null) { list = new ArrayList<>(); optionValues.put(option, list); } list.add(value); return this; }
@Test public void testThatInvalidValuesAsDefaultValueAreReported() throws CLIException { try { cli.addArgument(new TypedArgument<Integer>() .setIndex(0).setArgName("1").setType(Integer.class).setDefaultValue("a")); } catch (IllegalArgumentException e) { assertThat(e.getCause()).isInstanceOf(InvalidValueException.class); InvalidValueException cause = (InvalidValueException) e.getCause(); assertThat(cause.getArgument().getIndex()).isEqualTo(0); assertThat(cause.getArgument().getArgName()).isEqualTo("1"); assertThat(cause.getValue()).isEqualTo("a"); } }
@Test public void testLongWithUnexpectedArgument2() throws Exception { String[] args = new String[]{"-foobar"}; TypedOption<Boolean> f = new TypedOption<Boolean>().setLongName("foo").setShortName("f").setType(Boolean.class) .setSingleValued(false) // No value accepted here. ; cli.addOption(f); try { CommandLine evaluated = cli.parse(Arrays.asList(args)); fail("Exception expected"); } catch (InvalidValueException e) { assertThat(e.getOption().getShortName()).isEqualToIgnoringCase("f"); assertThat(e.getValue()).isEqualToIgnoringCase("bar"); } }
/** * Handles the following tokens: * <p/> * --L=V * -L=V * --l=V * -l=V * * @param token the command line token to handle */ private void handleLongOptionWithEqual(String token) throws CLIException { int pos = token.indexOf('='); String value = token.substring(pos + 1); String opt = token.substring(0, pos); List<Option> matchingOpts = getMatchingOptions(opt); if (matchingOpts.isEmpty()) { handleArgument(token); } else if (matchingOpts.size() > 1) { throw new AmbiguousOptionException(opt, matchingOpts); } else { Option option = matchingOpts.get(0); if (commandLine.acceptMoreValues(option)) { handleOption(option); commandLine.addRawValue(option, value); current = null; } else { throw new InvalidValueException(option, value); } } }
@Test public void testShortWithUnexpectedArgument() throws Exception { String[] args = new String[]{"-f=bar"}; TypedOption<Boolean> f = new TypedOption<Boolean>().setLongName("foo").setShortName("f").setType(Boolean.class) .setSingleValued(false) // No value accepted here. ; cli.addOption(f); try { CommandLine evaluated = cli.parse(Arrays.asList(args)); fail("Exception expected"); } catch (InvalidValueException e) { assertThat(e.getOption().getShortName()).isEqualToIgnoringCase("f"); assertThat(e.getValue()).isEqualToIgnoringCase("bar"); } }
@Test public void testThatInvalidValuesAreReported() throws CLIException { cli.addArgument(new TypedArgument<Integer>() .setIndex(0).setArgName("1").setType(Integer.class)); try { evaluated = cli.parse(Collections.singletonList("a")); evaluated.getArgumentValue(0); fail("Exception expected"); } catch (CLIException e) { assertThat(e).isInstanceOf(InvalidValueException.class); InvalidValueException cause = (InvalidValueException) e; assertThat(cause.getArgument().getIndex()).isEqualTo(0); assertThat(cause.getArgument().getArgName()).isEqualTo("1"); assertThat(cause.getValue()).isEqualTo("a"); } }
/** * Creates the value for the given argument from the given raw value. * * @param value the value * @return the created value */ public static <T> T create(String value, TypedArgument<T> argument) { Objects.requireNonNull(argument); if (value == null) { value = argument.getDefaultValue(); } if (value == null) { return null; } try { if (argument.getConverter() != null) { return Converters.create(value, argument.getConverter()); } else { return Converters.create(argument.getType(), value); } } catch (Exception e) { throw new InvalidValueException(argument, value, e); } }
@Test public void testLongWithUnexpectedArgument1() throws Exception { String[] args = new String[]{"--foo=bar"}; TypedOption<Boolean> f = new TypedOption<Boolean>().setLongName("foo").setShortName("f") .setType(Boolean.class) .setSingleValued(false) // No value accepted here. .setFlag(true); cli.addOption(f); try { CommandLine evaluated = cli.parse(Arrays.asList(args)); fail("Exception expected"); } catch (InvalidValueException e) { assertThat(e.getOption().getShortName()).isEqualToIgnoringCase("f"); assertThat(e.getValue()).isEqualToIgnoringCase("bar"); } }
@Test public void testThatInvalidValuesAsDefaultValueAreReported() throws CLIException { try { cli.addArgument(new TypedArgument<Integer>() .setIndex(0).setArgName("1").setType(Integer.class).setDefaultValue("a")); } catch (IllegalArgumentException e) { assertThat(e.getCause()).isInstanceOf(InvalidValueException.class); InvalidValueException cause = (InvalidValueException) e.getCause(); assertThat(cause.getArgument().getIndex()).isEqualTo(0); assertThat(cause.getArgument().getArgName()).isEqualTo("1"); assertThat(cause.getValue()).isEqualTo("a"); } }
/** * 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); } }
@Test public void testLongWithUnexpectedArgument2() throws Exception { String[] args = new String[]{"-foobar"}; TypedOption<Boolean> f = new TypedOption<Boolean>().setLongName("foo").setShortName("f").setType(Boolean.class) .setSingleValued(false) // No value accepted here. ; cli.addOption(f); try { CommandLine evaluated = cli.parse(Arrays.asList(args)); fail("Exception expected"); } catch (InvalidValueException e) { assertThat(e.getOption().getShortName()).isEqualToIgnoringCase("f"); assertThat(e.getValue()).isEqualToIgnoringCase("bar"); } }
@Test public void testThatInvalidValuesAreReported() throws CLIException { cli.addArgument(new TypedArgument<Integer>() .setIndex(0).setArgName("1").setType(Integer.class)); try { evaluated = cli.parse(Collections.singletonList("a")); evaluated.getArgumentValue(0); fail("Exception expected"); } catch (CLIException e) { assertThat(e).isInstanceOf(InvalidValueException.class); InvalidValueException cause = (InvalidValueException) e; assertThat(cause.getArgument().getIndex()).isEqualTo(0); assertThat(cause.getArgument().getArgName()).isEqualTo("1"); assertThat(cause.getValue()).isEqualTo("a"); } }
current = null; } else { throw new InvalidValueException(getOption(opt), t.substring(opt.length())); current = null; } else { throw new InvalidValueException(option, value);
@Test public void testShortWithUnexpectedArgument() throws Exception { String[] args = new String[]{"-f=bar"}; TypedOption<Boolean> f = new TypedOption<Boolean>().setLongName("foo").setShortName("f").setType(Boolean.class) .setSingleValued(false) // No value accepted here. ; cli.addOption(f); try { CommandLine evaluated = cli.parse(Arrays.asList(args)); fail("Exception expected"); } catch (InvalidValueException e) { assertThat(e.getOption().getShortName()).isEqualToIgnoringCase("f"); assertThat(e.getValue()).isEqualToIgnoringCase("bar"); } }
public DefaultCommandLine addRawValue(Option option, String value) { if (!acceptMoreValues(option) && !option.isFlag()) { throw new CLIException("The option " + option.getName() + " does not accept value or has " + "already been set"); } if (! option.getChoices().isEmpty() && ! option.getChoices().contains(value)) { throw new InvalidValueException(option, value); } List<String> list = optionValues.get(option); if (list == null) { list = new ArrayList<>(); optionValues.put(option, list); } list.add(value); return this; }
@Test public void testLongWithUnexpectedArgument1() throws Exception { String[] args = new String[]{"--foo=bar"}; TypedOption<Boolean> f = new TypedOption<Boolean>().setLongName("foo").setShortName("f") .setType(Boolean.class) .setSingleValued(false) // No value accepted here. .setFlag(true); cli.addOption(f); try { CommandLine evaluated = cli.parse(Arrays.asList(args)); fail("Exception expected"); } catch (InvalidValueException e) { assertThat(e.getOption().getShortName()).isEqualToIgnoringCase("f"); assertThat(e.getValue()).isEqualToIgnoringCase("bar"); } }
/** * Handles the following tokens: * <p/> * --L=V * -L=V * --l=V * -l=V * * @param token the command line token to handle */ private void handleLongOptionWithEqual(String token) throws CLIException { int pos = token.indexOf('='); String value = token.substring(pos + 1); String opt = token.substring(0, pos); List<Option> matchingOpts = getMatchingOptions(opt); if (matchingOpts.isEmpty()) { handleArgument(token); } else if (matchingOpts.size() > 1) { throw new AmbiguousOptionException(opt, matchingOpts); } else { Option option = matchingOpts.get(0); if (commandLine.acceptMoreValues(option)) { handleOption(option); commandLine.addRawValue(option, value); current = null; } else { throw new InvalidValueException(option, value); } } }
/** * Creates the value for the given argument from the given raw value. * * @param value the value * @return the created value */ public static <T> T create(String value, TypedArgument<T> argument) { Objects.requireNonNull(argument); if (value == null) { value = argument.getDefaultValue(); } if (value == null) { return null; } try { if (argument.getConverter() != null) { return Converters.create(value, argument.getConverter()); } else { return Converters.create(argument.getType(), value); } } catch (Exception e) { throw new InvalidValueException(argument, value, e); } }
/** * 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); } }
private void calculate_btnActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: InvalidValueException ive = new InvalidValueException(); calculate_btn.addActionListener(ive); //addActionListener }