java 分析 cli_picocli - 基于注释的Java命令行解析库

625332134c6f4d4600884b99daebf603.png

625332134c6f4d4600884b99daebf603.png625332134c6f4d4600884b99daebf603.png625332134c6f4d4600884b99daebf603.png625332134c6f4d4600884b99daebf603.png625332134c6f4d4600884b99daebf603.png

picocli - a mighty tiny command line interface

Java command line parser with both an annotations API and a programmatic API, featuring usage help with ANSI colors, autocomplete and nested subcommands. In a single file, so you can include it in source form. This lets users run picocli-based applications without requiring picocli as an external dependency.

How it works: annotate your class and picocli initializes it from the command line arguments, converting the input to strongly typed data. Supports git-like subcommands (and nested sub-subcommands), any option prefix style, POSIX-style grouped short options, custom type converters, password options and more. Parser tracing facilitates troubleshooting.

Distinguishes between named options and positional parameters and allows both to be strongly typed. Multi-valued fields can specify an exact number of parameters or a range (e.g., 0..*, 1..2). Supports Map options like -Dkey1=val1 -Dkey2=val2, where both key and value can be strongly typed.

Generates polished and easily tailored usage help and version help, using ANSI colors where possible. Works with Java 5 or higher (but is designed to facilitate the use of Java 8 lambdas).

Picocli-based command line applications can have TAB autocompletion, interactively showing users what options and subcommands are available. When an option has completionCandidates or has an enum type, autocompletion can also suggest option values. Picocli can generate completion scripts for bash and zsh, and offers an API to easily create a JLine Completer for your application.

Picocli-based applications can easily integrate with Dependency Injection containers.

625332134c6f4d4600884b99daebf603.png

Releases

Documentation

Articles

Related

Check out Thibaud Lepretre's picocli Spring boot starter!

Credit

625332134c6f4d4600884b99daebf603.png

Reallinfo designed the new picocli logo! Many thanks!

Adoption

625332134c6f4d4600884b99daebf603.png625332134c6f4d4600884b99daebf603.png625332134c6f4d4600884b99daebf603.png625332134c6f4d4600884b99daebf603.png625332134c6f4d4600884b99daebf603.png

Picocli is now part of Groovy. From Groovy 2.5, all Groovy command line tools are picocli-based, and picocli is the underlying parser for Groovy's CliBuilder DSL.

Picocli is now part of Micronaut. The Micronaut CLI has been rewritten with picocli, and Micronaut has dedicated support for running microservices standalone with picocli.

Picocli is now part of JUnit 5. JUnit 5.3 migrated its ConsoleLauncher from jopt-simple to picocli to support @-files (argument files); this helps users who need to specify many tests on the command line and run into system limitations.

Picocli is used in the Intuit Karate standalone JAR / executable.

625332134c6f4d4600884b99daebf603.png

Glad to see more people are using picocli. We must be doing something right. :-)

Example

Annotate fields with the command line parameter names and description. Optionally implement Runnable or Callable to delegate error handling and requests for usage help or version help to picocli. For example:

import picocli.CommandLine;

import picocli.CommandLine.Option;

import picocli.CommandLine.Parameters;

import java.io.File;

@Command(name = "example", mixinStandardHelpOptions = true, version = "Picocli example 3.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) {

CommandLine.run(new Example(), System.out, args);

}

}

If your command implements Runnable, all the code that is necessary to parse the command line and execute the command is a call to CommandLine.run with the command line parameters and the Runnable command. When the program is run on the command line, the command line arguments are converted to Java objects and assigned to the annotated fields. After the arguments are successfully parsed, picocli calls the command's run method.

$ java Example -v inputFile1 inputFile2

2 files to process...

The CommandLine.run convenience method automatically prints the usage help message if the user requested help or when the input was invalid.

625332134c6f4d4600884b99daebf603.png

If you want more control, you may be interested in the CommandLine.parse or CommandLine.parseWithHandlers methods. See the user manual for details.

Usage Help with ANSI Colors and Styles

Colors, styles, headers, footers and section headings are easily customized with annotations. For example:

625332134c6f4d4600884b99daebf603.png

Usage Help API

Picocli annotations offer many ways to customize the usage help message.

If annotations are not sufficient, you can use picocli's Help API to customize even further. For example, your application can generate help like this with a custom layout:

625332134c6f4d4600884b99daebf603.png

Download

You can add picocli as an external dependency to your project, or you can include it as source. See the source code. Copy and paste it into a file called CommandLine.java, add it to your project, and enjoy!

Gradle

compile 'info.picocli:picocli:3.6.1'

Maven

info.picocli

picocli

3.6.1

Scala SBT

libraryDependencies += "info.picocli" % "picocli" % "3.6.1"

Ivy

Grape

@Grapes(

@Grab(group='info.picocli', module='picocli', version='3.6.1')

)

Leiningen

[info.picocli/picocli "3.6.1"]

Buildr

'info.picocli:picocli:jar:3.6.1'

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值