Skip to content

What is the intended way of getting the value of manually registered option #514

Answered by ajalt
yngvedh asked this question in Q&A
Discussion options

You must be logged in to vote
  • You can use OptionDelegate instead of OptionWithValues, since that's the interface that defines value. If it's not an OptionDelegate, then it doesn't have a value.
  • Options normally do return null if they weren't provided. value only throws an exception on access when you haven't called parse/main, or if you access it after parse thrown an exception.
class C : CliktCommand() {
    override fun run() {
        registeredOptions().forEach {
            echo("${it.names.joinToString()}: ${(it as? OptionDelegate<*>)?.value}")
        }
    }
}
with(C()) {
    registerOption(option("--foo"))
    main(emptyArray())
}
$ ./c
--foo: null
-h, --help: false

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by yngvedh
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants