Picocli:如何始终显示 header/banner
Picocli: how to show header/banner at all times
Picocli 提供了在 @Command
注释中添加漂亮的 header 的功能,例如:
@Command(name = "git-star", header = {
"@|green _ _ _ |@",
"@|green __ _(_) |_ __| |_ __ _ _ _ |@",
"@|green / _` | | _(_-< _/ _` | '_| |@",
"@|green \__, |_|\__/__/\__\__,_|_| |@",
"@|green |___/ |@"},
description = "Shows GitHub stars for a project",
mixinStandardHelpOptions = true, version = "git-star 0.1")
如何在程序为 运行 时始终显示 header/banner,而不在两个地方复制此横幅?
这有两个方面:
- 如何从应用程序中获取横幅文本?
- 如何呈现 ANSI 颜色和样式?
您可以通过 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);
}
}
对我来说,在 Picocli 4.5.2 中,工作方式如下:
public void run() {
CommandLine cmd = new CommandLine(new App());
cmd.usage(System.out, Ansi.ON);
// business logic here...
}
Picocli 提供了在 @Command
注释中添加漂亮的 header 的功能,例如:
@Command(name = "git-star", header = {
"@|green _ _ _ |@",
"@|green __ _(_) |_ __| |_ __ _ _ _ |@",
"@|green / _` | | _(_-< _/ _` | '_| |@",
"@|green \__, |_|\__/__/\__\__,_|_| |@",
"@|green |___/ |@"},
description = "Shows GitHub stars for a project",
mixinStandardHelpOptions = true, version = "git-star 0.1")
如何在程序为 运行 时始终显示 header/banner,而不在两个地方复制此横幅?
这有两个方面:
- 如何从应用程序中获取横幅文本?
- 如何呈现 ANSI 颜色和样式?
您可以通过 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);
}
}
对我来说,在 Picocli 4.5.2 中,工作方式如下:
public void run() {
CommandLine cmd = new CommandLine(new App());
cmd.usage(System.out, Ansi.ON);
// business logic here...
}