JCommander-Spring-Boot-Starter

JCommander integration with SpringBoot

License

License

Categories

Categories

Spring Boot Container Microservices JCommander User Interface CLI
GroupId

GroupId

com.etolmach.spring
ArtifactId

ArtifactId

jcommander-spring-boot-starter
Last Version

Last Version

1.1
Release Date

Release Date

Type

Type

jar
Description

Description

JCommander-Spring-Boot-Starter
JCommander integration with SpringBoot
Project URL

Project URL

http://jcommander.spring.etolmach.com/
Source Code Management

Source Code Management

http://github.com/etolmach/jcommander-spring-boot-starter/tree/master

Download jcommander-spring-boot-starter

How to add to project

<!-- https://jarcasting.com/artifacts/com.etolmach.spring/jcommander-spring-boot-starter/ -->
<dependency>
    <groupId>com.etolmach.spring</groupId>
    <artifactId>jcommander-spring-boot-starter</artifactId>
    <version>1.1</version>
</dependency>
// https://jarcasting.com/artifacts/com.etolmach.spring/jcommander-spring-boot-starter/
implementation 'com.etolmach.spring:jcommander-spring-boot-starter:1.1'
// https://jarcasting.com/artifacts/com.etolmach.spring/jcommander-spring-boot-starter/
implementation ("com.etolmach.spring:jcommander-spring-boot-starter:1.1")
'com.etolmach.spring:jcommander-spring-boot-starter:jar:1.1'
<dependency org="com.etolmach.spring" name="jcommander-spring-boot-starter" rev="1.1">
  <artifact name="jcommander-spring-boot-starter" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.etolmach.spring', module='jcommander-spring-boot-starter', version='1.1')
)
libraryDependencies += "com.etolmach.spring" % "jcommander-spring-boot-starter" % "1.1"
[com.etolmach.spring/jcommander-spring-boot-starter "1.1"]

Dependencies

compile (4)

Group / Artifact Type Version
org.springframework.boot : spring-boot-configuration-processor Optional jar 2.0.2.RELEASE
org.springframework.boot : spring-boot-autoconfigure jar 2.0.2.RELEASE
com.beust : jcommander jar 1.72
org.mockito : mockito-core jar 2.8.9

provided (1)

Group / Artifact Type Version
org.projectlombok : lombok jar 1.16.18

test (1)

Group / Artifact Type Version
org.springframework.boot : spring-boot-starter-test jar 2.0.2.RELEASE

Project Modules

There are no modules declared in this project.

JCommander integration with SpringBoot

Maven Central Master Branch Build Status codecov Requirements Status

"Because life is too short to parse command line parameters"

-Cédric Beust

JCommander Spring-Boot-Starter will help you to use JCommander with ease.

For more detailed explanation and code samples please consult the home page.

Hello World

  1. Add jcommander-spring-boot-starter to your SpringBoot project

    <dependency>
        <groupId>com.etolmach.spring.boot</groupId>
        <artifactId>jcommander-spring-boot-starter</artifactId>
        <version>${jcommander.starter.version}</version>
    </dependency>
  2. Define your command parameters bean

    @Data
    @Component
    @Parameters(
        commandNames = "greeting",
        commandDescription = "Say hi to user",
        separators = "="
    )
    public class GreetingCommand {
        @Parameter(names = {"-name", "-n"},  description = "User name", required = true)
        private String name;
     
        @Parameter(names = {"-lang"},  description = "Language")
        private String topic = "en";
    }

    For more details consult JCommander guide.

  3. Define your command controller bean

    @CommandController
    public class HelloWorldController {
        
        private static final String EN_TEMPLATE = "Hello %s!";
        private static final String ES_TEMPLATE = "¡Hola %s!";
        private static final String RU_TEMPLATE = "Привет %s!";
    
        @CommandHandler(command = "greeting")
        public void greet(
            @CommandParameter(name = "-name") String name,
            @CommandParameter(name = "-lang") String language
        ) {  
            String template;
            switch (language){  
                case "es":
                    template = ES_TEMPLATE;
                    break;   
                case "ru":
                    template = RU_TEMPLATE;
                    break;   
                case "en":
                default:
                    template = EN_TEMPLATE;
            }
            System.out.println(String.format(template, name));
        }
    }
  4. Build and run

    java -jar hello.jar greeting -name=Jack -lang=es
    
  5. Output

    ¡Hola Jack!
    

Configuration

By default the starter automatically listens for command-line arguments on application start-up. You can disable this option by setting parameter:

jcommander.runner.enabled=false    

Requirements

  • JDK 1.8+
  • SpringBoot 2+

Versions

Version
1.1