https://github.com/nike-inc/gimme-a-cli
Gimme a CLI is a Java library for creating quick and easy command line interfaces (CLIs) using JCommander and Spring dependency injection.
https://github.com/nike-inc/gimme-a-cli
gimme-a-cli java jcommander spring-ioc
Last synced: 11 months ago
JSON representation
Gimme a CLI is a Java library for creating quick and easy command line interfaces (CLIs) using JCommander and Spring dependency injection.
- Host: GitHub
- URL: https://github.com/nike-inc/gimme-a-cli
- Owner: Nike-Inc
- License: apache-2.0
- Created: 2019-05-25T01:03:21.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2020-11-18T23:16:11.000Z (over 5 years ago)
- Last Synced: 2025-05-02T13:47:18.428Z (about 1 year ago)
- Topics: gimme-a-cli, java, jcommander, spring-ioc
- Language: Java
- Homepage:
- Size: 97.7 KB
- Stars: 2
- Watchers: 7
- Forks: 7
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- License: LICENSE.txt
- Security: SECURITY.md
Awesome Lists containing this project
README


[](https://opensource.org/licenses/Apache-2.0)
[  ](https://bintray.com/nike/maven/gimme-a-cli/_latestVersion)
# Gimme a CLI
Gimme a CLI is a Java library for creating quick and easy command line interfaces (CLIs) using [JCommander](http://jcommander.org/) and
[Spring's IoC Container](https://docs.spring.io/spring/docs/current/spring-framework-reference/core.html#spring-core).
- JCommander is great for creating command-style CLI argument parsers. Examples of command-style CLIs include git and the AWS CLI.
- Spring's IoC Container provides dependency injection which helps reduce boiler plate and makes it easier to write testable code.
- Gimme a Cli eliminates the tedious setup you would otherwise have to do.
## Getting Started
[Example starter project](https://github.com/Nike-Inc/gimme-a-cli-starter-project) can simply be cloned and modified.
## Usage
1. Define one or more commands for your CLI by implementing the Command interface.
```java
import com.nike.gimme.a.cli.Command;
public class HelloWorld implements Command {
@Override
public void execute() {
System.out.println("Hello World!");
}
}
```
Commands are automatically instantiated as Spring beans (e.g. dependencies can be supplied via constructor injection, etc).
2. Optionally use JCommander annotations to define command arguments and options.
```java
import com.beust.jcommander.Parameter;
import com.beust.jcommander.Parameters;
import com.nike.gimme.a.cli.Command;
@Parameters(commandNames = "hello-world",
commandDescription = "Prints \"Hello \" to the terminal")
public class HelloWorld implements Command {
@Parameter(names = {"--name"}, required = true)
private String name;
@Override
public void execute() {
System.out.println("Hello " + name);
}
}
```
3. Define a main class and run the GimmeACli program.
```java
import com.nike.gimme.a.cli.Config;
import com.nike.gimme.a.cli.GimmeACli;
public class Main {
public static void main(String[] args) {
new GimmeACli(
Config.builder()
.withCliName("my-cli-name")
.withPackagesToScan("com.nike")
.build()
).run(args);
}
}
```
Spring setup is done for you using classpath scanning.
4. Configure SLF4J logging, e.g.,
1. Include logback as a dependency.
``` groovy
dependencies {
implementation "ch.qos.logback:logback-classic:1.1.11"
}
```
2. Provide a [logback.xml](https://github.com/Nike-Inc/gimme-a-cli-starter-project/blob/master/src/main/resources/logback.xml).
All of these steps have been done for you in the
[example starter project](https://github.com/Nike-Inc/gimme-a-cli-starter-project) which can simply be cloned and
modified to fit your needs..
## Additional Features
- Global `--help` option gives nicely formatted output.
## References
- [JCommander](http://jcommander.org/) - a library for CLI argument parsing.
- [Spring's IoC](https://docs.spring.io/spring/docs/current/spring-framework-reference/core.html#spring-core) - a library for easy dependency injection.
- [gimme-a-cli-starter-project](https://github.com/Nike-Inc/gimme-a-cli-starter-project) - An example starter project that can simply be cloned and modified.