scoop:scoop.cli:0.1.0
src/input.scoop
Selected production source · src/input.scoop
1
package scoop.cli3
/** Original argv item and its zero-based index in the supplied array. */4
public struct CliSource(val index: Long, val spelling: String)5
public enum CliToken { Text(String), Flag(Boolean) }6
public struct CliOccurrence(val token: CliToken, val source: CliSource)8
/** A command-line error with its source field path and original token, when present. */9
public class CliParseException public constructor(10
public val path: String, public val reason: String, public val source: Option<CliSource> = None11
) : Exception(Some(when (source) {12
case Some(val item) -> reason + " at token " + item.index.toString() + " (" + item.spelling + ")"13
case None -> reason14
}))16
internal struct CliNamedField(val name: String, val values: CliFieldValues)17
internal enum CliInputResult { Help, Value(CliValues) }19
/** Immutable collected fields, looked up by their source names. */20
public class CliValues internal constructor(private val fields: Array<CliNamedField>) {21
public fun optional(name: String): Option<CliFieldValues> {22
for (field in fields) { if (field.name == name) { return Some(field.values) } }23
return None24
}25
public fun required(name: String): CliFieldValues = when (optional(name)) {26
case Some(val field) -> field27
case None -> inputError(childPath("", name), None, "missing required field")28
}29
}31
internal struct CliAlias(val index: Long, val negative: Boolean)33
internal fun childPath(path: String, name: String): String {34
val result = StringBuilder().append(path).append("/")35
for (character in name) {36
when (character) {37
case '~' -> { result.append("~0") }38
case '/' -> { result.append("~1") }39
else -> { result.append(character) }40
}41
}42
return result.toString()43
}45
internal fun inputError(path: String, source: Option<CliSource>, message: String): Nothing {46
throw CliParseException(path, message, source)47
}49
internal fun optionToken(text: String): Boolean = text.charLength >= 2L && text.getCharAt(0L) == '-' &&50
(text.getCharAt(1L) == '-' || asciiLetter(text.getCharAt(1L)))52
internal fun findAlias(entries: Array<CliEntry>, name: String): Option<CliAlias> {53
for (index in 0L until entries.size) {54
for (alias in entries[index].names) { if (alias == name) { return Some(CliAlias(index, false)) } }55
for (alias in entries[index].negativeNames) { if (alias == name) { return Some(CliAlias(index, true)) } }56
}57
return None58
}60
internal fun textInput(entry: CliEntry, text: String, source: CliSource): CliOccurrence {61
val choices = when (entry.valueShape) {62
case CliValueShape.Choice(val names) -> Some(names)63
case CliValueShape.Repeated(CliElementShape.Choice(val names)) -> Some(names)64
case CliValueShape.Flag -> inputError(childPath("", entry.sourceName), Some(source), "flag does not accept a value")65
else -> None66
}67
when (choices) {68
case Some(val names) -> {69
for (name in names) { if (name == text) { return CliOccurrence(CliToken.Text(text), source) } }70
inputError(childPath("", entry.sourceName), Some(source), "unknown choice " + text)71
}72
case None -> {}73
}74
return CliOccurrence(CliToken.Text(text), source)75
}