Skip to content

Commit

Permalink
Simplify splitOptionPrefix implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
ajalt committed Nov 1, 2023
1 parent 93d88e9 commit c680a95
Show file tree
Hide file tree
Showing 5 changed files with 5 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,4 @@ internal expect fun isWindowsMpp(): Boolean
/** Doesn't return [Nothing], since it's a no-op on the browser */
internal expect fun exitProcessMpp(status: Int)

internal expect fun isLetterOrDigit(c: Char): Boolean

internal expect fun readFileIfExists(filename: String): String?
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.github.ajalt.clikt.parameters.options

import com.github.ajalt.clikt.completion.CompletionCandidates
import com.github.ajalt.clikt.core.*
import com.github.ajalt.clikt.mpp.isLetterOrDigit
import com.github.ajalt.clikt.core.Context
import com.github.ajalt.clikt.core.GroupableOption
import com.github.ajalt.clikt.core.ParameterHolder
import com.github.ajalt.clikt.core.StaticallyGroupedOption
import com.github.ajalt.clikt.output.HelpFormatter
import com.github.ajalt.clikt.parsers.Invocation
import com.github.ajalt.clikt.sources.ValueSource
Expand Down Expand Up @@ -130,7 +132,7 @@ internal fun inferEnvvar(names: Set<String>, envvar: String?, autoEnvvarPrefix:
/** Split an option token into a pair of prefix to simple name. */
internal fun splitOptionPrefix(name: String): Pair<String, String> =
when {
name.length < 2 || isLetterOrDigit(name[0]) -> "" to name
name.length < 2 || name[0] !in "-@/+" -> "" to name
name.length > 2 && name[0] == name[1] -> name.slice(0..1) to name.substring(2)
else -> name.substring(0, 1) to name.substring(1)
}
Expand Down
4 changes: 0 additions & 4 deletions clikt/src/jsMain/kotlin/com/github/ajalt/clikt/mpp/MppImpl.kt
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,8 @@ private val impls: JsMppImpls = try {
BrowserMppImpls
}

private val LETTER_OR_DIGIT_RE = Regex("""[a-zA-Z0-9]""")

internal actual val String.graphemeLengthMpp: Int get() = replace(ANSI_CODE_RE, "").length

internal actual fun isLetterOrDigit(c: Char): Boolean = LETTER_OR_DIGIT_RE.matches(c.toString())

internal actual fun readEnvvar(key: String): String? = impls.readEnvvar(key)
internal actual fun isWindowsMpp(): Boolean = impls.isWindowsMpp()
internal actual fun exitProcessMpp(status: Int): Unit = impls.exitProcessMpp(status)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@ internal actual fun exitProcessMpp(status: Int) {
exitProcess(status)
}

internal actual fun isLetterOrDigit(c: Char): Boolean {
return c.isLetterOrDigit()
}

internal actual fun readFileIfExists(filename: String): String? {
val file = File(filename)
if (!file.isFile) return null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,8 @@ import platform.posix.fopen
import kotlin.experimental.ExperimentalNativeApi
import kotlin.system.exitProcess

private val LETTER_OR_DIGIT_RE = Regex("""[a-zA-Z0-9]""")

internal actual val String.graphemeLengthMpp: Int get() = replace(ANSI_CODE_RE, "").length

internal actual fun isLetterOrDigit(c: Char): Boolean = LETTER_OR_DIGIT_RE.matches(c.toString())

internal actual fun isWindowsMpp(): Boolean = Platform.osFamily == OsFamily.WINDOWS

internal actual fun exitProcessMpp(status: Int): Unit = exitProcess(status)
Expand Down

0 comments on commit c680a95

Please sign in to comment.