Anthony Mittaz Anthony Mittaz - 4 months ago 18
Swift Question

How do you access command line arguments in Swift?

How do you access command line arguments for a command line application in Swift?

orj orj

Use the top level constants C_ARGC and C_ARGV.

for i in 1..C_ARGC {
    let index = Int(i);

    let arg = String.fromCString(C_ARGV[index])
    switch arg {
    case "this":
        println("this yo");

    case "that":
        println("that yo")

        println("dunno bro")

Note that I'm using the range of 1..C_ARGC because the first element of the C_ARGV "array" is the application's path.

The C_ARGV variable is not actually an array but is sub-scriptable like an array.