Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Command line Picocli:如何始终显示标题/横幅_Command Line_Command Line Interface_Picocli - Fatal编程技术网

Command line 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 \\__, |_|\\__/__/\\__\\__

Picocli能够在
@命令
注释中添加漂亮的标题,例如:

@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”,标题={
“绿色”,
“@|绿色|(|)| | | | | | | | | |”,
“@|绿色/| | |(-
在程序运行时,如何始终显示标题/横幅,而不在两个位置复制此横幅


(另请参见)

这有两个方面:

  • 如何从应用程序中获取横幅文本
  • 如何渲染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”,标题={
“绿色”,
“@|绿色|(|)| | | | | | | | | |”,
“@|绿色/| | |(-
对于我来说,在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()
?这也可以,并打印完整的用法帮助消息。如果这是适合应用程序的,当然可以。最初的问题来自应用程序只想打印横幅的情况(没有选项说明),然后是一些其他输出。