Tuesday, May 23, 2017

Swift: Command Line Argument Parsing

Command:

$ cat ./Desktop/Arguments\ Test/Arguments\ Test/main.swift


Result:

import Foundation

var count = 0

for argument in CommandLine.arguments {
    print("Arg[\(count)]: \(argument)")
    count += 1
}


Command:

$ ./Library/Developer/Xcode/DerivedData/Arguments_Test-ffnafkbrsdftjxdawcsyrdhlfnvu/Build/Products/Debug/Arguments\ Test first second third


Result:

Arg[0]: ./Library/Developer/Xcode/DerivedData/Arguments_Test-ffnafkbrsdftjxdawcsyrdhlfnvu/Build/Products/Debug/Arguments Test
Arg[1]: first
Arg[2]: second
Arg[3]: third