无法在Spring-shell中使用可选参数

pzfprimi  于 2022-12-23  发布在  Spring
关注(0)|答案(1)|浏览(185)

我正在尝试用spring-shell编写一些代码,遇到了一些问题。请查找下面的代码

@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.当我在greet上发出help命令时,它显示没有可用选项。**
shell:>help greet
NAME
       greet - This command is used to greet a user

SYNOPSIS
       greet 

OPTIONS
    • 2."问候语约翰"给了我一个意想不到的结果,我的期望是"你好约翰"**
shell:>greet -name john
Hello null
    • 3.当我执行"mvn clean package"时,构建在测试阶段受到冲击,直到我删除测试类**

      4.删除"SpringShellExampleApplicationTests"后,当我执行命令java -jar spring-shell-example-0.0.1-SNAPSHOT.jar --debug时,spring将--debug视为spring shell命令,并给我以下错误。
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)

先谢了。

50pmv0ei

50pmv0ei1#

我不知道您使用的是哪个版本的spring-shell。一个可能的解决方案与Spring Shell 3.0.0-M3和Sping 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 great", 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);
    }

}

由此产生的帮助:

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 great
       [Optional, default = User]

       --city String
       Give the city name you are from
       [Mandatory]

测试1:

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

测试二:

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

相关问题