@Test public void testCreation() { AmbiguousOptionException exception = new AmbiguousOptionException("foo", Arrays.asList(new Option().setLongName("foobar"), new Option().setLongName("foobaz"))); assertThat(exception.getOptions()).hasSize(2); assertThat(exception.getToken()).isEqualTo("foo"); assertThat(exception.getMessage()) .contains("Ambiguous argument in command line") .contains("'foo'") .contains("foobar").contains("foobaz"); }
@Test public void testAmbiguousPartialLongOption2() throws Exception { String[] args = new String[]{"-ver"}; TypedOption<Integer> v1 = new TypedOption<Integer>().setLongName("verbose") .setSingleValued(true) .setType(Integer.class); TypedOption<Boolean> v2 = new TypedOption<Boolean>().setLongName("version").setType(Boolean.class) .setSingleValued(false); cli.addOption(v1).addOption(v2); try { cli.parse(Arrays.asList(args)); fail("Exception expected"); } catch (AmbiguousOptionException e) { assertThat(e.getToken()).isEqualTo("-ver"); assertThat(e.getOptions()).hasSize(2); } }
/** * Handles the following tokens: * <p/> * --L * -L * --l * -l * * @param token the command line token to handle */ private void handleLongOptionWithoutEqual(String token) throws CLIException { List<Option> matchingOpts = getMatchingOptions(token); if (matchingOpts.isEmpty()) { handleArgument(token); } else if (matchingOpts.size() > 1) { throw new AmbiguousOptionException(token, matchingOpts); } else { final Option option = matchingOpts.get(0); handleOption(option); } }
@Test public void testAmbiguousPartialLongOption1() throws Exception { String[] args = new String[]{"--ver"}; TypedOption<Integer> v1 = new TypedOption<Integer>().setLongName("verbose") .setSingleValued(true) .setType(Integer.class); TypedOption<Boolean> v2 = new TypedOption<Boolean>().setLongName("version") .setType(Boolean.class) .setSingleValued(false); cli.addOption(v1).addOption(v2); try { cli.parse(Arrays.asList(args)); fail("Exception expected"); } catch (AmbiguousOptionException e) { assertThat(e.getToken()).isEqualTo("--ver"); assertThat(e.getOptions()).hasSize(2); } }
/** * 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 testCreation() { AmbiguousOptionException exception = new AmbiguousOptionException("foo", Arrays.asList(new Option().setLongName("foobar"), new Option().setLongName("foobaz"))); assertThat(exception.getOptions()).hasSize(2); assertThat(exception.getToken()).isEqualTo("foo"); assertThat(exception.getMessage()) .contains("Ambiguous argument in command line") .contains("'foo'") .contains("foobar").contains("foobaz"); }
@Test public void testAmbiguousPartialLongOption3() throws Exception { String[] args = new String[]{"--ver=1"}; TypedOption<Integer> v1 = new TypedOption<Integer>().setLongName("verbose") .setSingleValued(true) .setType(Integer.class); TypedOption<Boolean> v2 = new TypedOption<Boolean>().setLongName("version").setType(Boolean.class) .setSingleValued(false); cli.addOption(v1).addOption(v2); try { cli.parse(Arrays.asList(args)); fail("Exception expected"); } catch (AmbiguousOptionException e) { assertThat(e.getToken()).isEqualTo("--ver"); assertThat(e.getOptions()).hasSize(2); } }
/** * Handles the following tokens: * <p/> * --L * -L * --l * -l * * @param token the command line token to handle */ private void handleLongOptionWithoutEqual(String token) throws CLIException { List<Option> matchingOpts = getMatchingOptions(token); if (matchingOpts.isEmpty()) { handleArgument(token); } else if (matchingOpts.size() > 1) { throw new AmbiguousOptionException(token, matchingOpts); } else { final Option option = matchingOpts.get(0); handleOption(option); } }
@Test public void testAmbiguousPartialLongOption4() throws Exception { String[] args = new String[]{"-ver=1"}; TypedOption<Integer> v1 = new TypedOption<Integer>().setLongName("verbose") .setSingleValued(true) .setType(Integer.class); TypedOption<Boolean> v2 = new TypedOption<Boolean>().setLongName("version").setType(Boolean.class) .setSingleValued(false); cli.addOption(v1).addOption(v2); try { cli.parse(Arrays.asList(args)); fail("Exception expected"); } catch (AmbiguousOptionException e) { assertThat(e.getToken()).isEqualTo("-ver"); assertThat(e.getOptions()).hasSize(2); } }
/** * 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 testAmbiguousPartialLongOption1() throws Exception { String[] args = new String[]{"--ver"}; TypedOption<Integer> v1 = new TypedOption<Integer>().setLongName("verbose") .setSingleValued(true) .setType(Integer.class); TypedOption<Boolean> v2 = new TypedOption<Boolean>().setLongName("version") .setType(Boolean.class) .setSingleValued(false); cli.addOption(v1).addOption(v2); try { cli.parse(Arrays.asList(args)); fail("Exception expected"); } catch (AmbiguousOptionException e) { assertThat(e.getToken()).isEqualTo("--ver"); assertThat(e.getOptions()).hasSize(2); } }
@Test public void testAmbiguousPartialLongOption2() throws Exception { String[] args = new String[]{"-ver"}; TypedOption<Integer> v1 = new TypedOption<Integer>().setLongName("verbose") .setSingleValued(true) .setType(Integer.class); TypedOption<Boolean> v2 = new TypedOption<Boolean>().setLongName("version").setType(Boolean.class) .setSingleValued(false); cli.addOption(v1).addOption(v2); try { cli.parse(Arrays.asList(args)); fail("Exception expected"); } catch (AmbiguousOptionException e) { assertThat(e.getToken()).isEqualTo("-ver"); assertThat(e.getOptions()).hasSize(2); } }
@Test public void testAmbiguousPartialLongOption3() throws Exception { String[] args = new String[]{"--ver=1"}; TypedOption<Integer> v1 = new TypedOption<Integer>().setLongName("verbose") .setSingleValued(true) .setType(Integer.class); TypedOption<Boolean> v2 = new TypedOption<Boolean>().setLongName("version").setType(Boolean.class) .setSingleValued(false); cli.addOption(v1).addOption(v2); try { cli.parse(Arrays.asList(args)); fail("Exception expected"); } catch (AmbiguousOptionException e) { assertThat(e.getToken()).isEqualTo("--ver"); assertThat(e.getOptions()).hasSize(2); } }
@Test public void testAmbiguousPartialLongOption4() throws Exception { String[] args = new String[]{"-ver=1"}; TypedOption<Integer> v1 = new TypedOption<Integer>().setLongName("verbose") .setSingleValued(true) .setType(Integer.class); TypedOption<Boolean> v2 = new TypedOption<Boolean>().setLongName("version").setType(Boolean.class) .setSingleValued(false); cli.addOption(v1).addOption(v2); try { cli.parse(Arrays.asList(args)); fail("Exception expected"); } catch (AmbiguousOptionException e) { assertThat(e.getToken()).isEqualTo("-ver"); assertThat(e.getOptions()).hasSize(2); } }