javaspring-bootspring-shell

Unable to use optional parameters in Spring-shell


I'm trying to write some code in spring-shell I'm getting some issues. Please find the code below

@ShellComponent
public class CommandDemo {

    @ShellMethod(value = "This command is used to greet a user")
    public void greet(
            @ShellOption(value = "-name", help = "Give the name to great", defaultValue = "User") String name,
            @ShellOption(value = "-city", help = "Give the city name you are from") String city
            ) {

        String message = "Hello "+name;
        if(city!=null && !city.isEmpty()) {
            message+=", I'm from "+city;
        }

        System.out.println(message);
    }

}

1. When I give the help command on greet, it is giving me as no options available.

shell:>help greet
NAME
       greet - This command is used to greet a user

SYNOPSIS
       greet 

OPTIONS

2. "greet -name john" is giving me an unexpected result, my expectation is "Hello john"

shell:>greet -name john
Hello null

3. When I execute "mvn clean package" the build is getting struck at the test phase until I delete the test class enter image description here 4. After deleting "SpringShellExampleApplicationTests", and when I execute the command java -jar spring-shell-example-0.0.1-SNAPSHOT.jar --debug the spring is considering --debug as the spring shell command and giving me the below error.

No command found for '--debug'
org.springframework.shell.CommandNotFound: No command found for '--debug'
        at org.springframework.shell.Shell.evaluate(Shell.java:231)
        at org.springframework.shell.Shell.run(Shell.java:140)
        at org.springframework.shell.jline.NonInteractiveShellRunner.run(NonInteractiveShellRunner.java:104)
        at org.springframework.shell.DefaultShellApplicationRunner.run(DefaultShellApplicationRunner.java:65)
        at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:762)
        at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:752)
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:315)
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1306)
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1295)
        at com.experiments.SpringShellExampleApplication.main(SpringShellExampleApplication.java:10)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
        at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.base/java.lang.reflect.Method.invoke(Method.java:568)
        at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:49)
        at org.springframework.boot.loader.Launcher.launch(Launcher.java:108)
        at org.springframework.boot.loader.Launcher.launch(Launcher.java:58)
        at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:65)

Thanks in advance.


Solution

  • I can't tell which version of spring-shell that you are using. A possible solution with Spring Shell 3.0.0-M3 and Spring Boot 3.0.0:

    public class CommandDemo {
    
        @ShellMethod(value = "This command is used to greet a user")
        public void greet(
                @ShellOption(help = "Give the name to greet", defaultValue = "User") String name,
                @ShellOption(help = "Give the city name you are from") String city
        ) {
            String message = "Hello " + name;
            if (city != null && !city.isEmpty()) {
                message += ", I'm from " + city;
            }
    
            System.out.println(message);
         }
    }
    

    The resulting help:

    shell:>help greet
    NAME
           greet - This command is used to greet a user
    
    SYNOPSIS
           greet --name String [--city String]
    
    OPTIONS
           --name String
           Give the name to greet
           [Optional, default = User]
    
           --city String
           Give the city name you are from
           [Mandatory]
    

    Test 1:

    shell:>greet --name john --city denver
    Hello john, I'm from denver
    

    Test 2:

    shell:>greet john denver
    Hello john, I'm from denver