How do you access command line arguments for a command line application in Swift?
-
2Possible duplicate of [How do I access program arguments in Swift?](http://stackoverflow.com/questions/24009050/how-do-i-access-program-arguments-in-swift) – Cœur Apr 17 '17 at 05:56
6 Answers
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")
}
}

- 35,668
- 12
- 125
- 132

- 30,776
- 11
- 77
- 77
-
If you want an array this construct is quite handy ```let args = [String](Process.arguments)``` – Albin Stigo Jan 05 '15 at 18:11
-
11@AlbinStigo Process.arguments is already an array of strings, no need to make a new one. – Lance Mar 03 '15 at 19:03
-
10
-
`println` has been changed to `print` recently, but indeed this is the best answer. – juandesant Sep 29 '15 at 08:44
-
5If anyone besides me cares, Process is actually an [enumeration](https://developer.apple.com/library/ios/documentation/Swift/Reference/Swift_Process_Enumeration/index.html). – robobrobro Dec 08 '15 at 17:42
-
1Is `Process.arguments` the same as `NSProcessInfo.processInfo().arguments`? – Franklin Yu Jun 07 '16 at 07:07
-
5In the most recent Swift snapshots (either the 7/28 snapshot or the 7/29 snapshot), the `Process` object is now known as the `CommandLine` object. This will probably be fully incorporated once Swift 3.0 is officially released. – TheSoundDefense Aug 08 '16 at 16:50
-
-
This example code makes no sense. Accessing CommandLine.arguments gives you an array of string, and the number of arguments is the array.count. As always, argument 0 is always present and is the name of the app. – w0mbat May 12 '23 at 20:48
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.

- 5,629
- 3
- 36
- 48

- 13,234
- 14
- 63
- 73
-
4You can also use NSProcessInfo, just like you do in Objective-C. – Jack Lawrence Jun 04 '14 at 05:30
-
3NSProcessInfo requires Foundation. My answer doesn't require Foundation. Just uses swift lang standard lib. – orj Jun 04 '14 at 05:33
-
7
-
2I can confirm that C_ARG no longer works with the latest version of the tools, XCode Version 7.1 (7B91b). – svth Dec 17 '15 at 22:30
-
1Use of unresolved identifier 'C_ARGC', not supported anymore on Xcode 7.3 – Honghao Z May 09 '16 at 13:52
-
1Operator `..` is not defined for ranges, it's either `...` or `..<` – Alonso Urbano Jul 11 '16 at 14:36
-
8You can instead use `Process.argc` and `Process.arguments` for this, though it looks like this might be changing to `CommandLine.argc` and `CommandLine.arguments` with the most recent changes to the language. – TheSoundDefense Aug 08 '16 at 16:51
-
Apple has released the ArgumentParser
library for doing just this:
We’re delighted to announce
ArgumentParser
, a new open-source library that makes it straightforward — even enjoyable! — to parse command-line arguments in Swift.
Swift Argument Parser
https://github.com/apple/swift-argument-parser
Begin by declaring a type that defines the information you need to collect from the command line. Decorate each stored property with one of
ArgumentParser
's property wrappers, and declare conformance toParsableCommand
.The
ArgumentParser
library parses the command-line arguments, instantiates your command type, and then either executes your customrun()
method or exits with useful a message.
Anyone who wants to use the old "getopt" (which is available in Swift) can use this as reference. I made a Swift port of the GNU example in C one can find at:
http://www.gnu.org/software/libc/manual/html_node/Example-of-Getopt.html
with a full description. It's tested and fully functional. It doesn't require Foundation either.
var aFlag = 0
var bFlag = 0
var cValue = String()
let pattern = "abc:"
var buffer = Array(pattern.utf8).map { Int8($0) }
while true {
let option = Int(getopt(C_ARGC, C_ARGV, buffer))
if option == -1 {
break
}
switch "\(UnicodeScalar(option))"
{
case "a":
aFlag = 1
println("Option -a")
case "b":
bFlag = 1
println("Option -b")
case "c":
cValue = String.fromCString(optarg)!
println("Option -c \(cValue)")
case "?":
let charOption = "\(UnicodeScalar(Int(optopt)))"
if charOption == "c" {
println("Option '\(charOption)' requires an argument.")
} else {
println("Unknown option '\(charOption)'.")
}
exit(1)
default:
abort()
}
}
println("aflag ='\(aFlag)', bflag = '\(bFlag)' cvalue = '\(cValue)'")
for index in optind..<C_ARGC {
println("Non-option argument '\(String.fromCString(C_ARGV[Int(index)])!)'")
}

- 1,474
- 2
- 15
- 25
You could create an argument parser by using the CommandLine.arguments
Array and add any logic you like.
You can test it. Create a file arguments.swift
//Remember the first argument is the name of the executable
print("you passed \(CommandLine.arguments.count - 1) argument(s)")
print("And they are")
for argument in CommandLine.arguments {
print(argument)
}
compile it and run it:
$ swiftc arguments.swift
$ ./arguments argument1 argument2 argument3
The issue with you building your own argument parser is taking into account all the command-line argument conventions. I would recommend using an existing Argument Parser.
You could use:
- Vapor's Console module
- TSCUtility Argument Parser used by the Swift Package manager
- The Swift Argument Parser open-sourced by Apple
I've written about how to build command-line tools on all three. You should check them out and decide what style suits you best.
If you are interested here are the links:

- 460
- 5
- 18