Skip to content
/ picocli Public

Picocli is a modern framework for building powerful, user-friendly, GraalVM-enabled command line apps with ease. It supports colors, autocompletion, subcommands, and more. In 1 source file so apps can include as source & avoid adding a dependency. Written in Java, usable from Groovy, Kotlin, Scala, etc.

License

Notifications You must be signed in to change notification settings

remkop/picocli

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

picocli - a mighty tiny Command Line Interpreter

A Java command line parsing framework 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 sub-commands, any option prefix style, POSIX-style short groupable options, custom type converters and more.

Distinguishes between named options and positional parameters and allows both to be strongly typed. Multi-valued fields can specify either an open or a precise range of parameters (e.g., 0..*, 1..2).

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

See the full user manual here: http://picocli.info

Example

Annotate fields with the command line parameter names and description.

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

public class Example {
    @Option(names = { "-v", "--verbose" }, description = "Be verbose.")
    private boolean verbose = false;

    @Option(names = { "-h", "--help" }, help = true,
            description = "Displays this help message and quits.")
    private boolean helpRequested = false;

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

Then invoke CommandLine.parse with the command line parameters and an object you want to initialize.

String[] args = { "-v", "inputFile1", "inputFile2" };
Example app = CommandLine.parse(new Example(), args);

assert !app.helpRequested;
assert  app.verbose;
assert  app.inputFiles != null && app.inputFiles.length == 2;

Usage Help

If the user requested help or if invalid input resulted in a ParameterException, picocli can generate a usage help message. For example:

CommandLine.usage(new Example(), System.out);

The generated help message looks like this (colors only rendered when ANSI codes are enabled):

Usage help message with ANSI colors

Customized Usage Help

Usage help is highly customizable. A more elaborate usage help example is shown below:

Longer help message with ANSI colors

See the source code.

More Customized Usage Help

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:

Usage help message with two options per row

See the source code.

About

Picocli is a modern framework for building powerful, user-friendly, GraalVM-enabled command line apps with ease. It supports colors, autocompletion, subcommands, and more. In 1 source file so apps can include as source & avoid adding a dependency. Written in Java, usable from Groovy, Kotlin, Scala, etc.

Topics

Resources

License

Security policy

Stars

Watchers

Forks

Packages

No packages published