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

The following examples show how to use picocli.CommandLine.Model.CommandSpec#wrapWithoutInspection() . 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: CommandSpecFactory.java    From milkman with MIT License 6 votes vote down vote up
CommandSpec getSpecFor(TerminalCommand cmd) {
       var cmdSpec = CommandSpec.wrapWithoutInspection(cmd);
       cmdSpec.name(cmd.getName())
       	.aliases(cmd.getAlias())
       	.usageMessage(new UsageMessageSpec()
       			.description(cmd.getDescription()));
       
       int idx = 0;
       for (Parameter parameter : cmd.getParameters()) {
       	addPositionalParameter(cmdSpec, parameter, idx);
       	idx++;
       }
       
       for(Option option : cmd.getOptions()) {
       	addBooleanOption(cmdSpec, option);
       }
	return cmdSpec;
}
 
Example 2
Source File: VersionProviderTest.java    From picocli with Apache License 2.0 6 votes vote down vote up
@Test
public void testInitVersionProvider() {
    CommandLine.IVersionProvider versionProvider1 = new CommandLine.IVersionProvider() {
        public String[] getVersion() { return new String[0]; }
    };
    CommandLine.IVersionProvider versionProvider2 = new CommandLine.IVersionProvider() {
        public String[] getVersion() { return new String[0];  }
    };

    CommandSpec spec = CommandSpec.wrapWithoutInspection(null);
    spec.versionProvider(versionProvider1);

    CommandSpec mixin = CommandSpec.wrapWithoutInspection(null);
    mixin.versionProvider(versionProvider2);

    spec.addMixin("helper", mixin);
    assertSame(versionProvider1, spec.versionProvider());
}
 
Example 3
Source File: ModelCommandSpecTest.java    From picocli with Apache License 2.0 6 votes vote down vote up
@Test
public void testResemblesOption_WithOptionsNonDash() {
    CommandSpec spec = CommandSpec.wrapWithoutInspection(null);
    spec.addOption(OptionSpec.builder("/x").build());

    spec.parser().unmatchedOptionsArePositionalParams(false);
    assertFalse(spec.resemblesOption("blah", null));

    System.setProperty("picocli.trace", "DEBUG");
    Tracer tracer = new Tracer();
    System.clearProperty("picocli.trace");
    assertFalse(spec.resemblesOption("blah", tracer));
    assertFalse(spec.resemblesOption("-a", tracer));
    assertTrue(spec.resemblesOption("/a", tracer));

    Tracer tracer2 = new Tracer();
    assertFalse(spec.resemblesOption("blah", tracer2));
    assertFalse(spec.resemblesOption("-a", tracer));
    assertTrue(spec.resemblesOption("/a", tracer));
}
 
Example 4
Source File: ModelCommandSpecTest.java    From picocli with Apache License 2.0 6 votes vote down vote up
@Test
public void testResemblesOption_WithOptionsDash() {
    CommandSpec spec = CommandSpec.wrapWithoutInspection(null);
    spec.addOption(OptionSpec.builder("-x").build());

    spec.parser().unmatchedOptionsArePositionalParams(false);
    assertFalse(spec.resemblesOption("blah", null));

    System.setProperty("picocli.trace", "DEBUG");
    Tracer tracer = new Tracer();
    System.clearProperty("picocli.trace");
    assertFalse(spec.resemblesOption("blah", tracer));
    assertTrue(spec.resemblesOption("-a", tracer));
    assertFalse(spec.resemblesOption("/a", tracer));

    Tracer tracer2 = new Tracer();
    assertFalse(spec.resemblesOption("blah", tracer2));
    assertTrue(spec.resemblesOption("-a", tracer));
    assertFalse(spec.resemblesOption("/a", tracer));
}
 
