Java Code Examples for picocli.CommandLine.Model.CommandSpec#create()

The following examples show how to use picocli.CommandLine.Model.CommandSpec#create() . 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: ModelMethodBindingTest.java    From picocli with Apache License 2.0 6 votes vote down vote up
@Test
public void testSetFailsIfObjectNotSet_ForSetterMethod() throws Exception {
    Method setX = ModelMethodBindingBean.class.getDeclaredMethod("setX", int.class);
    setX.setAccessible(true);

    CommandSpec spec = CommandSpec.create();
    MethodBinding binding = new MethodBinding(new ObjectScope(null), setX, spec);

    try {
        binding.set(41);
        fail("Expect exception");
    } catch (Exception ex) {
        ParameterException pex = (ParameterException) ex;
        assertSame(spec, pex.getCommandLine().getCommandSpec());
        assertThat(pex.getCause().getClass().toString(), pex.getCause() instanceof NullPointerException);
    }
}
 
Example 2
Source File: ModelUnmatchedArgsBindingTest.java    From picocli with Apache License 2.0 6 votes vote down vote up
@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 3
Source File: ModelUnmatchedArgsBindingTest.java    From picocli with Apache License 2.0 6 votes vote down vote up
@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"));
    }
}
 
Example 4
Source File: ModelCommandSpecTest.java    From picocli with Apache License 2.0 6 votes vote down vote up
@Test
public void testPositionalClearCustomSetterBeforeParse() {
    CommandSpec cmd = CommandSpec.create();
    final List<Object> values = new ArrayList<Object>();
    ISetter setter = new ISetter() {
        public <T> T set(T value) {
            values.add(value);
            return null;
        }
    };
    cmd.add(PositionalParamSpec.builder().type(String.class).setter(setter).build());

    CommandLine cl = new CommandLine(cmd);
    assertTrue(values.isEmpty());
    cl.parseArgs("1");
    assertEquals(2, values.size());
    assertEquals(null, values.get(0));
    assertEquals("1", values.get(1));

    values.clear();
    cl.parseArgs("2");
    assertEquals(2, values.size());
    assertEquals(null, values.get(0));
    assertEquals("2", values.get(1));
}
 
Example 5
Source File: ModelUsageMessageSpecTest.java    From picocli with Apache License 2.0 6 votes vote down vote up
@Test
public void testModelUsageHelpWithCustomSeparator() {
    CommandSpec spec = CommandSpec.create();
    spec.addOption(OptionSpec.builder("-h", "--help").usageHelp(true).description("show help and exit").build());
    spec.addOption(OptionSpec.builder("-V", "--version").versionHelp(true).description("show help and exit").build());
    spec.addOption(OptionSpec.builder("-c", "--count").paramLabel("COUNT").arity("1").type(int.class).description("number of times to execute").build());
    spec.addOption(OptionSpec.builder("-f", "--fix").paramLabel("FIXED(=BOOLEAN)").arity("1").hideParamSyntax(true).required(true).description("run with fixed option").build());
    CommandLine commandLine = new CommandLine(spec).setSeparator(" ");
    String actual = usageString(commandLine, CommandLine.Help.Ansi.OFF);
    String expected = String.format("" +
            "Usage: <main class> [-hV] [-c COUNT] -f FIXED(=BOOLEAN)%n" +
            "  -c, --count COUNT   number of times to execute%n" +
            "  -f, --fix FIXED(=BOOLEAN)%n" +
            "                      run with fixed option%n" +
            "  -h, --help          show help and exit%n" +
            "  -V, --version       show help and exit%n");
    assertEquals(expected, actual);
}
 
Example 6
Source File: NegatableOptionTest.java    From picocli with Apache License 2.0 5 votes vote down vote up
@Test
public void testRegexTransformDefault() {
    CommandLine.INegatableOptionTransformer transformer = CommandLine.RegexTransformer.createDefault();

    CommandSpec dummy = CommandSpec.create();
    assertEquals("-X:-option", transformer.makeNegative("-X:+option", dummy));
    assertEquals("-X:(+|-)option", transformer.makeSynopsis("-X:+option", dummy));
    assertEquals("-X:+option", transformer.makeNegative("-X:-option", dummy));
    assertEquals("-X:(+|-)option", transformer.makeSynopsis("-X:-option", dummy));

    assertEquals("--no-verbose", transformer.makeNegative("--verbose", dummy));
    assertEquals("--[no-]verbose", transformer.makeSynopsis("--verbose", dummy));
    assertEquals("--verbose", transformer.makeNegative("--no-verbose", dummy));
    assertEquals("--[no-]verbose", transformer.makeSynopsis("--no-verbose", dummy));
}
 
