zneak
zneak

Reputation: 138081

How do I access program arguments in Swift?

C and derivatives have argc and argv (and envp) parameters to their entry point functions, but Swift doesn't have one proper: top-level code is just code and it doesn't have parameters.

How can one access the equivalent of argc and argv in a Swift program?

Upvotes: 44

Views: 20894

Answers (6)

Rob
Rob

Reputation: 437632

An elegant alternative to CommandLine.arguments is the Swift Argument Parser. ArgumentParser will do the parsing of the command line arguments for you, mapping the arguments into a struct. It provides all the features you’d expect from a command line app including type safe parsing of arguments, automatic help features, short and long options, etc.

Just use the “Swift Package Manager” to add the ArgumentParser package to your command line project (e.g., in Xcode’s “File” » “Add Package” command).

Upvotes: 2

ambientlight
ambientlight

Reputation: 7332

Process was just renamed into CommandLine (since Swift 3.0 August 4 snapshot)

let arguments = CommandLine.arguments

(for some reason this wasn't mentioned on the changelog)

Upvotes: 55

Darrarski
Darrarski

Reputation: 4032

For Swift 3 you can use this code:

let argc = CommandLine.argc
let argv = UnsafeMutableRawPointer(CommandLine.unsafeArgv).bindMemory(to: UnsafeMutablePointer<Int8>.self, capacity: Int(CommandLine.argc))

which is equivalent of argc and argv parameters used in Objective-C main function:

int main(int argc, char *argv[])

For older versions of Swift, you can use Process.argc and Process.unsafeArgv or C_ARGC and C_ARGV.

You can pass this variables to UIApplicationMain function in iOS app:

Swift 3:

let argc = CommandLine.argc
let argv = UnsafeMutableRawPointer(CommandLine.unsafeArgv).bindMemory(to: UnsafeMutablePointer<Int8>.self, capacity: Int(CommandLine.argc))
UIApplicationMain(argc, argv, nil, NSStringFromClass(AppDelegate.self))

previous Swift versions:

UIApplicationMain(Process.argc, Process.unsafeArgv, nil, NSStringFromClass(AppDelegate.self))

or:

UIApplicationMain(C_ARGC, C_ARGC, nil, NSStringFromClass(AppDelegate.self))

Objective-C:

int main(int argc, char *argv[])
{
    @autoreleasepool {
        return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
    }
}

Upvotes: 11

Kevin Frost
Kevin Frost

Reputation: 798

Process.arguments is your friend!

Fortunately this is much easier, and built in: no importing anything, no getting your hands dirty with C, objective or otherwise.

Consider this, let's call it args.swift:

Swift 2 version:

var c = 0;
for arg in Process.arguments {
    println("argument \(c) is: \(arg)")
    c++
}

Swift 3 version:

var c = 0;
for arg in CommandLine.arguments {
    print("argument \(c) is: \(arg)")
    c += 1
}

We can compile and run it like this:

$ swift -o args args.swift && ./args fee fi fo fum
argument 0 is: ./args
argument 1 is: fee
argument 2 is: fi
argument 3 is: fo
argument 4 is: fum

Note that the first argument is the program name, as you might expect.

It seems every argument is a String, as you might also expect.

I hope very much that Process becomes more useful as Swift matures, but right now it seems to only give you the arguments. Which is a lot, if you're trying to write a pure-Swift program.

Upvotes: 39

Daij-Djan
Daij-Djan

Reputation: 50099

As soon as your app is up I'd use the process info:

let args = NSProcessInfo.processInfo().arguments
print(args)

Nothing unsafe there, very convenient.

Note that you have to import Foundation (or Cocoa / UIKit).

Upvotes: 13

Waruna
Waruna

Reputation: 1174

     import Foundation


     println(C_ARGC) //CInt    
     println(C_ARGV) // CString

As in the above code, you can use C_ARGC to get number of arguments. C_ARGV to get this arguments.

Upvotes: 0

Related Questions