5

I have a windows batch

@echo off

setlocal
  call kotlinc-jvm -cp "%~dp0\lib\commons-cli-1.3.1.jar" -script "%~dp0\RmMvnRepo.kts" %*
endlocal

If I pass "-h" option to the batch, kotlinc-jvm own's help will display. But I want my RmMvnRepo.kts to receive the option. How to do that?


EDIT: No need to answer this question anymore. I've found a bug in kotlinc related to this.

3
  • Can you provide the YouTrack issue tracking the bug? Commented Dec 20, 2015 at 19:39
  • @mfulton26 youtrack.jetbrains.com/issue/KT-9370 Commented Dec 21, 2015 at 2:17
  • You might post that link as an answer to your own question. That way you can accept it and this question will no longer show up as "unanswered", etc. Thanks for the link! Commented Dec 27, 2015 at 4:26

2 Answers 2

7

Think of your code wrapped in main function. Then you can access arguments through args array.

Call the script with arguments
kotlinc -script ...kts "option1" "option2"
Access the arguments through args variable
println(args.size) // will output 2

Update: Try quotes to pass -h parameter

kotlinc -script ...kts "%*"
Sign up to request clarification or add additional context in comments.

2 Comments

There are some typos here. And the length of an array is called size.
Via discuss.kotlinlang.org/t/… and github.com/JetBrains/kotlin/blob/master/compiler/testData/… this the correct answer to the question "How to accept kotlin script arguments" )
3

As @mfulton suggested, this issue describes the problem, so I will close this question.

From a comment on that ticket:

This will be possible in 1.1.3 using the "--" delimiter for the compiler arguments and the rest (free arguments):

And the comment's example usage of --:

kotlinc -script foo.kts -- -arg value

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.