Scoop API

scoop:scoop.cli:0.1.0

src/field_values.scoop

Selected production source · src/field_values.scoop

1package scoop.cli
3/** Occurrences of a supplied field. Reading or converting a value does not consume it. */
4public class CliFieldValues internal constructor(public val path: String, public val items: Array<CliOccurrence>) {
5 public fun fail(message: String): Nothing = inputError(path, Some(items[0L].source), message)
6 public fun element(index: Long): CliFieldValues =
7 CliFieldValues(childPath(path, index.toString()), [items[index]])
8 private fun single(): CliOccurrence {
9 if (items.size != 1L) { fail("expected a single argument") }
10 return items[0L]
11 }
13 public fun readString(): String = when (single().token) {
14 case CliToken.Text(val text) -> text
15 else -> fail("expected text")
16 }
17 public fun readBoolean(): Boolean = when (single().token) {
18 case CliToken.Flag(val result) -> result
19 case CliToken.Text(val text) -> {
20 if (text == "true") { true } else { if (text == "false") { false } else { fail("expected true or false") } }
21 }
22 }
23 public fun readLong(): Long = signedInteger(this, readString())
24 public fun readULong(): ULong = unsignedInteger(this, readString())
25 public fun readFloat(): Float {
26 val text = readString()
27 checkDecimal(this, text)
28 return when (parseFloat(text)) { case Some(val value) -> value; case None -> fail("Float is out of range") }
29 }
30 public fun readDouble(): Double {
31 val text = readString()
32 checkDecimal(this, text)
33 return when (parseDouble(text)) { case Some(val value) -> value; case None -> fail("Double is out of range") }
34 }
35 public fun readChar(): Char {
36 val text = readString()
37 if (text.charLength != 1L) { fail("expected one character") }
38 return text.getCharAt(0L)
39 }
40 public fun readInt8(): Int8 {
41 val value = readLong()
42 if (value < -128L || value > 127L) { fail("Int8 is out of range") }
43 return value.toByte()
44 }
45 public fun readUInt8(): UInt8 {
46 val value = readULong()
47 if (value > 255uL) { fail("UInt8 is out of range") }
48 return value.toUByte()
49 }
50 public fun readInt16(): Int16 {
51 val value = readLong()
52 if (value < -32768L || value > 32767L) { fail("Int16 is out of range") }
53 return value.toShort()
54 }
55 public fun readUInt16(): UInt16 {
56 val value = readULong()
57 if (value > 65535uL) { fail("UInt16 is out of range") }
58 return value.toUShort()
59 }
60 public fun readInt(): Int {
61 val value = readLong()
62 if (value < -2147483648L || value > 2147483647L) { fail("Int is out of range") }
63 return value.toInt()
64 }
65 public fun readUInt(): UInt {
66 val value = readULong()
67 if (value > 4294967295uL) { fail("UInt is out of range") }
68 return value.toUInt()
69 }