Creating Java Interfaces with Kotlin


#1

Hi,

I’m trying to use Kotlin with JewelCLI which requires that I be able to:

  • Create a Java interface
  • Annotate that interface
  • Reference the interface class (e.g. MyOptions.class)

e.g.:

public interface MyExample
{
  
@Option
  
String getMyOption();

  @Option(helpRequest = true)
  
boolean getHelp();
}



public static void main(String [] args)
{
  
try
  
{
  
MyExample result = CliFactory.parseArguments(MyExample.class, args);
  
[…]
  
}
  
catch(ArgumentValidationException e)
  
{
  
[…]
  
}
}

Note: there are a few alternatives to this e.g.:

  • Use the 'instance' alternative with JewelCLI
  • Use a different CLI parser

But I'm curious if this is something that I can do in Kotlin, and if so, how?


#2

I think this should work:

public trait MyExample {
  Option fun getMyOption(): String
  ...
}

ClipFactory.parseArgument(javaClass<MyExample>(), args)


#3

That worked. Thanks!