Example 5
Source File: ModelCommandSpecTest.java    From picocli with Apache License 2.0 6 votes vote down vote up
@Test
public void testResemblesOption_WithoutOptions() {
    CommandSpec spec = CommandSpec.wrapWithoutInspection(null);
    spec.parser().unmatchedOptionsArePositionalParams(false);
    assertFalse(spec.resemblesOption("blah", null));

    System.setProperty("picocli.trace", "DEBUG");
    Tracer tracer = new Tracer();
    System.clearProperty("picocli.trace");
    assertFalse(spec.resemblesOption("blah", tracer));
    assertTrue(spec.resemblesOption("-a", tracer));

    Tracer tracer2 = new Tracer();
    assertFalse(spec.resemblesOption("blah", tracer2));
    assertTrue(spec.resemblesOption("-a", tracer));
}
 
Example 6
Source File: ModelCommandSpecTest.java    From picocli with Apache License 2.0 5 votes vote down vote up
@Test
public void testCommandSpecParserSetter() {
    CommandSpec spec = CommandSpec.wrapWithoutInspection(null);
    ParserSpec old = spec.parser();
    assertSame(old, spec.parser());
    assertFalse(spec.parser().collectErrors());
    assertFalse(spec.parser().caseInsensitiveEnumValuesAllowed());

    ParserSpec update = new ParserSpec().collectErrors(true).caseInsensitiveEnumValuesAllowed(true);
    spec.parser(update);
    assertSame(old, spec.parser());
    assertTrue(spec.parser().collectErrors());
    assertTrue(spec.parser().caseInsensitiveEnumValuesAllowed());
}
 
Example 7
Source File: CommandMethodTest.java    From picocli with Apache License 2.0 5 votes vote down vote up
@Test
public void testAddMethodSubcommands_DisallowedIfUserObjectIsMethod() throws Exception {
    Method m = MethodApp.class.getDeclaredMethod("run1", int.class);
    CommandSpec spec = CommandSpec.wrapWithoutInspection(m);

    try {
        spec.addMethodSubcommands();
    } catch (InitializationException ex) {
        assertEquals("Cannot discover subcommand methods of this Command Method: int picocli.CommandMethodTest$MethodApp.run1(int)", ex.getMessage());
    }
}
 
Example 8
Source File: CommandMethodTest.java    From picocli with Apache License 2.0 5 votes vote down vote up
@Test
public void testAddMethodSubcommands() {
    CommandSpec spec = CommandSpec.wrapWithoutInspection(new StaticMethodCommand(1));
    assertEquals(0, spec.subcommands().size());

    spec.addMethodSubcommands();
    assertEquals(4, spec.subcommands().size());
}
 
Example 9
Source File: ModelCommandSpecTest.java    From picocli with Apache License 2.0 5 votes vote down vote up
@Test
public void testResemblesOption_WhenUnmatchedArePositional() {
    CommandSpec spec = CommandSpec.wrapWithoutInspection(null);
    spec.parser().unmatchedOptionsArePositionalParams(true);
    assertFalse(spec.resemblesOption("blah", null));

    System.setProperty("picocli.trace", "DEBUG");
    Tracer tracer = new Tracer();
    System.clearProperty("picocli.trace");
    assertFalse(spec.resemblesOption("blah", tracer));

    Tracer tracer2 = new Tracer();
    assertFalse(spec.resemblesOption("blah", tracer2));
}
 
Example 10
Source File: ModelCommandSpecTest.java    From picocli with Apache License 2.0 5 votes vote down vote up
@Test
public void testInitHelpCommand() {
    CommandSpec spec = CommandSpec.wrapWithoutInspection(null);
    assertFalse(spec.helpCommand());

    CommandSpec mixin = CommandSpec.wrapWithoutInspection(null);
    mixin.helpCommand(true);

    spec.addMixin("helper", mixin);
    assertTrue(spec.helpCommand());
}
 
