Shared Options/Arguments with Subcommands
AvlWx2014 opened this issue · 2 comments
AvlWx2014 commented
Is it possible with the current state of the API to share options/arguments with subcommands?
Example:
I have two subcommands, which process either a) a single file or b) a set of files contained in multiple directories.
Naturally I created two subcommands:
class Single(private val runner: Runner) : Subcommand("single", "Process a single file") {
private val file by argument(
type = ArgType.String,
fullName = "file",
description = "Process the given file."
)
override fun execute() {
runner.single(File(file))
}
}
class Multiple(private val runner: Runner) : Subcommand("multiple", "Process all files in the given directories") {
private val directories by argument(
type = ArgType.String,
fullName = "directories",
description = "Process all files in the given directory or directories."
).vararg()
override fun execute() {
runner.multiple(directories.map(::File))
}
}
What I'd like to do is have both subcommands be able to access a shared -o --output
option to specify the output directory.
private val output by option(
type = ArgType.String,
shortName = "o", fullName = "output",
description = "Specify the output directory"
).default(".")
At the moment it seems like I have to duplicate this in each subcommand for this to work.
LepilkinaElena commented
Hello, yes, you can with inheritance. Example https://github.com/Kotlin/kotlinx-cli/blob/master/core/commonTest/src/SubcommandsTests.kt#L38
AvlWx2014 commented
Awesome, thank you!