Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Default group choice causes null pointer exception #179

Closed
FWDekker opened this issue May 19, 2020 · 2 comments · Fixed by #180
Closed

Default group choice causes null pointer exception #179

FWDekker opened this issue May 19, 2020 · 2 comments · Fixed by #180

Comments

@FWDekker
Copy link

FWDekker commented May 19, 2020

If a command has a group choice with a default group, then the options in this group do not work correctly if the user does not specify the group choice explicitly.

Expectation

Consider the following minimal working example.

class Command : CliktCommand() {
    class GroupA : OptionGroup() {
        val subOptionA: Int by option()
            .int()
            .default(5)
    }
    class GroupB : OptionGroup()

    val groupOption by option()
        .groupChoice("a" to GroupA(), "b" to GroupB())
        .defaultByName("a")

    override fun run() {
        when (val it = groupOption) {
            is GroupA -> println(it.subOptionA)
            is GroupB -> println("Nothing to see here")
            else -> error("Oops")
        }
    }
}

fun main(args: Array<String>) = Command().main(args)

The default choice for groupOption is a. Therefore, I expect that if --group-option is not specified, the behaviour is equivalent to specifying --group-option a. This means that subOptionA takes on the value 5, and running the program should print 5.

Reality

The above code throws an exception if no arguments are provided to the program. Similarly, running with only the arguments --sub-option-a 3 also throws an exception. Only when --group-option a is also specified does the program work without exceptions.

Not giving any exceptions results in the following trace.

Exception in thread "main" java.lang.NullPointerException
    at com.fwdekker.mypackage.Command$GroupA.getSubOptionA(Main.kt)
    at com.fwdekker.mypackage.Command.run(Main.kt:47)
    at com.github.ajalt.clikt.parsers.Parser.parse(Parser.kt:168)
    at com.github.ajalt.clikt.parsers.Parser.parse(Parser.kt:16)
    at com.github.ajalt.clikt.core.CliktCommand.parse(CliktCommand.kt:258)
    at com.github.ajalt.clikt.core.CliktCommand.parse$default(CliktCommand.kt:255)
    at com.github.ajalt.clikt.core.CliktCommand.main(CliktCommand.kt:273)
    at com.github.ajalt.clikt.core.CliktCommand.main(CliktCommand.kt:298)
    at com.fwdekker.mypackage.MainKt.main(Main.kt:54)

Additional information

System Version
Operating system Windows 10 or Ubuntu 18.04
Kotlin 1.3.72
Java 1.8.0 or 11.0.6
@FWDekker
Copy link
Author

Awesome! Thanks!

@ajalt
Copy link
Owner

ajalt commented May 19, 2020

Thank you for the clear write-up! I just released version 2.7.1, which contains this fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants