2

如何构建一个Spring Boot 2.3多命令 CLI 应用程序,该应用程序可以使用单个命令、@script 并在picocli中以交互方式运行?它的行为应该是这样的:

manager -u <user> -p <pass> [list|create|delete] # run and exit
manager -u <user> -p <pass> @script              # run and exit
manager -u <user> -p <pass>                      # run shell

用户名-u和密码-p是必需的,三个命令(listcreatedelete)各有不同的选项和参数。

4

1 回答 1

5

Spring Boot 应用程序很简单:

@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        System.exit(SpringApplication.exit(
            SpringApplication.run(Application.class, args))
        );
    }

}

CommandLineRunner并且带有返回值的 Spring Boot也很简单,调用picocliCommandLine解析和执行命令:

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

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

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

}

启用picocli的 @-file 支持并使用“标准”选项( ,等)启用帮助和版本信息的ConnectCommandhas :showAtFileInUsageHelp = truemixinStandardHelpOptions-h--help

@Command(
    name = "manager",
    description = "The manager description",
    showAtFileInUsageHelp = true,
    mixinStandardHelpOptions = true,
    subcommands = {
        ListCommand.class,
        CreateCommand.class,
        DeleteCommand.class
    })
@Component 
public class ConnectCommand implements Runnable, ExitCodeGenerator {
    @Option(
        names        = {"-u", "--username"},
        description  = "The username")
    private String username;

    @Option(
        names        = {"-p", "--password"},
        description  = "The password")
    private String password;

    private int exitCode;

    @Override
    public void run() {
        // WIP: kick-off shell
    }

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

}

并且(子)命令都采用这种形式(撒在picocli@Option@Parameters根据需要):

@Command(
    name = "list",
    mixinStandardHelpOptions = true,
    header = "list stuff")
@Component
class ListCommand implements Runnable{
    @Override
    public void run() {
        System.out.println("listing...");
    }

}

有了这个,帮助现在看起来像:

Usage: manager [-hV] [-u=username] [-p=password] [@<filename>...] [COMMAND]
The manager description
      [@<filename>...]    One or more argument files containing options.
  -u, --username=name     The username
  -p, --password=pass     The password
  -h, --help              Show this help message and exit.
  -V, --version           Print version information and exit.
Commands:
  list    list stuff
  create  create stuff
  delete  delete stuff

运行单个命令即可:

java -jar manager.jar -u=myname -p=mypass list
listing...

并且运行包含“列表”的@文件也可以:

java -jar manager.jar -u=myname -p=mypass @listing
listing...

这是一个示例存储库。现在我们需要折叠外壳......

于 2020-06-12T18:11:21.243 回答