Spring Shell - 在执行ShellMethod的过程中捕获用户输入

问题描述 投票:1回答:2

是在执行@ShellMethod的过程中捕获用户输入的一种方法。基本上停止执行该方法以请求用户输入并在捕获之后继续进行。

spring-shell
2个回答
2
投票

这里有可能的解决方案:https://stackoverflow.com/a/50954716撰写的ZachOfAllTrades

它仅在您的应用程序基于SpringBoot时才有效,因此您可以访问由SpringBoot配置的LineReader对象。

@Autowired
LineReader reader;

public String ask(String question) {
    return this.reader.readLine("\n" + question + " > ");
}

@ShellMethod(key = { "setService", "select" }, value = "Choose a Speech to Text Service")
public void setService() {
    boolean success = false;
    do {
        String question = "Please select a service.";

        // Get Input
        String input = this.ask(question);

        // Input handling
        /*
         * do something with input variable
         */
        success = true;

        }
    } while (!success);
}

不过,我自己也没试过。


0
投票

你应该能够直接与System.in交互,虽然它不是Spring Shell的真正含义:命令应该是自包含的。

© www.soinside.com 2019 - 2024. All rights reserved.