How can I add Interface's fields as option in picocli?

Viewed 44

I have a problem while refactoring my code that uses picocli

Here's the example

@Command(name='-classA')
class A{
  @Options(names = '-n1')
  public int num1;
  @Options(names = '-n2')
  public int num2;

  public void testA(){
    System.out.println("class A Testing");
    System.out.println(num1+num2);
  }
}
@Command(name='-classB')
class B{
  @Options(names = '-n1')
  public int num1;
  @Options(names = '-n2')
  public int num2;

  public void testB(){
    System.out.println("class B Testing");
    System.out.println(num1+num2);
  }
}

Currently, my cli tool works like

java -jar poo.jar -classA -n1 -n2
java -jar poo.jar -classB -n1 -n2

Although both class A and B works well, while refactoring my code I wanted to use interface to put common variables in one place. Like this

interface MyInterface{
  @Options(names = '-n1')
  public int num1;
  @Options(names = '-n2')
  public int num2;

  public int getNum1(){return num1;}
  public int getNum2(){return num2;}
}

However, when i execute my app with options all included, it is out of order while compiling the options' of interface.

Is is impossible to give options to variables in the interface?

Thank you for reading

0 Answers
Related