picocli.4.0.1.source-code.overview.html Maven / Gradle / Ivy
Show all versions of picocli Show documentation
Picocli is a one-file framework for creating Java command line applications with almost zero code.
It supports a variety of command line syntax styles including POSIX, GNU, MS-DOS and more.
Generates highly customizable usage help messages with ANSI colors and styles.
Picocli-based applications can have command line TAB completion
showing available options, option parameters and subcommands, for any level of nested subcommands.
How it works: annotate your class and pass it to the CommandLine
constructor.
Then invoke CommandLine.parseArgs
or CommandLine.execute
with
the command line parameters, and picocli parses the command line arguments and converts them
to strongly typed values, which are then injected in the annotated fields and methods of your class.
Picocli provides an execute method
that allows applications to omit error handling and other boilerplate code for common use cases.
Here is a small example application that uses the CommandLine.execute
method
to do parsing and error handling in one line of code.
The full user manual is hosted at http://picocli.info.
@Command(name = "checksum", mixinStandardHelpOptions = true, version = "Checksum 4.0",
description = "Prints the checksum (MD5 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {
@Parameters(index = "0", description = "The file whose checksum to calculate.")
private File file;
@Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
private String algorithm = "MD5";
// CheckSum implements Callable, so parsing, error handling and handling user
// requests for usage help or version help can be done with one line of code.
public static void main(String[] args) throws Exception {
int exitCode = new CommandLine(new CheckSum()).execute(args);
System.exit(exitCode);
}
@Override
public Integer call() throws Exception { // your business logic goes here
byte[] fileContents = Files.readAllBytes(file.toPath());
byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
return 0;
}
}