command-line - Picocli:如何始终显示标题/横幅
问题描述
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")
如何在程序运行时始终显示该标题/横幅,而不在两个地方复制此横幅?
解决方案
这有两个方面:
- 如何从应用程序中获取横幅文本?
- 如何渲染 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);
}
}
推荐阅读
- c++ - 加快 C++ 代码性能(map、find()、输入和输出)
- php - 如何在奏鸣曲管理员中启用特定实体的删除和编辑
- redux - 如何在单独导入的函数上使用 jest.fn()
- qt - Qt、Visual Studio 2017 和 .vcxproj.user 文件
- javascript - 如何将 PWA 重定向到新服务器
- apache-spark - 在 Scala Spark 中将多列作为 Seq/Array 传递给 UDF
- bash - 我的 bash 脚本是否足够准确以检查图像列表是否在目录中的任何位置被引用?
- r - 从列表中子集逗号分隔的字符串
- django - 如何从 DJANGO REST FRAMEWOK 中的序列化模型创建 excel 以供使用(下载)
- rest - 使用 databricks Resr API 运行 spark 作业