MTI TEK
Spring Framework | Spring Shell

Spring Shell

Overview

Maven Dependency

<properties>
    <spring-shell.version>4.0.2</spring-shell.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.shell</groupId>
        <artifactId>spring-shell-starter</artifactId>
        <version>${spring-shell.version}</version>
    </dependency>
</dependencies>

Defining Shell Commands: @Command and @Option

package com.mtitek.spring.shell;

import org.springframework.shell.core.command.annotation.Command;
import org.springframework.shell.core.command.annotation.Option;
import org.springframework.stereotype.Component;

@Component
public class ShellCommands {

    @Command(name = "min-value", description = "Display Min Value")
    public int minValue() {
        return 1_000;
    }

    @Command(name = "max-value", description = "Display Max Value")
    public void maxValue() {
        System.out.println(1_000_000);
    }

    @Command(name = "to-lower-case", description = "Convert to lower case")
    public String toLowerCase(
            @Option(longName = "input", shortName = 'i', description = "String to convert", defaultValue = "HELLO")
            String input) {
        return input.toLowerCase();
    }

    @Command(name = "add-values", description = "Add two values")
    public Integer addValues(@Option(shortName = 'a') Integer a, @Option(shortName = 'b') Integer b) {
        return a + b;
    }
}

Running the Shell

shell:> help
AVAILABLE COMMANDS

Built-In Commands
    clear: Clear the terminal screen
    help: Show help about available commands
    quit, exit: Exit the shell
    script: Execute commands from a script file
    version: Show version info
Shell Commands
    add-values: Add two values
    max-value: Display Max Value
    min-value: Display Min Value
    to-lower-case: Convert to lower case

shell:> min-value
1000

shell:> max-value
1000000

shell:> to-lower-case --input ABC
abc

shell:> add-values -a 5 -b 7
12

Notes