adopt stdlib to CLI tool
This commit is contained in:
parent
3948283481
commit
d3785afa6f
@ -10,10 +10,11 @@ import com.github.ajalt.clikt.parameters.options.flag
|
|||||||
import com.github.ajalt.clikt.parameters.options.option
|
import com.github.ajalt.clikt.parameters.options.option
|
||||||
import kotlinx.coroutines.runBlocking
|
import kotlinx.coroutines.runBlocking
|
||||||
import net.sergeych.lyng.LyngVersion
|
import net.sergeych.lyng.LyngVersion
|
||||||
import net.sergeych.lyng.Scope
|
import net.sergeych.lyng.Script
|
||||||
import net.sergeych.lyng.ScriptError
|
import net.sergeych.lyng.ScriptError
|
||||||
import net.sergeych.lyng.Source
|
import net.sergeych.lyng.Source
|
||||||
import net.sergeych.lyng.obj.*
|
import net.sergeych.lyng.obj.*
|
||||||
|
import net.sergeych.mp_tools.globalDefer
|
||||||
import okio.FileSystem
|
import okio.FileSystem
|
||||||
import okio.Path.Companion.toPath
|
import okio.Path.Companion.toPath
|
||||||
import okio.SYSTEM
|
import okio.SYSTEM
|
||||||
@ -38,14 +39,13 @@ data class CommandResult(
|
|||||||
val error: String
|
val error: String
|
||||||
)
|
)
|
||||||
|
|
||||||
val baseScope = Scope().apply {
|
val baseScopeDefer = globalDefer {
|
||||||
addFn("exit") {
|
Script.newScope().apply {
|
||||||
exit(requireOnlyArg<ObjInt>().toInt())
|
addFn("exit") {
|
||||||
ObjVoid
|
exit(requireOnlyArg<ObjInt>().toInt())
|
||||||
|
ObjVoid
|
||||||
|
}
|
||||||
}
|
}
|
||||||
// ObjString.type.addFn("shell") {
|
|
||||||
//
|
|
||||||
// }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fun runMain(args: Array<String>) {
|
fun runMain(args: Array<String>) {
|
||||||
@ -88,41 +88,44 @@ private class Lyng(val launcher: (suspend () -> Unit) -> Unit) : CliktCommand()
|
|||||||
""".trimIndent()
|
""".trimIndent()
|
||||||
|
|
||||||
override fun run() {
|
override fun run() {
|
||||||
when {
|
runBlocking {
|
||||||
version -> {
|
val baseScope = baseScopeDefer.await()
|
||||||
println("Lyng language version ${LyngVersion}")
|
when {
|
||||||
}
|
version -> {
|
||||||
|
println("Lyng language version ${LyngVersion}")
|
||||||
|
}
|
||||||
|
|
||||||
execute != null -> {
|
execute != null -> {
|
||||||
val objargs = mutableListOf<String>()
|
val objargs = mutableListOf<String>()
|
||||||
script?.let { objargs += it }
|
script?.let { objargs += it }
|
||||||
objargs += args
|
objargs += args
|
||||||
baseScope.addConst(
|
baseScope.addConst(
|
||||||
"ARGV", ObjList(
|
"ARGV", ObjList(
|
||||||
objargs.map { ObjString(it) }.toMutableList()
|
objargs.map { ObjString(it) }.toMutableList()
|
||||||
|
)
|
||||||
)
|
)
|
||||||
)
|
launcher {
|
||||||
launcher {
|
// there is no script name, it is a first argument instead:
|
||||||
// there is no script name, it is a first argument instead:
|
processErrors {
|
||||||
processErrors {
|
baseScope.eval(execute!!)
|
||||||
baseScope.eval(execute!!)
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
else -> {
|
else -> {
|
||||||
if (script == null) {
|
if (script == null) {
|
||||||
println(
|
println(
|
||||||
"""
|
"""
|
||||||
|
|
||||||
Error: no script specified.
|
Error: no script specified.
|
||||||
|
|
||||||
""".trimIndent()
|
""".trimIndent()
|
||||||
)
|
)
|
||||||
echoFormattedHelp()
|
echoFormattedHelp()
|
||||||
} else {
|
} else {
|
||||||
baseScope.addConst("ARGV", ObjList(args.map { ObjString(it) }.toMutableList()))
|
baseScope.addConst("ARGV", ObjList(args.map { ObjString(it) }.toMutableList()))
|
||||||
launcher { executeFile(script!!) }
|
launcher { executeFile(script!!) }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -131,7 +134,7 @@ private class Lyng(val launcher: (suspend () -> Unit) -> Unit) : CliktCommand()
|
|||||||
|
|
||||||
fun executeFileWithArgs(fileName: String, args: List<String>) {
|
fun executeFileWithArgs(fileName: String, args: List<String>) {
|
||||||
runBlocking {
|
runBlocking {
|
||||||
baseScope.addConst("ARGV", ObjList(args.map { ObjString(it) }.toMutableList()))
|
baseScopeDefer.await().addConst("ARGV", ObjList(args.map { ObjString(it) }.toMutableList()))
|
||||||
executeFile(fileName)
|
executeFile(fileName)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -148,7 +151,7 @@ suspend fun executeFile(fileName: String) {
|
|||||||
text = text.substring(pos + 1)
|
text = text.substring(pos + 1)
|
||||||
}
|
}
|
||||||
processErrors {
|
processErrors {
|
||||||
baseScope.eval(Source(fileName, text))
|
baseScopeDefer.await().eval(Source(fileName, text))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user