You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Can not use the same option in different classes if set up kapt to use picocli annotation processor.
I'm getting DuplicateOptionAnnotationsException even if classes are separate commands.
It looks that there is an error when processing INHERIT options, check is perfrmed for all classes, not for sub-commands only.
Here is a code to reproduce:
import picocli.CommandLine
import picocli.CommandLine.Command
import picocli.CommandLine.Model.CommandSpec
import picocli.CommandLine.Spec
@Command(name = "Command1", subcommands = [ CommandLine.HelpCommand::class ],
description = ["Command 1 description"])
class Command1 {
@Spec
lateinit var spec: CommandSpec
@CommandLine.Option(names = ["--option"],
scope = CommandLine.ScopeType.INHERIT,
description = ["If set to true, will not ask questions and configurations, e.g. to overwrite a file. We don't recommend setting this option to true unless you are working in a scripted environment"])
var option: String = ""
}
import picocli.CommandLine
import picocli.CommandLine.Command
import picocli.CommandLine.Model.CommandSpec
import picocli.CommandLine.Spec
@Command(name = "Command1", subcommands = [ CommandLine.HelpCommand::class ],
description = ["Command 1 description"])
class Command2 {
@Spec
lateinit var spec: CommandSpec
@CommandLine.Option(names = ["--option"],
scope = CommandLine.ScopeType.INHERIT,
description = ["If set to true, will not ask questions and configurations, e.g. to overwrite a file. We don't recommend setting this option to true unless you are working in a scripted environment"])
var option: String = ""
}
and build.gradle.kts file:
plugins {
kotlin("jvm") version "1.5.31"
kotlin("kapt") version "1.5.31"
}
group = "org.example"
version = "1.0-SNAPSHOT"
repositories {
mavenCentral()
}
dependencies {
implementation(kotlin("stdlib"))
implementation("info.picocli:picocli:4.6.1")
kapt("info.picocli:picocli-codegen:4.6.1")
}
kapt {
arguments {
arg("project", "${project.group}/${project.name}")
}
}
The text was updated successfully, but these errors were encountered:
The cause of the issue was that both commands had picocli.CommandLine.HelpCommand as a subclass.
In the annotation processor, only one CommandSpec instance was created for this HelpCommand, and this instance was shared between both Command1 and Command2.
When the inherited --option was added to Command1 it was also added to its subcommand.
When the other inherited option with the same name --option was added to Command2 it was also added to its subcommand.
The problem was that while there should have been separate CommandSpec instances for these subcommands, they were pointing to the same single instance, resulting in the DuplicateOptionAnnotationsException.
The solution was to ensure that the annotation processor did not try to reuse CommandSpec instances for subcommands.
Can not use the same option in different classes if set up kapt to use picocli annotation processor.
I'm getting
DuplicateOptionAnnotationsException
even if classes are separate commands.It looks that there is an error when processing INHERIT options, check is perfrmed for all classes, not for sub-commands only.
Here is a code to reproduce:
and build.gradle.kts file:
The text was updated successfully, but these errors were encountered: