How do you access command line arguments for a command line application in Swift?
相关问题
- “Zero out” sensitive String data in Swift
- SwiftUI: UIImage (QRCode) does not load after call
- Get the NSRange for the visible text after scroll
- UIPanGestureRecognizer is not working in iOS 13
- What does a Firebase observer actually do?
相关文章
- 现在使用swift开发ios应用好还是swift?
- Visual Studio Code, MAC OS X, OmniSharp server is
- Using if let syntax in switch statement
- xcode 4 garbage collection removed?
- IntelliJ IDEA can't open projects or add SDK o
- Automator: How do I use the Choose from List actio
- Enum with associated value conforming to CaseItera
- Swift - hide pickerView after value selected
Update 01/17/17: Updated the example for Swift 3.
Process
has been renamed toCommandLine
.Update 09/30/2015: Updated the example to work in Swift 2.
It's actually possible to do this without Foundation or
C_ARGV
andC_ARGC
.The Swift standard library contains a struct
CommandLine
which has a collection ofString
s calledarguments
. So you could switch on arguments like this: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.
Use the top level constants
C_ARGC
andC_ARGV
.Note that I'm using the range of
1..C_ARGC
because the first element of theC_ARGV
"array" is the application's path.The
C_ARGV
variable is not actually an array but is sub-scriptable like an array.In Swift 3 use
CommandLine
enum instead ofProcess
So: