46

Is there any complete documentation (the interface is present in crt_externs.h) about this functions : _NSGetArgc and _NSGetArgv I can't get any documentation on the apple website about this functions.

3 Answers 3

86

If all you need to do is get the command line arguments in Cocoa, you can do:

NSArray *arguments = [[NSProcessInfo processInfo] arguments];
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks but this is just ok from the main function? not from anywhere in the program like _NSGetArgc and _NSGetArgv can do.
Yes -- from anywhere (I'm not sure what gave you the impression that it might not be OK?).
28

You can also access the commandline arguments using NSUserDefaults as described in the blogposts by Greg Miller or Alex Rozanski.

You basically get an NSUserDefaults instance through a call to [NSUserDefaults standardUserDefaults] and then use messages like boolForKey: or stringForKey: to access the values.

The official Apple documentation can be found here.

4 Comments

I did not notice this answer until after I had found the post by Alex Rozanski on my own. Is any one of the methods described preferred? I am leaning towards using the NSUserDefaults because that way I do not have to parse the arguments and do things like make sure the value was not omitted. I want to make sure there is not some reason to not use teh NSUserDefaults method.
I don't know if there is a preferred method. However, I am using this method to check if a commandline option is present before using it by evaluating if objectForKey: returns nil. Sample code can be found here: github.com/mkroehnert/SaneKit/blob/master/Tools/scantool.m#L79
Those documentation links are broken now. Here's what I found on the topic: developer.apple.com/library/archive/documentation/Cocoa/… Apparently there is an NSArgumentDomain specifically for command-line overrides of user preferences.
On MacOS the high-level APIs are ALWAYS preferred, if they exist and provide you with the functionality you need. Not only they're optimized by Apple (who has full and transparent access to the lower-level implementations) they are also sturdier because if something changes in the lower-level Apple is responsible to keep compatibility.
7

As those functions are prefixed with an "_", that's usually a sign that they are private, and not meant to be used by you. If you need to get the command line arguments, a better way to do it would be to look up NSProcessInfo.

2 Comments

Thanks i was reading this article(unixjunkie.blogspot.com/2006/07/…) and i'm just curious about this functions and their capacity to accessing command line arguments from anywhere.
@Evariste Yes, you can use it anywhere. NSProcess info returns information about the current process.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.