split error and exception
This commit is contained in:
@@ -0,0 +1,24 @@
|
||||
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"?""")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user