Example 7
Source File: ModelCommandSpecTest.java    From picocli with Apache License 2.0 5 votes vote down vote up
@Test
public void testMultiValuePositionalParamWithListWithoutAuxTypes() {
    CommandSpec spec = CommandSpec.create();
    PositionalParamSpec positional = PositionalParamSpec.builder().index("0").arity("3").type(List.class).build();
    assertTrue(positional.isMultiValue());

    spec.addPositional(positional);
    CommandLine commandLine = new CommandLine(spec);
    commandLine.parseArgs("1", "2", "3");
    assertEquals(Arrays.asList("1", "2", "3"), spec.positionalParameters().get(0).getValue());
}
 
Example 8
Source File: ExecuteTest.java    From picocli with Apache License 2.0 5 votes vote down vote up
@Test
public void testCommandSpecExitCodesMutable() {
    CommandSpec spec = CommandSpec.create();
    spec.exitCodeOnSuccess(1)
            .exitCodeOnUsageHelp(2)
            .exitCodeOnVersionHelp(3)
            .exitCodeOnInvalidInput(4)
            .exitCodeOnExecutionException(5);

    assertEquals(1, spec.exitCodeOnSuccess());
    assertEquals(2, spec.exitCodeOnUsageHelp());
    assertEquals(3, spec.exitCodeOnVersionHelp());
    assertEquals(4, spec.exitCodeOnInvalidInput());
    assertEquals(5, spec.exitCodeOnExecutionException());
}
 
Example 9
Source File: ModelCommandSpecTest.java    From picocli with Apache License 2.0 5 votes vote down vote up
@Test
public void testMultiValueOptionWithMapWithoutAuxTypes() {
    CommandSpec spec = CommandSpec.create();
    OptionSpec option = OptionSpec.builder("-c", "--count").arity("3").type(Map.class).build();
    assertTrue(option.isMultiValue());

    spec.addOption(option);
    CommandLine commandLine = new CommandLine(spec);
    commandLine.parseArgs("-c", "1=1.0", "2=2.0", "3=3.0");
    Map<String, String> expected = new LinkedHashMap<String, String>();
    expected.put("1", "1.0");
    expected.put("2", "2.0");
    expected.put("3", "3.0");
    assertEquals(expected, spec.optionsMap().get("-c").getValue());
}
 
Example 10
Source File: ModelCommandSpecTest.java    From picocli with Apache License 2.0 5 votes vote down vote up
@Test
public void testMultiValuePositionalParamWithArray() {
    CommandSpec spec = CommandSpec.create();
    PositionalParamSpec positional = PositionalParamSpec.builder().index("0").arity("3").type(int[].class).build();
    assertTrue(positional.isMultiValue());

    spec.addPositional(positional);
    CommandLine commandLine = new CommandLine(spec);
    commandLine.parseArgs("1", "2", "3");
    assertArrayEquals(new int[] {1, 2, 3}, (int[]) spec.positionalParameters().get(0).getValue());
}
 
Example 11
Source File: NegatableOptionTest.java    From picocli with Apache License 2.0 5 votes vote down vote up
@Test
public void testRegexTransformCustomForShortOptions() {
    CommandLine.RegexTransformer transformer = createNegatableShortOptionsTransformer();

    CommandSpec dummy = CommandSpec.create();
    assertEquals("-x", transformer.makeNegative("+x", dummy));
    assertEquals("\u00b1x", transformer.makeSynopsis("+x", dummy));
    assertEquals("+x", transformer.makeNegative("-x", dummy));
    assertEquals("\u00b1x", transformer.makeSynopsis("-x", dummy));
}
 
Example 12
Source File: ModelCommandSpecTest.java    From picocli with Apache License 2.0 5 votes vote down vote up
@Test
public void testModelParse() {
    CommandSpec spec = CommandSpec.create();
    spec.addOption(OptionSpec.builder("-h", "--help").usageHelp(true).description("show help and exit").build());
    spec.addOption(OptionSpec.builder("-V", "--version").versionHelp(true).description("show help and exit").build());
    spec.addOption(OptionSpec.builder("-c", "--count").paramLabel("COUNT").arity("1").type(int.class).description("number of times to execute").build());
    CommandLine commandLine = new CommandLine(spec);
    commandLine.parseArgs("-c", "33");
    assertEquals(Integer.valueOf(33), spec.optionsMap().get("-c").getValue());
}
 
Example 13
Source File: ModelUsageMessageSpecTest.java    From picocli with Apache License 2.0 5 votes vote down vote up
@Test
public void testUsageHelp_abbreviateSynopsisWithoutPositional() throws UnsupportedEncodingException {
    CommandSpec spec = CommandSpec.create();
    spec.usageMessage().abbreviateSynopsis(true).requiredOptionMarker('!').sortOptions(false);
    spec.addOption(OptionSpec.builder("-x").required(true).description("required").build());
    CommandLine commandLine = new CommandLine(spec);
    String actual = usageString(commandLine, CommandLine.Help.Ansi.OFF);
    String expected = String.format("" +
            "Usage: <main class> [OPTIONS]%n" +
            "! -x     required%n");
    assertEquals(expected, actual);
}
 
