scoop:scoop.cli:0.1.0
src/schema_validation.scoop
Selected production source · src/schema_validation.scoop
1
package scoop.cli3
internal struct CliLayout(val entries: Array<CliEntry>, val positions: Array<Long>)5
internal fun asciiLetter(value: Char): Boolean =6
(value >= 'a' && value <= 'z') || (value >= 'A' && value <= 'Z')8
internal fun invalidNameCharacter(value: Char): Boolean {9
val code = value.code10
return code <= 32 || (code >= 127 && code <= 160) || code == 5760 ||11
(code >= 8192 && code <= 8202) || code == 8232 || code == 8233 ||12
code == 8239 || code == 8287 || code == 12288 || value == '='13
}15
internal fun addUnique(values: ArrayList<String>, name: String, kind: String) {16
for (existing in values) {17
if (existing == name) { configuration("duplicate " + kind + ": " + name) }18
}19
values.add(name)20
}22
internal fun checkOptionName(name: String) {23
val short = name.charLength == 2L && name.getCharAt(0L) == '-' && asciiLetter(name.getCharAt(1L))24
val long = name.charLength > 2L && name.getCharAt(0L) == '-' && name.getCharAt(1L) == '-'25
if (!short && !long) { configuration("invalid option name: " + name) }26
for (character in name) {27
if (invalidNameCharacter(character)) { configuration("invalid option name: " + name) }28
}29
}31
internal fun checkChoices(choices: Array<String>) {32
if (choices.size == 0L) { configuration("CLI choices must not be empty") }33
val names = ArrayList<String>()34
for (choice in choices) { addUnique(names, choice, "choice") }35
}37
internal fun checkedEntries(entries: Array<CliEntry>): CliLayout {38
val names = ArrayList<String>()39
names.add("-h"); names.add("--help")40
val fields = ArrayList<String>()41
var positionalCount = 0L42
for (entry in entries) {43
addUnique(fields, entry.sourceName, "source name")44
when (entry.role) {45
case CliRole.Argument(_) -> {46
positionalCount = positionalCount + 1L47
if (entry.names.size != 0L || entry.negativeNames.size != 0L) {48
configuration("positional argument cannot have option names: " + entry.sourceName)49
}50
}51
else -> {52
if (entry.names.size == 0L) { configuration("option requires a name: " + entry.sourceName) }53
}54
}55
val flag = when (entry.role) { case CliRole.Flag -> true; else -> false }56
val flagShape = when (entry.valueShape) { case CliValueShape.Flag -> true; else -> false }57
if (flag != flagShape || (!flag && entry.negativeNames.size != 0L)) {58
configuration("flag role and value shape disagree: " + entry.sourceName)59
}60
for (alias in entry.names) { checkOptionName(alias); addUnique(names, alias, "option name") }61
for (alias in entry.negativeNames) { checkOptionName(alias); addUnique(names, alias, "option name") }62
when (entry.valueShape) {63
case CliValueShape.Choice(val choices) -> checkChoices(choices)64
case CliValueShape.Repeated(CliElementShape.Choice(val choices)) -> checkChoices(choices)65
else -> {}66
}67
}68
val positions = MutableArray<Long>(positionalCount) { _ -> -1L }69
for (index in 0L until entries.size) {70
when (entries[index].role) {71
case CliRole.Argument(val position) -> {72
if (position < 0L || position >= positionalCount) { configuration("positional indices must start at zero and be contiguous") }73
if (positions[position] != -1L) { configuration("duplicate positional index " + position.toString()) }74
positions[position] = index75
}76
else -> {}77
}78
}79
var optionalSeen = false80
for (position in 0L until positions.size) {81
val entry = entries[positions[position]]82
if (optionalSeen && entry.required && !isRepeated(entry.valueShape)) { configuration("required positional follows an optional argument: " + entry.sourceName) }83
if (!entry.required) { optionalSeen = true }84
if (isRepeated(entry.valueShape) && position != positions.size - 1L) {85
configuration("variadic positional must be last: " + entry.sourceName)86
}87
}88
return CliLayout(entries, positions.toArray())89
}91
internal fun isRepeated(shape: CliValueShape): Boolean = when (shape) {92
case CliValueShape.Repeated(_) -> true93
else -> false94
}