On Jul 29, 2009, at 8:22 PM, Bill Bumgarner wrote:

On Jul 29, 2009, at 7:09 PM, DeNigris Sean wrote:
When using NSUserDefaults to get command line arguments, it doesn't seem to handle negative numbers correctly.
In main.m:
NSUserDefaults *args = [NSUserDefaults standardUserDefaults];
                
int x = [args integerForKey:@"x"];
int y = [args integerForKey:@"y"];
If the command line is "MyApp -x -100 -y 100", NSUserDefaults does not recognize the -100 as the value of the x argument - it sets x to 0. If the '-' is removed, everything is fine. Is this a bug? Is there a way around?

Yes-- -100 is parsed as an argument, not as a value to the previous argument.

So, no, it isn't a bug. It is behaving correctly, for some values of correct. Welcome to shell programming & the interface between shell & process. Fragile space. Coder beware.

If you are writing a command line tool, you'll want to use a parsing API that actually lets you specify arguments more completely.

man 3 getopt

That'll give you a start. But I'd recommend Dave Dribin's DDCLI stuff; http://www.dribin.org/dave/blog/archives/2008/04/29/ddcli/

If this is a Cocoa application, you'll want to do the command line parsing *before* NSApplicationMain() is called as it'll munch the arguments beyond recovery IIRC.

b.bum
_______________________________________________

Cocoa-dev mailing list ([email protected])

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [email protected]

Reply via email to