Richard Ibarra
Richard Ibarra

Reputation: 51

Taking decision on command line parameters in a cocoa applications

I have my Cocoa Application, which will be called with or without parameters in the command line.

I would like to take decisions over the parameters received on the application, ie, if a special parameter is received I would like to trigger an action on it. Is there anyway for doing this??

Cheers

Upvotes: 3

Views: 734

Answers (2)

Carl Norum
Carl Norum

Reputation: 224972

Sure, your program has a main() function like any C program. The default one that comes with a new Cocoa project just calls NSApplicationMain(), but you can do other actions if you'd like.

If you want to easily access the command line information from elsewhere in your program, you can use _NSGetArgv(), _NSGetArgc(), _NSGetEnviron(), and _NSGetProgname(). They're declared in crt_externs.h:

extern char ***_NSGetArgv(void);
extern int *_NSGetArgc(void);
extern char ***_NSGetEnviron(void);
extern char **_NSGetProgname(void);

Here's a blog post about these functions, and a link to the NSApplicationMain documentation.

Upvotes: 4

Jeremy W. Sherman
Jeremy W. Sherman

Reputation: 36143

You might find it easier to access the NSArgumentDomain in user defaults:

NSDictionary *const args = [[NSUserDefaults standardUserDefaults]
                             volatileDomainForName:NSArgumentDomain];

This will handle arguments of the form -NSZombieEnabled YES. Other forms of arguments (such as -NSZombieEnabled=YES) might be ignored; I haven't tested or looked at the source.

Upvotes: 2

Related Questions