How do you access command line arguments in Swift?

前端 未结 6 1747
野趣味
野趣味 2020-12-04 07:02

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

6条回答
  •  庸人自扰
    2020-12-04 07:06

    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")
    
        default:
            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.

提交回复
热议问题