Java Code Examples for picocli.CommandLine.Model.CommandSpec#addUnmatchedArgsBinding()
The following examples show how to use
picocli.CommandLine.Model.CommandSpec#addUnmatchedArgsBinding() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ModelUnmatchedArgsBindingTest.java From picocli with Apache License 2.0 | 6 votes |
@Test public void testUnmatchedArgsBinding_forStringArrayConsumer() { setTraceLevel("OFF"); class ArrayBinding implements ISetter { String[] array; @SuppressWarnings("unchecked") public <T> T set(T value) { T old = (T) array; array = (String[]) value; return old; } } ArrayBinding setter = new ArrayBinding(); CommandSpec cmd = CommandSpec.create(); UnmatchedArgsBinding unmatched = UnmatchedArgsBinding.forStringArrayConsumer(setter); assertSame(setter, unmatched.setter()); cmd.addUnmatchedArgsBinding(unmatched); cmd.addOption(CommandLine.Model.OptionSpec.builder("-x").build()); CommandLine.ParseResult result = new CommandLine(cmd).parseArgs("-x", "a", "b", "c"); assertEquals(Arrays.asList("a", "b", "c"), result.unmatched()); assertArrayEquals(new String[]{"a", "b", "c"}, setter.array); assertSame(unmatched, cmd.unmatchedArgsBindings().get(0)); assertEquals(1, cmd.unmatchedArgsBindings().size()); }
Example 2
Source File: ModelUnmatchedArgsBindingTest.java From picocli with Apache License 2.0 | 6 votes |
@Test public void testUnmatchedArgsBinding_forStringCollectionSupplier() { setTraceLevel("OFF"); class ArrayBinding implements IGetter { List<String> list = new ArrayList<String>(); @SuppressWarnings("unchecked") public <T> T get() { return (T) list; } } ArrayBinding binding = new ArrayBinding(); CommandSpec cmd = CommandSpec.create(); UnmatchedArgsBinding unmatched = UnmatchedArgsBinding.forStringCollectionSupplier(binding); assertSame(binding, unmatched.getter()); cmd.addUnmatchedArgsBinding(unmatched); cmd.addOption(CommandLine.Model.OptionSpec.builder("-x").build()); CommandLine.ParseResult result = new CommandLine(cmd).parseArgs("-x", "a", "b", "c"); assertEquals(Arrays.asList("a", "b", "c"), result.unmatched()); assertEquals(Arrays.asList("a", "b", "c"), binding.list); assertSame(unmatched, cmd.unmatchedArgsBindings().get(0)); assertEquals(1, cmd.unmatchedArgsBindings().size()); }
Example 3
Source File: ModelUnmatchedArgsBindingTest.java From picocli with Apache License 2.0 | 6 votes |
@Test public void testUnmatchedArgsBinding_forStringArrayConsumer_withInvalidBinding() { setTraceLevel("OFF"); class ListBinding implements ISetter { List<String> list = new ArrayList<String>(); @SuppressWarnings("unchecked") public <T> T set(T value) { T old = (T) list; list = (List<String>) value; return old; } } CommandSpec cmd = CommandSpec.create(); cmd.addUnmatchedArgsBinding(UnmatchedArgsBinding.forStringArrayConsumer(new ListBinding())); try { new CommandLine(cmd).parseArgs("-x", "a", "b", "c"); } catch (Exception ex) { assertTrue(ex.getMessage(), ex.getMessage().startsWith("Could not invoke setter (")); assertTrue(ex.getMessage(), ex.getMessage().contains("with unmatched argument array '[-x, a, b, c]': java.lang.ClassCastException")); } }
Example 4
Source File: ModelUnmatchedArgsBindingTest.java From picocli with Apache License 2.0 | 6 votes |
@Test public void testUnmatchedArgsBinding_forStringCollectionSupplier_withInvalidBinding() { setTraceLevel("OFF"); class ListBinding implements IGetter { @SuppressWarnings("unchecked") public <T> T get() { return (T) new Object(); } } CommandSpec cmd = CommandSpec.create(); cmd.addUnmatchedArgsBinding(UnmatchedArgsBinding.forStringCollectionSupplier(new ListBinding())); try { new CommandLine(cmd).parseArgs("-x", "a", "b", "c"); } catch (Exception ex) { assertTrue(ex.getMessage(), ex.getMessage().startsWith("Could not add unmatched argument array '[-x, a, b, c]' to collection returned by getter (")); assertTrue(ex.getMessage(), ex.getMessage().contains("): java.lang.ClassCastException: ")); assertTrue(ex.getMessage(), ex.getMessage().contains("java.lang.Object")); } }