kscripting / kscript

Scripting enhancements for Kotlin
MIT License
2.07k stars 125 forks source link

stdin does not allow further switches #94

Closed ilanpillemer closed 6 years ago

ilanpillemer commented 6 years ago

./mme -extract -model=DEVAL_12 > temp2.kts; ./mm temp2.kts -graph works ./mme -extract -model=DEVAL_12 | ./mm - -graph does not work.

where mm is the DSL.

note running ./mme -extract -model=DEVAL_12 | ./mm - does half work, but not very useful as I need the switch.

holgerbrandl commented 6 years ago

I've tested test_script.kts - -graph with

#!/usr/bin/env kscript

println("arugment test")

for (arg in args) {
    println("arg: $arg")
}

to make sure that arguments are correctly forward to the script, which seems the case. So if stream processing fails if the option is provided, I'd suspect rather some issue with your script logic.

Could you simplify/share temp2.kts to help me reproducing the problem?

ilanpillemer commented 6 years ago

The issue is happening somehow in the preamble bash file. The kts file is piped into a preamble bash file.

ilanpillemer commented 6 years ago
#!/usr/bin/env bash

export CUSTOM_KSCRIPT_NAME='mm'

export CUSTOM_KSCRIPT_PREAMBLE='
// declare dependencies
#!/usr/bin/env kscript

//DEPS com.eclipsesource.minimal-json:minimal-json:0.9.4

//INCLUDE /Users/ilanpillemer/git/model-language/src/main/kotlin/Command.kt
//INCLUDE /Users/ilanpillemer/git/model-language/src/main/kotlin/SemanticModel.kt
//INCLUDE /Users/ilanpillemer/git/model-language/src/main/kotlin/Config.kt
//INCLUDE /Users/ilanpillemer/git/model-language/src/main/kotlin/MetaParser.kt
//INCLUDE /Users/ilanpillemer/git/model-language/src/main/kotlin/DslInterpreter.kt
//INCLUDE /Users/ilanpillemer/git/model-language/src/main/kotlin/DslParser.kt
//INCLUDE /Users/ilanpillemer/git/model-language/src/main/kotlin/BrixApi.kt
//INCLUDE /Users/ilanpillemer/git/model-language/src/main/kotlin/Brix/BrixParser.kt

// also adjust jvm settings
// @file:KotlinOpts("-J-Xmx5g")

USERNAME = "'"$MMDSL_USERNAME"'"
PASSWORD = "'"$MMDSL_PASSWORD"'"
BASEURL = "'"$MMDSL_BASEURL"'"
// define some important variables to be used throughout the dsl
    if (args.size > 0) {
        if (args.contains("-graph-measure")) showGraphMeasure = true
        if (args.contains("-graph-measures")) showGraphMeasure = true
        if (args.contains("-graph-network")) showGraphNetwork = true
        if (args.contains("-graph")) showGraphComplete = true
        if (args.contains("-create-model")) createNewModel = true
        if (args.contains("-syntax-tree")) showSyntaxTree = true
        if (args.contains("-symbol-table")) showSymbolTable = true
        if (args.contains("-symbols")) showKeys = true
        if (args.any({ s -> s.startsWith("-user") })) {
            USERNAME = args.first( { s -> s.startsWith("-user") } ).split("=")[1]
        }
        if (args.any({ s -> s.startsWith("-password") })) {
            PASSWORD = args.first( { s -> s.startsWith("-password") } ).split("=")[1]
        }
        if (args.any({ s -> s.startsWith("-baseurl") })) {
            BASEURL = args.first( { s -> s.startsWith("-baseurl") } ).split("=")[1]
        }
    }

var foo = "bar"
'

~/git/kscript/kscript $@
hj-lee commented 6 years ago

It looks like kscript treats "-" as one of kscript options, not as the script. Below code in Kscript.kt seems to be the culprit.

val userArgs = args.dropWhile { it.startsWith("-") }.drop(1)

Changing it to something like below would fix the issue.

val userArgs = args.dropWhile { it.startsWith("-") && it != "-" }.drop(1)
ilanpillemer commented 6 years ago

I will give that a try this week and see if it works. And if it does put together a pull request