Reading command line arguments

Kotlin:
Reading command line arguments

How to:

fun main(args: Array<String>) {
    if (args.isNotEmpty()) {
        println("Hello, ${args[0]}!")
    } else {
        println("Hello, unknown person!")
    }
}

// Sample Output if passed 'Kotlinista' as an argument:
// Hello, Kotlinista!

In the code above, args is an array holding the command line arguments. The main function checks if we got any, and greets accordingly.

Deep Dive

The concept of command line arguments is old as hills; it’s been a part of programming since the dawn of time—or at least since the creation of interactive terminals. In the context of Kotlin, which runs on the JVM, command line arguments work similarly to Java.

Other languages offer similar means, like argv in Python or $argc and $argv in PHP. Kotlin’s approach keeps it simple—the main function just takes an Array<String>.

As for implementation details, remember that array indices start at zero. args[0] is the first argument, args[1] is the second, and so on. Also, bear in mind that if you’re building a complex app that needs to parse commands more flexibly, you might want to look into a dedicated library like kotlinx-cli.

See Also