Skip to content

Commit 381921b

Browse files
committed
fixed #59: Kscript just reports usage info if user arguments start with - or --
1 parent e9a6ebb commit 381921b

File tree

3 files changed

+15
-5
lines changed

3 files changed

+15
-5
lines changed

src/main/kotlin/kscript/app/Kscript.kt

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,11 @@ fun main(args: Array<String>) {
5757
quit(0)
5858
}
5959

60-
val docopt = DocOptWrapper(args, USAGE)
60+
// note: with current impt we still don't support `kscript -1` where "-1" is a valid kotlin expression
61+
val userArgs = args.dropWhile { it.startsWith("-") }.drop(1)
62+
val kscriptArgs = args.take(args.size - userArgs.size)
63+
64+
val docopt = DocOptWrapper(kscriptArgs, USAGE)
6165
val loggingEnabled = !docopt.getBoolean("silent")
6266

6367

@@ -239,9 +243,10 @@ fun main(args: Array<String>) {
239243

240244

241245
// print the final command to be run by exec
242-
val shiftedArgs = args.drop(1 + args.indexOfFirst { it == scriptResource }).joinToString(" ")
246+
// val joinedUserArgs = args.drop(1 + args.indexOfFirst { it == scriptResource }).joinToString(" ")
247+
val joinedUserArgs = userArgs.joinToString(" ")
243248

244-
println("kotlin ${kotlinOpts} -classpath ${jarFile}${CP_SEPARATOR_CHAR}${KOTLIN_HOME}${File.separatorChar}lib${File.separatorChar}kotlin-script-runtime.jar${CP_SEPARATOR_CHAR}${classpath} ${execClassName} ${shiftedArgs} ")
249+
println("kotlin ${kotlinOpts} -classpath ${jarFile}${CP_SEPARATOR_CHAR}${KOTLIN_HOME}${File.separatorChar}lib${File.separatorChar}kotlin-script-runtime.jar${CP_SEPARATOR_CHAR}${classpath} ${execClassName} ${joinedUserArgs} ")
245250
}
246251

247252
fun collectDependencies(scriptText: List<String>): List<String> {

src/main/kotlin/org/docopt/DocoptHelper.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import java.io.File
88
*/
99

1010
/** Simple Kotlin facade for org.org.docopt.Docopt.Docopt(java.lang.String) .*/
11-
class DocOptWrapper(args: Array<String>, val usage: String) {
11+
class DocOptWrapper(args: Iterable<String>, val usage: String) {
1212

1313
val parsedArgs = try {
1414
Docopt(usage).withExit(false).parse(args.toList())

test/test_suite.sh

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ assert "kscript ''" ""
3939
assert 'kscript "println(1+1)"' '2'
4040

4141

42+
## use dashed arguments (to prevent regression from https://github.com/holgerbrandl/kscript/issues/59)
43+
assert 'kscript "println(args.joinToString(\"\"))" --arg u ments' '--arguments'
44+
assert 'kscript -s "println(args.joinToString(\"\"))" --arg u ments' '--arguments'
45+
46+
4247
## provide script via stidin
4348
assert "echo 'println(1+1)' | kscript -" "2"
4449

@@ -90,7 +95,7 @@ assert_end script_input_modes
9095
#assert "kscript -i '//DEPS log4j:log4j:1.2.14'" "To create a shell with script dependencies run:\nkotlinc -classpath '${HOME}/.m2/repository/log4j/log4j/1.2.14/log4j-1.2.14.jar'"
9196
#assert "kscript -i <(echo '//DEPS log4j:log4j:1.2.14')" "To create a shell with script dependencies run:\nkotlinc -classpath '${HOME}/.m2/repository/log4j/log4j/1.2.14/log4j-1.2.14.jar'"
9297

93-
assert_end cli_helper_tests
98+
#assert_end cli_helper_tests
9499

95100
########################################################################################################################
96101
## environment_tests

0 commit comments

Comments
 (0)