25 lines
736 B
Kotlin
25 lines
736 B
Kotlin
package com.infendro.cli.error
|
|
|
|
import com.infendro.cli.command.Command
|
|
import com.infendro.cli.util.distance
|
|
import com.infendro.cli.util.threshold
|
|
import kotlin.math.min
|
|
|
|
class UnknownOption(
|
|
val command: Command,
|
|
val name: String,
|
|
) : Error() {
|
|
override val message: String
|
|
get() = buildString {
|
|
val suggestion = command.options
|
|
.flatMap { it.names }
|
|
.associateWith { distance(name, it) }
|
|
.filter { it.value <= min(it.key.length - 1, threshold) }
|
|
.minByOrNull { it.value }
|
|
?.key
|
|
|
|
append("""Unknown option "$name".""")
|
|
if (suggestion != null) append(""" Did you mean "$suggestion"?""")
|
|
}
|
|
}
|