0

我正在 picocli 中编写一个 CLI 应用程序,它将执行用户指定次数的操作,然后在此之后正常退出。但是,当程序退出时,picocli 会打印一个“未知选项”错误,即使程序已经识别出选项已成功运行。

错误输出如下:

    Unknown options: '-j', '~/example_dir/example.json'
    Usage: <main class> [-h] [COMMAND]
        -h, --help   Prints this help message and exits
    Commands:
        example_command  does things

Process finished with exit code 0

任何想法为什么会发生这种情况?

以下是代码结构的概述:

Command Line Runner,创建 Picocli 的 CommandLine 实例

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.ExitCodeGenerator;
import org.springframework.stereotype.Component;
import picocli.CommandLine;

@Component
public class ApplicationRunner implements CommandLineRunner, ExitCodeGenerator {
    private int exitCode;

    @Autowired
    private CommandSample commandSample;

    @Override
    public void run(String... args) throws Exception {
        exitCode = new CommandLine(commandSample).execute(args);
    }

    @Override
    public int getExitCode() {
        return exitCode;
    }

}

春季启动应用程序:

@SpringBootApplication
public class SpringApp {
    public static void main(String[] args) {
        SpringApplication app = new SpringApplication(SpringApp.class);
        app.setBannerMode(Banner.Mode.OFF);
        System.exit(SpringApplication.exit(app.run(args)));
    }
}

CommandSample 类中的选项,即 @Command 正确的

@Command(
        description = "Publish Specified Test JSON to Specified Topics",
        mixinStandardHelpOptions = true,
        version = "beta"
        )
@Controller
public class CommandSample implements Runnable, ExitCodeGenerator{

    @Option(
            names = {"-j", "--json"},
            paramLabel = "<PATH_TO_JSON>",
            description = "Absolute Path to the JSON Payload, supports '~' as an alias for home directory"
    )
    private String path_to_JSON;

    @Option(
            names = {"-t", "--topic"},
            paramLabel = "<MQTT_TOPIC>",
            description = "MQTT topic which you wish to publish to"
    )
    private String mqtt_topic;

    @Option(
            names = {"-n", "--number"},
            paramLabel = "<NUMBER_OF_REQUESTS>",
            description = "Number of times to publish to topic before exiting",
            defaultValue = "5"
    )
    private long number_of_requests = 5;

    @Option(
            names = {"-i", "--interval"},
            paramLabel ="<INTERVAL_BETWEEN_PUBLISH>",
            description = "Interval between individual publishes (in milliseconds)"
    )
    private long sleep_time_ms = 5000L;

@Override
    public void run() {
        // Some MQTT stuff
    }
    int exitcode;
    @Override
    public int getExitCode() {
        return exitcode;
    }

}
4

1 回答 1

1

有趣的是,问题归结为我在加载主要 picocli-4.6.1 依赖项后包含了以下依赖项。其中包括可能已经更新的版本。

我的 pom.xml 中有问题的依赖项:

        <dependency>
            <groupId>com.kakawait</groupId>
            <artifactId>picocli-spring-boot-starter</artifactId>
            <version>0.2.0</version>
        </dependency>
  
于 2021-04-28T15:53:24.893 回答