Example 11
Source File: ModelCommandSpecTest.java    From picocli with Apache License 2.0 5 votes vote down vote up
@Test
public void testNamesIncludesAliases() {
    CommandSpec spec = CommandSpec.wrapWithoutInspection(null);
    spec.aliases("a", "b", "d");
    spec.name("c");
    Set<String> all = spec.names();
    assertArrayEquals(new String[] {"c", "a", "b", "d"}, all.toArray(new String[0]));
}
 
Example 12
Source File: ModelCommandSpecTest.java    From picocli with Apache License 2.0 5 votes vote down vote up
@Test
public void testAliasesWithEmptyArray() {
    CommandSpec spec = CommandSpec.wrapWithoutInspection(null);
    assertArrayEquals(new String[0], spec.aliases());
    spec.aliases((String[]) null);
    assertArrayEquals(new String[0], spec.aliases());
}
 
Example 13
Source File: ModelCommandSpecTest.java    From picocli with Apache License 2.0 5 votes vote down vote up
@Test
public void testCommandSpecAddSubcommand_SubcommandInheritsResourceBundle() {
    ResourceBundle rb = ResourceBundle.getBundle("picocli.SharedMessages");
    CommandSpec spec = CommandSpec.wrapWithoutInspection(null);
    spec.resourceBundle(rb);
    assertSame(rb, spec.resourceBundle());

    CommandSpec sub = CommandSpec.wrapWithoutInspection(null);
    spec.addSubcommand("a", new CommandLine(sub));

    assertSame(rb, sub.resourceBundle());
}
 
Example 14
Source File: ModelCommandSpecTest.java    From picocli with Apache License 2.0 5 votes vote down vote up
@Test
public void testCommandSpecAddSubcommand_DisallowsDuplicateSubcommandAliases() {
    CommandSpec spec = CommandSpec.wrapWithoutInspection(null);
    CommandSpec sub = CommandSpec.wrapWithoutInspection(null);

    spec.addSubcommand("a", new CommandLine(sub));

    CommandSpec sub2 = CommandSpec.wrapWithoutInspection(null);
    sub2.aliases("a");
    try {
        spec.addSubcommand("x", new CommandLine(sub2));
    } catch (InitializationException ex) {
        assertEquals("Alias 'a' for subcommand 'x' is already used by another subcommand of '<main class>'", ex.getMessage());
    }
}
 
Example 15
Source File: ModelCommandSpecTest.java    From picocli with Apache License 2.0 5 votes vote down vote up
@Test
public void testCommandSpecAddSubcommand_DisallowsDuplicateSubcommandNames() {
    CommandSpec spec = CommandSpec.wrapWithoutInspection(null);
    CommandSpec sub = CommandSpec.wrapWithoutInspection(null);

    spec.addSubcommand("a", new CommandLine(sub));
    try {
        spec.addSubcommand("a", new CommandLine(sub));
    } catch (InitializationException ex) {
        assertEquals("Another subcommand named 'a' already exists for command '<main class>'", ex.getMessage());
    }
}
 
Example 16
Source File: ModelCommandSpecTest.java    From picocli with Apache License 2.0 5 votes vote down vote up
@Test
public void testCommandSpecUsageMessageSetter() {
    CommandSpec spec = CommandSpec.wrapWithoutInspection(null);
    UsageMessageSpec old = spec.usageMessage();
    assertSame(old, spec.usageMessage());
    assertArrayEquals(new String[0], spec.usageMessage().description());

    UsageMessageSpec update = new UsageMessageSpec().description("hi");
    spec.usageMessage(update);
    assertSame(old, spec.usageMessage());
    assertArrayEquals(new String[] {"hi"}, spec.usageMessage().description());
}
 
Example 17
Source File: CommandUserObjectTest.java    From picocli with Apache License 2.0 4 votes vote down vote up
@Test
public void testCommandSpecWrapWithoutInspection() {
    String userObject = "hello";
    CommandSpec spec = CommandSpec.wrapWithoutInspection(userObject, CommandLine.defaultFactory());
    assertSame(userObject, spec.userObject());
}