Example 14
Source File: ModelCommandSpecTest.java    From picocli with Apache License 2.0 5 votes vote down vote up
@Test
public void testClearScalarOptionOldValueBeforeParse() {
    CommandSpec cmd = CommandSpec.create();
    cmd.addOption(OptionSpec.builder("-x").type(String.class).initialValue(null).build());

    CommandLine cl = new CommandLine(cmd);
    cl.parseArgs("-x", "1");
    assertEquals("1", cmd.findOption("x").getValue());

    cl.parseArgs("-x", "2");
    assertEquals("2", cmd.findOption("x").getValue());

    cl.parseArgs();
    assertNull(cmd.findOption("x").getValue());
}
 
Example 15
Source File: ModelCommandSpecTest.java    From picocli with Apache License 2.0 5 votes vote down vote up
@Test
public void testMultiValuePositionalParamWithMapWithoutAuxTypes() {
    CommandSpec spec = CommandSpec.create();
    PositionalParamSpec positional = PositionalParamSpec.builder().index("0").arity("3").type(Map.class).build();
    assertTrue(positional.isMultiValue());

    spec.addPositional(positional);
    CommandLine commandLine = new CommandLine(spec);
    commandLine.parseArgs("1=1.0", "2=2.0", "3=3.0");
    Map<String, String> expected = new LinkedHashMap<String, String>();
    expected.put("1", "1.0");
    expected.put("2", "2.0");
    expected.put("3", "3.0");
    assertEquals(expected, spec.positionalParameters().get(0).getValue());
}
 
Example 16
Source File: InheritedOptionTest.java    From picocli with Apache License 2.0 5 votes vote down vote up
@Test
public void testProgrammaticAddPositionalParamBeforeSub() {
    PositionalParamSpec positional = PositionalParamSpec.builder().scopeType(INHERIT).build();
    CommandSpec spec = CommandSpec.create();
    spec.add(positional);
    assertFalse(positional.inherited());

    CommandSpec sub = CommandSpec.create();
    spec.addSubcommand("sub", sub);
    assertFalse(spec.positionalParameters().isEmpty());
    assertFalse(sub.positionalParameters().isEmpty());

    assertFalse(spec.positionalParameters().get(0).inherited());
    assertTrue(sub.positionalParameters().get(0).inherited());
}
 
Example 17
Source File: ModelUsageMessageSpecTest.java    From picocli with Apache License 2.0 5 votes vote down vote up
@Test
public void testEmptyModelUsageHelp() {
    CommandSpec spec = CommandSpec.create();
    CommandLine commandLine = new CommandLine(spec);
    String actual = usageString(commandLine, CommandLine.Help.Ansi.OFF);
    assertEquals(String.format("Usage: <main class>%n"), actual);
}
 
Example 18
Source File: ModelCommandSpecTest.java    From picocli with Apache License 2.0 5 votes vote down vote up
@Test
public void testMultiValueOptionWithMapAndAuxTypes() {
    CommandSpec spec = CommandSpec.create();
    OptionSpec option = OptionSpec.builder("-c", "--count").arity("3").type(Map.class).auxiliaryTypes(Integer.class, Double.class).build();
    assertTrue(option.isMultiValue());

    spec.addOption(option);
    CommandLine commandLine = new CommandLine(spec);
    commandLine.parseArgs("-c", "1=1.0", "2=2.0", "3=3.0");
    Map<Integer, Double> expected = new LinkedHashMap<Integer, Double>();
    expected.put(1, 1.0);
    expected.put(2, 2.0);
    expected.put(3, 3.0);
    assertEquals(expected, spec.optionsMap().get("-c").getValue());
}
 
Example 19
Source File: InheritedOptionTest.java    From picocli with Apache License 2.0 5 votes vote down vote up
@Test
public void testProgrammaticAddPositionalParamAfterSub() {
    PositionalParamSpec positional = PositionalParamSpec.builder().scopeType(INHERIT).build();
    CommandSpec spec = CommandSpec.create();
    CommandSpec sub = CommandSpec.create();
    spec.addSubcommand("sub", sub);
    spec.add(positional);
    assertFalse(positional.inherited());

    assertFalse(spec.positionalParameters().isEmpty());
    assertFalse(sub.positionalParameters().isEmpty());

    assertFalse(spec.positionalParameters().get(0).inherited());
    assertTrue(sub.positionalParameters().get(0).inherited());
}
 
Example 20
Source File: ModelMessagesTest.java    From picocli with Apache License 2.0 4 votes vote down vote up
@Test
public void testMessagesCommandSpec() {
    CommandSpec spec = CommandSpec.create();
    Messages orig = new Messages(spec, (ResourceBundle) null);
    assertSame(spec, orig.commandSpec());
}