Title here
Summary here
Command-line arguments are a common way to parameterize execution of programs. For example, swift run hello.swift uses run and hello.swift arguments to the swift program.
import Foundation
func main() {
// CommandLine.arguments provides access to raw command-line
// arguments. Note that the first value in this array
// is the path to the program, and CommandLine.arguments[1...]
// holds the arguments to the program.
let argsWithProg = CommandLine.arguments
let argsWithoutProg = Array(CommandLine.arguments.dropFirst())
// You can get individual args with normal indexing.
let arg = CommandLine.arguments[3]
print(argsWithProg)
print(argsWithoutProg)
print(arg)
}
main()To experiment with command-line arguments it’s best to build an executable first.
$ swiftc command-line-arguments.swift -o command-line-arguments
$ ./command-line-arguments a b c d
[./command-line-arguments, a, b, c, d]
[a, b, c, d]
cNext we’ll look at more advanced command-line processing with argument parsing libraries.