Title here
Summary here
Command-line arguments are a common way to parameterize execution of programs. For example, kotlin HelloWorld.kt
uses HelloWorld.kt
as an argument to the kotlin
command.
To experiment with command-line arguments it’s best to compile the Kotlin file to a JAR first.
In this Kotlin version:
args: Array<String>
in the main
function to access command-line arguments.System.getProperty("sun.java.command")
, which gives us the JAR file name when executed.argsWithProg
by combining the program name with the arguments.argsWithoutProg
is simply the args
converted to a list.args
before accessing the third argument to avoid an IndexOutOfBoundsException
.Note that in Kotlin, we don’t need to explicitly import standard library functions like println
, as they’re available by default.
Next, we’ll look at more advanced command-line processing with command-line option parsing libraries in Kotlin.