代码之家  ›  专栏  ›  技术社区  ›  Remko Popma

Picocli:如何始终显示标题/横幅

  •  0
  • Remko Popma  · 技术社区  · 6 年前

    @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")
    

    如何在程序运行时始终显示标题/横幅,而不在两个位置复制此横幅?

    (另见 https://github.com/remkop/picocli/issues/517

    1 回复  |  直到 6 年前
        1
  •  2
  •   Remko Popma    4 年前

    • 如何从应用程序中获取横幅文本?

    您可以从用法帮助消息中获取横幅,也可以使用 new CommandLine(new App()).getCommandSpec().usageHelpMessage().header() @Spec 注释 CommandSpec 应用程序中的字段。

    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);
      }
    }
    
        2
  •  0
  •   caiosduarte    4 年前

    public void run() {
        CommandLine cmd = new CommandLine(new App());
        cmd.usage(System.out, Ansi.ON);
    
        // business logic here...
    }