I have a CLI with multiple sub-commands, some of the sub-commands have an optional flag -f
with which an input file can be specified, e.g.
@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)
}
}
The input file format can be different from command to command. Ideally, I'd like to parse the file automatically if it was specified as an argument. Also the file content can be different on each command (but will be a specific format, CSV or JSON).
For example I'd like to have something like this
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
}
}
Would anyone have an idea if this is possible or how to do it?