Skip to content

Instantly share code, notes, and snippets.

@jeremy-w
Created August 2, 2013 23:50
Show Gist options
  • Save jeremy-w/6144348 to your computer and use it in GitHub Desktop.
Save jeremy-w/6144348 to your computer and use it in GitHub Desktop.
//cc -Wall -framework Foundation nsargs.m -o nsargs
/* @author Jeremy W. Sherman
* @date 2013-08-02
*
* Demonstrates NSArgumentDomain.
*
* Try:
* ./nsargs -a YES -foo fishy -x 3
*
* 2013-08-02 19:49:35.331 nsargs[48904:707] nsargs: command-line args:
* a <- YES (__NSCFConstantString)
* foo <- fishy (__NSCFString)
* x <- 3 (__NSCFConstantString)
*/
#import <Foundation/Foundation.h>
int
main(void)
{
NSDictionary *args = [[NSUserDefaults standardUserDefaults]
volatileDomainForName:NSArgumentDomain];
NSMutableArray *items = [NSMutableArray new];
for (NSString *key in args) {
id arg = args[key];
NSString *item = [NSString stringWithFormat:@" %@ <- %@ (%@)",
key, arg, [arg class]];
[items addObject:item];
}
NSString *argsAlt = [items componentsJoinedByString:@"\n"];
/* Note how they all come through as NSStrings. */
NSLog(@"%@: command-line args:\n%@",
[NSProcessInfo processInfo].processName, argsAlt);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment