Scoop API

scoop:scoop.cli:0.1.0

src/tokens.scoop

Selected production source · src/tokens.scoop

1package scoop.cli
3internal class CliTokens(
4 private val entries: Array<CliEntry>, private val positions: Array<Long>,
5 private val arguments: Array<String>, start: Long
6) {
7 private var cursor: Long = start
8 private var positional: Long = 0L
9 private val values: Array<ArrayList<CliOccurrence>> = Array<ArrayList<CliOccurrence>>(entries.size) { _ -> ArrayList<CliOccurrence>() }
11 internal fun parse(): CliInputResult {
12 var options = true
13 while (cursor < arguments.size) {
14 val source = CliSource(cursor, arguments[cursor])
15 val text = source.spelling
16 cursor = cursor + 1L
17 if (options && text == "--") { options = false; continue }
18 if (options && optionToken(text)) {
19 val help = if (text.getCharAt(1L) == '-') { longOption(text, source) } else { shortOptions(text, source) }
20 if (help) { return CliInputResult.Help }
21 } else {
22 if (positional == positions.size) { inputError("", Some(source), "extra positional argument") }
23 val index = positions[positional]
24 add(index, textInput(entries[index], text, source), source)
25 if (!isRepeated(entries[index].valueShape)) { positional = positional + 1L }
26 }
27 }
28 val fields = ArrayList<CliNamedField>()
29 for (index in 0L until entries.size) {
30 val items = values[index]
31 if (items.size != 0L) {
32 val name = entries[index].sourceName
33 fields.add(CliNamedField(name, CliFieldValues(childPath("", name), items.toArray())))
34 }
35 }
36 return CliInputResult.Value(CliValues(fields.toArray()))
37 }
39 private fun alias(name: String, source: CliSource): CliAlias = when (findAlias(entries, name)) {
40 case Some(val result) -> result
41 case None -> inputError("", Some(source), "unknown option " + name)
42 }
44 private fun add(index: Long, value: CliOccurrence, source: CliSource) {
45 if (!isRepeated(entries[index].valueShape) && values[index].size != 0L) {
46 inputError(childPath("", entries[index].sourceName), Some(source), "duplicate argument " + entries[index].sourceName)
47 }
48 values[index].add(value)
49 }
51 private fun takeValue(index: Long, attached: Option<String>, source: CliSource): CliOccurrence {
52 when (attached) {
53 case Some(val text) -> { return textInput(entries[index], text, source) }
54 case None -> {}
55 }
56 if (cursor == arguments.size || optionToken(arguments[cursor])) {
57 inputError(childPath("", entries[index].sourceName), Some(source), "missing option value")
58 }
59 val next = CliSource(cursor, arguments[cursor])
60 cursor = cursor + 1L
61 return textInput(entries[index], next.spelling, next)
62 }
64 private fun longOption(text: String, source: CliSource): Boolean {
65 var split = text.charLength
66 for (index in 2L until text.charLength) { if (text.getCharAt(index) == '=') { split = index; break } }
67 val name = text.slice(0L, split)
68 val attached = if (split == text.charLength) { None } else { Some(text.slice(split + 1L, text.charLength)) }
69 if (name == "--help") {
70 if (split != text.charLength) { inputError("", Some(source), "help does not accept a value") }
71 return true
72 }
73 val selected = alias(name, source)
74 when (entries[selected.index].role) {
75 case CliRole.Flag -> {
76 if (split != text.charLength) { inputError(childPath("", entries[selected.index].sourceName), Some(source), "flag does not accept a value") }
77 add(selected.index, CliOccurrence(CliToken.Flag(!selected.negative), source), source)
78 }
79 else -> add(selected.index, takeValue(selected.index, attached, source), source)
80 }
81 return false
82 }
84 private fun shortOptions(text: String, source: CliSource): Boolean {
85 var index = 1L
86 while (index < text.charLength) {
87 val name = "-" + text.getCharAt(index).toString()
88 index = index + 1L
89 if (name == "-h") {
90 if (index < text.charLength && text.getCharAt(index) == '=') { inputError("", Some(source), "help does not accept a value") }
91 return true
92 }
93 val selected = alias(name, source)
94 when (entries[selected.index].role) {
95 case CliRole.Flag -> {
96 if (index < text.charLength && text.getCharAt(index) == '=') { inputError(childPath("", entries[selected.index].sourceName), Some(source), "flag does not accept a value") }
97 add(selected.index, CliOccurrence(CliToken.Flag(!selected.negative), source), source)
98 }
99 else -> {
100 val attached = if (index == text.charLength) { None } else {
101 if (text.getCharAt(index) == '=') { index = index + 1L }
102 Some(text.slice(index, text.charLength))
103 }
104 add(selected.index, takeValue(selected.index, attached, source), source)
105 return false
106 }
107 }
108 }
109 return false
110 }