Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: How to pass cmd line arguments to my carbon app?



On Sep 22, 2005, at 10:01 AM, Jay Vaughan wrote:
Mac OS X provides no user interface other than Terminal for users to
specify command-line arguments. There's simply no way for a user to
provide that info.
then how is XCode doing it, when i set the 'arguments' for my project?

This is a question more appropos to xcode-users, but I'll answer anyway :). Xcode's pseudo-terminal execution environment, both the basic Run command and the gdb shell of the Debug command, runs the command through something similar to this Terminal process:


$ cd /path/to/app/bundle
$ ./Application.app/Contents/MacOS/Application <the arguments you specified>


Users can't do this except by using Terminal themselves. Users shouldn't do this unless your application is specifically designed to be used from Terminal. Command-line arguments simply aren't supposed to be used in GUI programs under MacOS. (Though you'll find plenty that do in the Windows and X11 worlds). It's not user-friendly. I can't imagine why you'd want to do this. If you want to supply configuration options, use preferences. If you want your app to do different things based on which version a user downloaded, make that determination via your own Info.plist key or some other mechanism. To the best of my knowledge, there's no rule against putting your own key/value pairs in an Info.plist as long as they don't conflict with the Apple ones (a Java-style reverse DNS naming scheme such as com.mycompany.mykey is definitely recommended for that, though I have personally found that a unique two-character prefix which is not "NS", "CF", or "LS" suffices and is easier to type :). Also, commandline arguments aren't reliable. Terminal users won't get the ones you define, you can't do it directly via Info.plist, and if you could, powerusers have a tendency to play with commandline arguments when they see them and you're inviting confusion at best that way.

Argc/argv were and are a great way to alter a CLI program's behavior, but this is the GUI age and a GUI OS. The commandline is pure gibberish to the majority of Mac users, in my experience. Don't get caught in the idea that you have to do it that way just because you always have :)

For the record, one way to actually do what you want would be to write a tiny wrapper program as your CFBundleExecutable that does this (written in Mail.app, standard disclaimers apply):

int main( int argc, char **argv )
{
pid_t pid;
CFURLRef resURL = CFBundleCopyResourceURL ( CFBundleGetMainBundle(), CFSTR( "RealApplication", CFSTR( "" ), NULL );


if ( !resURL || ( pid = vfork() ) == -1 )
exit( errno );
else if ( pid == 0 ) // child
{
execl( CFURLCopyFileSystemPath( resURL, kCFURLPOSIXPathStyle ), CFURLCopyFileSystemPath( resURL, kCFURLPOSIXPathStyle ), "--one-argument-here", "--another argument here", "some more arguments!" );
exit( errno );
}
CFRelease( resURL );
return 0;
}


-- Gwynne, daughter of the Code
http://musicimage.plasticchicken.com
"This whole world is an asylum for the incurable."

_______________________________________________
Do not post admin requests to the list. They will be ignored.
Carbon-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/carbon-dev/email@hidden

This email sent to email@hidden
References: 
 >Re: How to pass cmd line arguments to my carbon app? (From: Jay Vaughan <email@hidden>)
 >Re: How to pass cmd line arguments to my carbon app? (From: Chris Espinosa <email@hidden>)
 >Re: How to pass cmd line arguments to my carbon app? (From: Jay Vaughan <email@hidden>)
 >Re: How to pass cmd line arguments to my carbon app? (From: Eric Schlegel <email@hidden>)
 >Re: How to pass cmd line arguments to my carbon app? (From: Jay Vaughan <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.