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

The following examples show how to use picocli.CommandLine.Model.CommandSpec#forAnnotatedObject() . 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: PicoCLIOptionsImpl.java    From besu with Apache License 2.0 6 votes vote down vote up
@Override
public void addPicoCLIOptions(final String namespace, final Object optionObject) {
  final String pluginPrefix = "--plugin-" + namespace + "-";
  final String unstablePrefix = "--Xplugin-" + namespace + "-";
  final CommandSpec mixin = CommandSpec.forAnnotatedObject(optionObject);
  boolean badOptionName = false;

  for (final OptionSpec optionSpec : mixin.options()) {
    for (final String optionName : optionSpec.names()) {
      if (!optionName.startsWith(pluginPrefix) && !optionName.startsWith(unstablePrefix)) {
        badOptionName = true;
        LOG.error(
            "Plugin option {} did not have the expected prefix of {}", optionName, pluginPrefix);
      }
    }
  }
  if (badOptionName) {
    throw new RuntimeException("Error loading CLI options");
  } else {
    commandLine.getCommandSpec().addMixin("Plugin " + namespace, mixin);
  }
}
 
Example 2
Source File: ExecuteTest.java    From picocli with Apache License 2.0 6 votes vote down vote up
@Test
public void testCommandSpecExitCodesFromAnnotations() {
    @Command(exitCodeOnSuccess = 1
           , exitCodeOnUsageHelp = 2
           , exitCodeOnVersionHelp = 3
           , exitCodeOnInvalidInput = 4
           , exitCodeOnExecutionException = 5)
    class Annotated{}
    CommandSpec spec = CommandSpec.forAnnotatedObject(new Annotated());

    assertEquals(1, spec.exitCodeOnSuccess());
    assertEquals(2, spec.exitCodeOnUsageHelp());
    assertEquals(3, spec.exitCodeOnVersionHelp());
    assertEquals(4, spec.exitCodeOnInvalidInput());
    assertEquals(5, spec.exitCodeOnExecutionException());
}
 
Example 3
Source File: VSCommandBase.java    From Valkyrien-Skies with Apache License 2.0 6 votes vote down vote up
@SneakyThrows
@Override
public List<String> getTabCompletions(MinecraftServer server, ICommandSender sender,
    final String[] splitArgs, @Nullable BlockPos targetPos) {

    VSCommandFactory factory = new VSCommandFactory(sender);
    CommandSpec spec = CommandSpec.forAnnotatedObject(factory.create(cmdClass), factory);

    String[] args = VSCommandUtil.toTabCompleteArgs(splitArgs);
    List<CharSequence> candidates = new ArrayList<>();

    AutoComplete.complete(spec, args, args.length - 1,
        args[args.length - 1].length(), 500, candidates);

    return candidates.stream()
        .distinct()
        .map(CharSequence::toString)
        .map(s -> args[args.length - 1] + s)
        .collect(Collectors.toList());
}
 
Example 4
Source File: AnnotatedCommandSourceGeneratorTest.java    From picocli with Apache License 2.0 5 votes vote down vote up
@Ignore
@Test
public void generate() {
    CommandSpec spec = CommandSpec.forAnnotatedObject(Example.class);
    String generated = new AnnotatedCommandSourceGenerator(spec).generate();
    //System.out.println(generated);
    
    String expected = Resources.slurp("/picocli/codegen/aot/graalvm/Example.txt");
    assertEquals(expected, generated);
}
 
Example 5
Source File: ModelCommandSpecTest.java    From picocli with Apache License 2.0 5 votes vote down vote up
@Test
public void testCommandSpec_forAnnotatedObject_requiresPicocliAnnotation() {
    Object userObject = new Object();
    try {
        CommandSpec.forAnnotatedObject(userObject);
        fail("Expected error");
    } catch (InitializationException ok) {
        assertEquals(userObject + " is not a command: it has no @Command, @Option, @Parameters or @Unmatched annotations", ok.getMessage());
    }
}
 
Example 6
Source File: AutoCompleteTest.java    From picocli with Apache License 2.0 5 votes vote down vote up
@Test
public void testCompleteFindCompletionStartPoint() {
    class App {
        @Option(names = "-x", arity = "2") List<Possibilities> poss;
    }
    CommandSpec spec = CommandSpec.forAnnotatedObject(new App());
    int cur = 500;
    test(spec, a("-x"),               1, 0, cur, l("Aaa", "Bbb", "Ccc"));
    test(spec, a("-x", "A"),          1, 0, cur, l("Aaa", "Bbb", "Ccc")); // suggest 1st arg of same type
    test(spec, a("-x", "A"),          1, 1, cur, l("aa"));
    test(spec, a("-x", "Aaa"),        2, 0, cur, l("Aaa", "Bbb", "Ccc")); // suggest 2nd arg of same type
    test(spec, a("-x", "Aaa", "Bbb"), 3, 0, cur, l("-x")); // we have 2 args for first -x. Suggest -x again.
}
 
