Command line Picocli:如何始终显示标题/横幅
Picocli能够在Command line Picocli:如何始终显示标题/横幅,command-line,command-line-interface,picocli,Command Line,Command Line Interface,Picocli,Picocli能够在@命令注释中添加漂亮的标题,例如: @Command(name = "git-star", header = { "@|green _ _ _ |@", "@|green __ _(_) |_ __| |_ __ _ _ _ |@", "@|green / _` | | _(_-< _/ _` | '_| |@", "@|green \\__, |_|\\__/__/\\__\\__
@命令
注释中添加漂亮的标题,例如:
@Command(name = "git-star", header = {
"@|green _ _ _ |@",
"@|green __ _(_) |_ __| |_ __ _ _ _ |@",
"@|green / _` | | _(_-< _/ _` | '_| |@",
"@|green \\__, |_|\\__/__/\\__\\__,_|_| |@",
"@|green |___/ |@"},
description = "Shows GitHub stars for a project",
mixinStandardHelpOptions = true, version = "git-star 0.1")
@命令(name=“git star”,标题={
“绿色”,
“@|绿色|(|)| | | | | | | | | |”,
“@|绿色/| | |(-| |'124; |@”,
“@|绿色\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\,
“@|绿色| | |/|@”,
description=“为项目显示GitHub星星”,
mixInstandardHelpions=true,version=“git star 0.1”)
在程序运行时,如何始终显示标题/横幅,而不在两个位置复制此横幅
(另请参见)这有两个方面:
- 如何从应用程序中获取横幅文本
- 如何渲染ANSI颜色和样式
new CommandLine(new App()).getCommandSpec().usageHelpMessage().header()
或通过在应用程序中插入带注释的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);
}
}
@命令(name=“git star”,标题={
“绿色”,
“@|绿色|(|)| | | | | | | | | |”,
“@|绿色/| | |(-| |'124; |@”,
“@|绿色\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\,
“@|绿色| | |/|@”,
description=“为项目显示GitHub星星”,
mixInstandardHelpions=true,version=“git star 0.1”)
类GitStar实现了Runnable{
@选项(名称=“-c”)
整数计数;
@规范命令规范;
//每次调用命令时打印横幅
公开募捐{
String[]banner=spec.usageHelpMessage().header();
//或者:String[]banner=new命令行(new GitStar())
//.getCommandSpec().usageHelpMessage().header();
用于(字符串行:横幅){
System.out.println(CommandLine.Help.Ansi.AUTO.string(line));
}
//这里的业务逻辑。。。
}
公共静态void main(字符串[]args){
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...
}
无法使用3.9.5编译此文件,应该是
this.spec.usageMessage().header()
?这也可以,并打印完整的用法帮助消息。如果这是适合应用程序的,当然可以。最初的问题来自应用程序只想打印横幅的情况(没有选项说明),然后是一些其他输出。