picocli 一个强大的Java编写的微型命令行界面

Armani1年前 ⋅ 2556 阅读

https://github.com/remkop/picocli#example 是一个构建 Java 命令行应用的框架。简单易用寥寥几行代码,就可以完成一个 Java 命令行应用程序。虽然由 Java 编写但可以在 Groovy、Kotlin、Scala 中使用,支持命令自动补全、颜色、子命令、帮助信息等功能。

import picocli.CommandLine;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;
import java.io.File;

@Command(name = "example", mixinStandardHelpOptions = true, version = "Picocli example 4.0")
public class Example implements Runnable {

    @Option(names = { "-v", "--verbose" },
      description = "Verbose mode. Helpful for troubleshooting. Multiple -v options increase the verbosity.")
    private boolean[] verbose = new boolean[0];

    @Parameters(arity = "1..*", paramLabel = "FILE", description = "File(s) to process.")
    private File[] inputFiles;

    public void run() {
        if (verbose.length > 0) {
            System.out.println(inputFiles.length + " files to process...");
        }
        if (verbose.length > 1) {
            for (File f : inputFiles) {
                System.out.println(f.getAbsolutePath());
            }
        }
    }

    public static void main(String[] args) {
        // By implementing Runnable or Callable, parsing, error handling and handling user
        // requests for usage help or version help can be done with one line of code.

        int exitCode = new CommandLine(new Example()).execute(args);
        System.exit(exitCode);
    }
}

https://github.com/remkop/picocli#example
https://picocli.info/#_example_application

不知道和commons-cli有什么区别? @Princess 功能更多命令行界面更绚 @Princess 传统 vs 先进

全部评论: 0

    相关推荐