Git Product home page Git Product logo

gimme-a-cli-starter-project's Introduction

Java CI with Gradle CodeQL License

Gimme a CLI Starter Project

This project is an example starter project for the Gimme a CLI library.

This project can be forked and used as a starting point for creating your own CLI.

Features

The starter project includes:

  • Gimme a CLI - a Java library for creating quick and easy CLIs using JCommander and Spring dependency injection.
  • An example Hello World command.
  • An example --version command.
  • Shadow Plugin gradle setup to make it easy to distribute your CLI. The Shadow plugin creates a fat jar with all dependencies, sh and .bat wrapper scripts, and .tar and .zip distributions.

Usage

  1. Install the Java 11 OpenJDK.

  2. Fork this project.

  3. Find/replace mycli with the name of your CLI.

  4. Define one or more commands for your CLI by implementing the Command interface. Optionally use JCommander annotations to define command arguments and options.

    import com.beust.jcommander.Parameter;
    import com.beust.jcommander.Parameters;
    import com.nike.gimme.a.cli.Command;
    
    @Parameters(commandNames = "hello",
                commandDescription = "Prints \"Hello <name>\" to the terminal")
    public class HelloWorldCommand implements Command {
    
        private final Logger log = LoggerFactory.getLogger(getClass());
    
        @Parameter(names = {"--name"}, required = true)
        private String name;
        
        @Override
        public void execute() {
            log.info("Hello " + name);
        }
    }

    Commands are automatically instantiated as Spring beans (e.g. dependencies can be supplied via constructor injection, etc).

  5. Build and run

    # Create the distribution
    ./gradlew shadowDistTar
    
    # Extract it
    tar xf build/distributions/mycli-shadow-1.0.0-SNAPSHOT.tar
    
    # Run the CLI
    mycli-shadow-1.0.0-SNAPSHOT/bin/mycli --help
  6. Optionally define gradle tasks for running CLI commands

    task helloWorldFromGradle(type: CliExec) {
        description = "Example of defining a CLI command as a Gradle task"
    
        // arguments to pass to the application
        args 'hello', '--name', 'world from gradle'
    }

    This lets you quickly run commands locally without building archives.

References

  • Gimme a CLI - the library that this project is a "starter project" for.
  • JCommander - a library for CLI argument parsing.
  • Spring's IoC - a library for dependency injection.
  • Shadow Plugin - a gradle plugin for creating a single output jar. Makes it easy to distribute your CLI to share with others.

gimme-a-cli-starter-project's People

Contributors

tlisonbee avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  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.