2

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

Leo Ha
  • 21
  • 2
  • 1
    In java, fields in interfaces are constants: they are given a value at compile time, and this value cannot be changed at runtime. So annotating fields in an interface will not work because picocli will not be able to assign a value to these fields. Better to use a class instead. – Remko Popma Nov 02 '21 at 09:56

0 Answers0