解析多个 picocli(子)命令和一个 shell
Parsing multiple picocli (sub-)commands and a shell
如何构建 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
,三个命令(list
、create
、delete
)各有不同的选项和参数。
Spring 引导应用程序很简单:
@SpringBootApplication
public class Application {
public static void main(String[] args) {
System.exit(SpringApplication.exit(
SpringApplication.run(Application.class, args))
);
}
}
并且 Spring 使用 return 值启动 CommandLineRunner
也很简单,调用 picocli 的 CommandLine
解析和执行命令:
@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
有 showAtFileInUsageHelp = true
启用 picocli 的 @-file 支持和 mixinStandardHelpOptions
启用帮助和版本信息 "standard" 选项(-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...
并且 运行 包含 'list' 的@文件也可以工作:
java -jar manager.jar -u=myname -p=mypass @listing
listing...
这是 sample repository。现在我们需要折叠 shell...
如何构建 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
,三个命令(list
、create
、delete
)各有不同的选项和参数。
Spring 引导应用程序很简单:
@SpringBootApplication
public class Application {
public static void main(String[] args) {
System.exit(SpringApplication.exit(
SpringApplication.run(Application.class, args))
);
}
}
并且 Spring 使用 return 值启动 CommandLineRunner
也很简单,调用 picocli 的 CommandLine
解析和执行命令:
@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
有 showAtFileInUsageHelp = true
启用 picocli 的 @-file 支持和 mixinStandardHelpOptions
启用帮助和版本信息 "standard" 选项(-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...
并且 运行 包含 'list' 的@文件也可以工作:
java -jar manager.jar -u=myname -p=mypass @listing
listing...
这是 sample repository。现在我们需要折叠 shell...