0

複数のサブコマンドを含む CLI があります。一部のサブコマンドには-f、入力ファイルを指定できるオプションのフラグがあります。

@CommandLine.Command(name = "get", description = ["Get something"])
class GetUserCommand: Runnable {

    @Option(names = ["-f", "--file"], description = ["Input file"])
    var filename: String? = null
    
    override fun run() {
       var content = read_file(filename)
    }
}


@CommandLine.Command(name = "query", description = ["Query something"])
class QueryUserCommand: Runnable {

    @Option(names = ["-f", "--file"], description = ["Input file"])
    var filename: String? = null
    
    override fun run() {
       var content = read_file(filename)
    }
}


入力ファイル形式は、コマンドごとに異なる場合があります。理想的には、引数として指定された場合、ファイルを自動的に解析したいと思います。また、ファイルの内容はコマンドごとに異なる場合があります (ただし、特定の形式、CSV または JSON になります)。

たとえば、私はこのようなものが欲しいです

data class First(val col1, val col2)

data class Second(val col1, val col2, val col3)

class CustomOption(// regular @Option parameters, targetClass=...) {
  // do generic file parsing
}


@CommandLine.Command(name = "get", description = ["Get something"])
class GetUserCommand: Runnable {

    @CustomOption(names = ["-f", "--file"], description = ["Input file"], targetClass=First))
    var content: List<First> = emptyList()
    
    override fun run() {
       // content now contains the parse file
    }
}


@CommandLine.Command(name = "query", description = ["Query something"])
class QueryUserCommand: Runnable {

    @CustomOption(names = ["-f", "--file"], description = ["Input file"], targetClass=Second))
    var content: List<Second> = emptyList()
    
    override fun run() {
       // content now contains the parse file
    }
}

これが可能かどうか、またはその方法を知っている人はいますか?

4

1 に答える 1