Picocli提供了在@Command注释中添加一个很好的标题的能力,例如:
@Command(name = "git-star", header = {
"@|green _ _ _ |@",
"@|green __ _(_) |_ __| |_ __ _ _ _ |@",
"@|green / _` | | _(_-< _/ _` | '_| |@",
"@|green \\__, |_|\\__/__/\\__\\__,_|_| |@",
"@|green |___/ |@"},
description = "Shows GitHub stars for a project",
mixinStandardHelpOptions = true, version = "git-star 0.1")如何在程序运行时始终显示标题/横幅,而不重复两个位置的标题/横幅?
发布于 2018-10-22 11:25:33
这方面有两个方面:
您可以使用new CommandLine(new App()).getCommandSpec().usageHelpMessage().header()或在应用程序中注入一个带有@Spec注释的CommandSpec字段,从而从使用帮助消息中获得横幅。
若要呈现ANSI样式,请对每行横幅使用CommandLine.Help.Ansi.AUTO.string(line)。
把这一切结合在一起:
@Command(name = "git-star", header = {
"@|green _ _ _ |@",
"@|green __ _(_) |_ __| |_ __ _ _ _ |@",
"@|green / _` | | _(_-< _/ _` | '_| |@",
"@|green \\__, |_|\\__/__/\\__\\__,_|_| |@",
"@|green |___/ |@"},
description = "Shows GitHub stars for a project",
mixinStandardHelpOptions = true, version = "git-star 0.1")
class GitStar implements Runnable {
@Option(names = "-c")
int count;
@Spec CommandSpec spec;
// prints banner every time the command is invoked
public void run() {
String[] banner = spec.usageHelpMessage().header();
// or: String[] banner = new CommandLine(new GitStar())
// .getCommandSpec().usageHelpMessage().header();
for (String line : banner) {
System.out.println(CommandLine.Help.Ansi.AUTO.string(line));
}
// business logic here...
}
public static void main(String[] args) {
CommandLine.run(new GitStar(), args);
}
}发布于 2021-01-09 23:07:28
在Picocli 4.5.2中,我的作品是这样的:
public void run() {
CommandLine cmd = new CommandLine(new App());
cmd.usage(System.out, Ansi.ON);
// business logic here...
}https://stackoverflow.com/questions/52928235
复制相似问题