【问题标题】:Parsing multiple picocli (sub-)commands and a shell解析多个 picocli(子)命令和 shell
【发布时间】:2020-06-11 22:29:00
【问题描述】:

如何构建一个 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)各有不同的选项和参数。

【问题讨论】:

    标签: java picocli


    【解决方案1】:

    Spring Boot 应用程序很简单:

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

    而带有返回值的Spring Boot CommandLineRunner也很简单,调用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;
        }
    
    }
    

    ConnectCommand 具有启用 picocli 的 @-file 支持的 showAtFileInUsageHelp = true 和启用带有“标准”选项的帮助和版本信息的mixinStandardHelpOptions-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...
    

    这是sample repository。现在我们需要折叠外壳...

    【讨论】:

    • 你救了我的命 Amigo。
    猜你喜欢
    • 2017-01-31
    • 2011-12-11
    • 2018-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-24
    • 1970-01-01
    相关资源
    最近更新 更多