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

Can't use option inside option group in defaultLazy #473

Closed
kunyavskiy opened this issue Dec 16, 2023 · 2 comments · Fixed by #474
Closed

Can't use option inside option group in defaultLazy #473

kunyavskiy opened this issue Dec 16, 2023 · 2 comments · Fixed by #474

Comments

@kunyavskiy
Copy link

Consider following code

import com.github.ajalt.clikt.core.CliktCommand
import com.github.ajalt.clikt.parameters.groups.OptionGroup
import com.github.ajalt.clikt.parameters.groups.provideDelegate
import com.github.ajalt.clikt.parameters.options.*

class Group : OptionGroup() {
    val baseInGroup by option("--base-in-group").required()
    override fun toString() = "Group(baseInGroup=$baseInGroup)"
}

class Command : CliktCommand() {
    val group by Group()
    val baseInCommand by option("--base-in-command").required()
    val derivedGroup by option("--derived-in-group").defaultLazy { group.baseInGroup }
    val derivedInCommand by option("--derived-in-command").defaultLazy { baseInCommand }
    override fun toString() = """
        Command(
            group=$group
            baseInCommand=$baseInCommand
            derivedGroup=$derivedGroup
            derivedInCommand=$derivedInCommand
        )
        """.trimIndent()
    override fun run() {
        println(this)
    }
}

fun main(args: Array<String>) {
    Command().main(listOf(
        "--base-in-group", "base-in-groерup",
        "--base-in-command", "base-in-command",
        "--derived-in-group", "derived-in-group", // commenting this is expcetion
        "--derived-in-command", "derived-in-command", // commenting this is fine
    ))
}

Here is exception I receive, when comment-out derived-in-command line

Exception in thread "main" java.lang.IllegalStateException: Cannot read from option delegate before parsing command line
	at com.github.ajalt.clikt.parameters.internal.NullableLateinit.getValue(NullableLateinit.kt:18)
	at com.github.ajalt.clikt.parameters.options.OptionWithValuesImpl.getValue(OptionWithValues.kt:184)
	at com.github.ajalt.clikt.parameters.options.OptionDelegate$DefaultImpls.getValue(Option.kt:107)
	at com.github.ajalt.clikt.parameters.options.OptionWithValues$DefaultImpls.getValue(OptionWithValues.kt:88)
	at com.github.ajalt.clikt.parameters.options.OptionWithValuesImpl.getValue(OptionWithValues.kt:159)
	at com.github.ajalt.clikt.parameters.options.OptionWithValuesImpl.getValue(OptionWithValues.kt:159)
	at Group.getBaseInGroup(A.kt:7)
	at Command$special$$inlined$defaultLazy$default$1.invoke(TransformAll.kt:253)
	at Command$special$$inlined$defaultLazy$default$1.invoke(TransformAll.kt:97)
	at com.github.ajalt.clikt.parameters.options.OptionWithValuesImpl.finalize(OptionWithValues.kt:221)
	at com.github.ajalt.clikt.internal.FinalizationKt.finalizeOptions(Finalization.kt:45)
	at com.github.ajalt.clikt.parsers.Parser.parse(Parser.kt:252)
	at com.github.ajalt.clikt.parsers.Parser.parse(Parser.kt:40)
	at com.github.ajalt.clikt.core.CliktCommand.parse(CliktCommand.kt:458)
	at com.github.ajalt.clikt.core.CliktCommand.parse$default(CliktCommand.kt:455)
	at com.github.ajalt.clikt.core.CliktCommand.main(CliktCommand.kt:475)
	at AKt.main(A.kt:30)
@ajalt
Copy link
Owner

ajalt commented Dec 17, 2023

Thanks for the report! This should be fixed now; give the latest snapshot a try and let me know if it still doesn't work.

@kunyavskiy
Copy link
Author

Looks like it works on my original example, with mavenLocal version built from the current main branch. Looking forward to the release with the 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