Example 7
Source File: AutoCompleteTest.java    From picocli with Apache License 2.0 5 votes vote down vote up
@Test
public void testCompleteFindPositionalForTopLevelCommand() {
    class App {
        @Parameters() List<Possibilities> poss;
    }
    CommandSpec spec = CommandSpec.forAnnotatedObject(new App());
    int cur = 500;
    test(spec, a(),                    0, 0, cur, l("Aaa", "Bbb", "Ccc"));
    test(spec, a("A"),          0, 0, cur, l("Aaa", "Bbb", "Ccc"));
    test(spec, a("A"),          0, 1, cur, l("aa"));
    test(spec, a("Aaa"),        1, 0, cur, l("Aaa", "Bbb", "Ccc"));
    test(spec, a("Aaa", "Bbb"), 2, 0, cur, l("Aaa", "Bbb", "Ccc"));
}
 
Example 8
Source File: ArgGroupTest.java    From picocli with Apache License 2.0 5 votes vote down vote up
@Test
public void testNonValidatingOptionsAreNotExclusive() {
    CommandSpec spec = CommandSpec.forAnnotatedObject(new Issue810Command());
    assertFalse(spec.argGroups().get(0).exclusive());

    CommandSpec spec2 = CommandSpec.forAnnotatedObject(new Issue810WithExplicitExclusiveGroup());
    assertFalse(spec2.argGroups().get(0).exclusive());
}
 
Example 9
Source File: SubcommandTests.java    From picocli with Apache License 2.0 5 votes vote down vote up
@Test
public void testAddSubcommandWithoutNameUsesInstanceSpecName() {
    CommandLine cmd = new CommandLine(new MainCommand());
    Object userObject = new SubcommandWithAnnotationName();
    CommandSpec instanceSpec = CommandSpec.forAnnotatedObject(userObject);
    cmd.addSubcommand(instanceSpec);
    assertTrue(cmd.getSubcommands().containsKey("annotationName"));
    assertSame(userObject, cmd.getSubcommands().get("annotationName").getCommand());
}
 
Example 10
Source File: SubcommandTests.java    From picocli with Apache License 2.0 5 votes vote down vote up
@Test
public void testAddSubcommandWithoutNameUsesClassSpecName() {
    CommandLine cmd = new CommandLine(new MainCommand());
    CommandSpec classSpec = CommandSpec.forAnnotatedObject(SubcommandWithAnnotationName.class);
    cmd.addSubcommand(classSpec);
    assertTrue(cmd.getSubcommands().containsKey("annotationName"));
    Object userObject = cmd.getSubcommands().get("annotationName").getCommand();
    assertTrue(userObject instanceof SubcommandWithAnnotationName);
}
 
Example 11
Source File: ModelCommandReflectionTest.java    From picocli with Apache License 2.0 5 votes vote down vote up
@Test
public void testInitSubcommands() {
    try {
        CommandSpec.forAnnotatedObject(InvalidTop.class);
    } catch (InitializationException ex) {
        assertEquals("Cannot instantiate subcommand picocli.ModelCommandReflectionTest$InvalidSub: the class has no constructor", ex.getMessage());
    }
}
 
Example 12
Source File: ModelCommandReflectionTest.java    From picocli with Apache License 2.0 5 votes vote down vote up
@Test
public void testSubcommandName() {
    try {
        CommandSpec.forAnnotatedObject(InvalidTop2.class);
    } catch (InitializationException ex) {
        assertEquals("Subcommand picocli.ModelCommandReflectionTest$InvalidSub2 is missing the mandatory @Command annotation with a 'name' attribute", ex.getMessage());
    }
}
 
Example 13
Source File: ModelCommandReflectionTest.java    From picocli with Apache License 2.0 5 votes vote down vote up
@Test
public void testBuildMixinForField_invalid() {
    CommandLine.IFactory myFactory = new CommandLine.IFactory() {
        public <K> K create(Class<K> cls) {
            throw new IllegalStateException("boom");
        }
    };

    try {
        CommandSpec.forAnnotatedObject(new MixeeUninstantiated(), myFactory);
    } catch (InitializationException ex) {
        assertEquals("Could not access or modify mixin member picocli.ModelCommandReflectionTest$ValidMixin picocli.ModelCommandReflectionTest$MixeeUninstantiated.mixin: java.lang.IllegalStateException: boom", ex.getMessage());
    }
}
 
Example 14
Source File: ModelCommandReflectionTest.java    From picocli with Apache License 2.0 4 votes vote down vote up
@Test
public void testBuildMixinForField_valid() {
    CommandSpec commandSpec = CommandSpec.forAnnotatedObject(new MixeeInstantiated());
    assertNotNull(commandSpec.findOption("h"));
}
 
Example 15
Source File: ModelCommandReflectionTest.java    From picocli with Apache License 2.0 4 votes vote down vote up
@Test(expected = InitializationException.class)
public void testCommandReflection_buildUnmatchedForField_raw() {
    CommandSpec.forAnnotatedObject(new MyUnmatched());
}
 
Example 16
Source File: ModelCommandReflectionTest.java    From picocli with Apache License 2.0 4 votes vote down vote up
@Test
public void testBuildUnmatchedForField_valid() {
    CommandSpec.forAnnotatedObject(new MyUnmatched2());
}