Git Product home page Git Product logo

xuchunh / picocli Goto Github PK

View Code? Open in Web Editor NEW

This project forked from remkop/picocli

0.0 1.0 0.0 18.93 MB

Parser library and framework to easily build JVM command line applications (Java, Groovy, Kotlin, etc). Usage help with ANSI colors. Autocomplete. Nested subcommands. Annotations and programmatic API. Easy to include as source to avoid adding dependencies.

License: Apache License 2.0

Java 97.57% Groovy 1.56% Kotlin 0.08% Shell 0.71% HTML 0.08%

picocli's Introduction

picocli

GitHub Release Build Status codecov Follow @remkopopma Follow @picocli Follow picocli on StackShare

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. Command-line argument files (@-files) allow applications to handle very long command lines.

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 picocli-shell-jline2 and picocli-shell-jline3 modules with JLine Completer implementations for building interactive shell applications.

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

Picocli Demo help message with ANSI colors

Releases

Documentation

Older

Articles & Presentations

English

русский

Français

日本語

中文

Mailing List

Join the picocli Google group if you are interested in discussing anything picocli-related and receiving announcements on new releases.

Related

Credit

Reallinfo designed the new picocli logo! Many thanks!

Commitments

This project follows semantic versioning and adheres to the Zero Bugs Commitment.

Adoption

  • 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.
  • Debian now offers a libpicocli-java package. Thanks to Miroslav Kravec.
  • Picocli is used in the Intuit Karate standalone JAR / executable.
  • Picocli is part of Ballerina. Ballerina uses picocli for all its command line utilities.
  • Picocli is used in the CheckStyle standalone JAR / executable from Checkstyle 8.15.
  • Picocli is included in the OpenJDK Quality Outreach list of Free Open Source Software (FOSS) projects that actively test against OpenJDK builds.
  • Picocli is used in the Apache Hadoop Ozone/HDDS command line tools.
  • Picocli is used in the Apache Hive benchmark CLI.
  • Picocli is listed on StackShare. Please add it to your stack and add/upvote reasons why you like picocli!

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

Help to promote picocli

If you like picocli and your project is on GitHub, consider adding this badge to your README.md: picocli

[![picocli](https://img.shields.io/badge/picocli-4.0.1-green.svg)](https://github.com/remkop/picocli)

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 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);
    }
}

If your command implements Runnable, all 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.

Usage help message with ANSI colors

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:

Longer help message with ANSI colors

See the source code.

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:

Usage help message with two options per row

See the source code.

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:4.0.1'

Maven

<dependency>
  <groupId>info.picocli</groupId>
  <artifactId>picocli</artifactId>
  <version>4.0.1</version>
</dependency>

Scala SBT

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

Ivy

<dependency org="info.picocli" name="picocli" rev="4.0.1" />

Grape

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

Leiningen

[info.picocli/picocli "4.0.1"]

Buildr

'info.picocli:picocli:jar:4.0.1'

picocli's People

Contributors

remkop avatar illes avatar triceo avatar reallinfo avatar sualeh avatar bbottema avatar nicolasmassart avatar mattirn avatar dwalluck avatar querqueq avatar stengerh avatar knutwalker avatar rpanak avatar simon04 avatar deining avatar warkdev avatar topalavlad avatar acid1103 avatar snicoll avatar charphi avatar marinier avatar sergioescala avatar kakawait avatar stefre avatar sjsajj avatar robertzenz avatar raducotescu avatar pditommaso avatar quinn-harper avatar mkobit avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.