添加 maven 依赖

当你已经创建好 springboot 应用后,将 picocli 的依赖加入到 pom.xml 中

		<dependency>
			<groupId>info.picocli</groupId>
			<artifactId>picocli-spring-boot-starter</artifactId>
			<version>4.6.3</version>
		</dependency>

修改 springboot 主程序

将你的 springboot 主程序修改为实现了ApplicationRunner接口,并将主程序命令定义为helper

package top.yjp.testing;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import picocli.CommandLine;
import top.yjp.testing.helper.cmd.DemoCommand;

import javax.annotation.Resource;

@SpringBootApplication(proxyBeanMethods = false)
@Slf4j
@CommandLine.Command(
        name = "helper",
        description = "自动化测试帮助程序 ",
        mixinStandardHelpOptions = true,
        version = "1.0.1",
        subcommands = {DemoCommand.class}
)
public class HelperApplication implements ApplicationRunner {
    @Resource
    private CommandLine.IFactory factory;

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

    @Override
    public void run(ApplicationArguments args) throws Exception {
        CommandLine commandLine = new CommandLine(this, factory);
        commandLine.setCaseInsensitiveEnumValuesAllowed(true);
        commandLine.execute(args.getSourceArgs());
    }
}

新增子命令接口程序

该类作为helper命令的子命令demo出现,并带有--jmx-dir--target-file选项

package top.yjp.testing.helper.cmd;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import picocli.CommandLine;

import java.util.concurrent.Callable;
@Slf4j
@Component
@CommandLine.Command(name = "demo", description = "子命令演示程序.", mixinStandardHelpOptions = true)
public class DemoCommand implements Callable<Integer> {
    @CommandLine.Option(description = { "指定jmx文件所在的文件夹,包括子文件夹内的jmx文件" }, names = { "-f","--jmx-dir" }, arity = "1", required = true)
    private String cmdJmxDir = null;
    @CommandLine.Option(description = { "要存储的文件名" }, names = { "-t","--target-file" }, arity = "1",required = true)
    private String targetSqlFile = null;
    @Override
    public Integer call() throws Exception {
        log.info("Demo here! jmxDir={}, targetSqlFile={}",cmdJmxDir,targetSqlFile);
        return 0;
    }
}

打包执行

java -jar demo.jar
# 输出:
Missing required subcommand
Usage: helper [-hV] [COMMAND]
自动化测试帮助程序
  -h, --help      Show this help message and exit.
  -V, --version   Print version information and exit.
Commands:
  demo  子命令演示程序.


#
java -jar demo.jar demo --help
输出:
Usage: helper demo [-hV] -f=<cmdJmxDir> -t=<targetSqlFile>
子命令演示程序.
  -f, --jmx-dir=<cmdJmxDir>
                  指定jmx文件所在的文件夹,包括子文件夹内的jmx文件
  -h, --help      Show this help message and exit.
  -t, --target-file=<targetSqlFile>
                  要存储的文件名
  -V, --version   Print version information and exit.

#
java -jar demo.jar demo --jmx-dir=/tmp --target-file=demo.sql
输出:
Demo here! jmxDir=/tmp, targetSqlFile=demo.sql