如何在Spring shell中使用Kotlin传递varargs

fdbelqdn  于 2022-11-16  发布在  Kotlin
关注(0)|答案(1)|浏览(150)

我正在尝试编写一个Spring shell应用程序并传递一些var参数。基本上我希望传递
fetch FA-207542 FA-207984 FA-211258 FA-202298此处的文档仍待定

@ShellMethod(value = "fetches all pic related data for processing")
    fun fetch(vararg tickets: String) {
        println("number of tickets is ${tickets.size}")
    }

成果

shell:>fetch FA-207542 FA-207984 FA-211258  FA-202298
number of tickets is 1

我尝试使用StringArray,但出现错误:

fun fetch(tickets: StringArray) {
        println("number of tickets is ${tickets.size()}")
    }

错误:

shell:>fetch FA-207542 FA-207984 FA-211258 FA-202298
Parameter specified as non-null is null: method FetchCommand.fetch, parameter tickets
Details of the error have been omitted. You can use the stacktrace command to print the full stacktrace.
4ktjp1zp

4ktjp1zp1#

使用java(sry),我可以实现以下功能:

@ShellComponent
public class MyCommands {

  @ShellMethod
  public String fetch(String... tickets) {
    return String.format("Number of tickets is %d.", tickets.length);
  }
}

然后:

shell:>fetch --tickets FA-207542 FA-207984 FA-211258 FA-202298
Number of tickets is 4.
shell:>

使用以下产品可获得相同的结果:分别为@ShellCommand(arity = -1)arity = Integer.MAX_VALUE。#

相关问题