How do you access command line arguments for a command line application in Swift?
Update 01/17/17: Updated the example for Swift 3. Process
has been renamed to CommandLine
.
Update 09/30/2015: Updated the example to work in Swift 2.
It's actually possible to do this without Foundation or C_ARGV
and C_ARGC
.
The Swift standard library contains a struct CommandLine
which has a collection of String
s called arguments
. So you could switch on arguments like this:
for argument in CommandLine.arguments {
switch argument {
case "arg1":
print("first argument")
case "arg2":
print("second argument")
default:
print("an argument")
}
}