在使用 cli 之前,我会有一个启动类,它调用我的 ApplicationPropertiesProvider 类(它读取我的属性文件),然后启动业务逻辑。所以有一个分离,ApplicationPropertiesProvider 只有一个工作。
现在使用 picocli,指南/文档指出我必须使用 CommandLine.run(objectToPopulate, args) 或 CommandLine.call(objectToPopulate, args)。因此,使用 cli 参数 (ApplicationPropertiesProvider) 填充的类必须实现 Runnable 或 Callable。现在我可以将 Starter 类的启动代码粘贴到 run() 或 call() 方法中,然后放弃 Starter 类。但我不喜欢那样,我想将一个只包含属性的类和我的 Starter 类分开。
我想到并在下面的示例中显示的一种肮脏的解决方法是将参数从 main 方法传递给我的 Starter 类的构造函数,使用 CommandLine.run() 填充 ApplicationPropertiesProvider 但只实现一个空的 run() 或 call( ) 方法,因此它将立即返回到我的 Starter 类,然后我在那里启动业务逻辑。那将是我要求的结果(分离),但这样看起来真的很愚蠢。
还有一个刚刚出现的问题:如果我有多个包含业务代码的类以及它们自己的属性(而不是单个属性提供类)的标准情况:是否可以使用一个 cli 调用填充多个不同的类,即调用“test.jar command --a --b”,其中参数“a”直接指向“X”类的实例,“b”指向“Y”类的实例?
public class Starter {
public static void main(String[] args) {
new Starter(args);
}
public Starter(String[] args) {
app = ApplicationPropertiesProvider.getInstance();
CommandLine.run(app, args);
//then kick off the business logic …Run Code Online (Sandbox Code Playgroud) picocli ×1