Make parseArguments a class method

This commit is contained in:
Corey Johnson & Nathan Sobo
2012-09-12 16:12:17 -07:00
parent 2de35d4098
commit ab18eaa211
2 changed files with 64 additions and 55 deletions

View File

@@ -5,12 +5,13 @@ class AtomCefClient;
@interface AtomApplication : NSApplication <CefAppProtocol, NSApplicationDelegate> {
NSWindowController *_backgroundWindowController;
NSMutableDictionary *_arguments;
NSDictionary *_arguments;
BOOL handlingSendEvent_;
}
+ (id)applicationWithArguments:(char **)argv count:(int)argc;
+ (CefSettings)createCefSettings;
+ (NSDictionary *)parseArguments:(char **)argv count:(int)argc;
- (void)open:(NSString *)path;
- (IBAction)runSpecs:(id)sender;
- (IBAction)runBenchmarks:(id)sender;
@@ -18,6 +19,6 @@ class AtomCefClient;
- (NSDictionary *)arguments;
- (void)runBenchmarksThenExit:(BOOL)exitWhenDone;
@property (nonatomic, readonly) NSDictionary *arguments;
@property (nonatomic, retain) NSDictionary *arguments;
@end

View File

@@ -12,10 +12,66 @@
+ (id)applicationWithArguments:(char **)argv count:(int)argc {
AtomApplication *application = (AtomApplication *)[super sharedApplication];
CefInitialize(CefMainArgs(argc, argv), [self createCefSettings], new AtomCefApp);
[application parseArguments:argv count:argc];
application.arguments = [self parseArguments:argv count:argc];
return application;
}
+ (NSDictionary *)parseArguments:(char **)argv count:(int)argc {
NSMutableDictionary *arguments = [[NSMutableDictionary alloc] init];
// Defaults
#ifdef RESOURCE_PATH
[arguments setObject:[NSString stringWithUTF8String:RESOURCE_PATH] forKey:@"resource-path"];
#endif
int opt;
int longindex;
if (argc > 2 && strcmp(argv[argc - 2], "-NSDocumentRevisionsDebugMode") == 0) { // Because Xcode inserts useless command-line args by default: http://trac.wxwidgets.org/ticket/13732
argc -= 2; // Ignore last two arguments
}
static struct option longopts[] = {
{ "executed-from", optional_argument, NULL, 'K' },
{ "resource-path", optional_argument, NULL, 'r' },
{ "benchmark", optional_argument, NULL, 'b' },
{ "test", optional_argument, NULL, 't' },
{ NULL, 0, NULL, 0 }
};
while ((opt = getopt_long(argc, argv, "r:K:bth?", longopts, &longindex)) != -1) {
NSString *key = [NSString stringWithUTF8String:longopts[longindex].name];
NSString *value = [NSString stringWithUTF8String:optarg];
switch (opt) {
case 'K':
case 'r':
case 'b':
case 't':
[arguments setObject:value forKey:key];
break;
default:
printf("usage: atom [--resource-path=<path>] [<path>]");
}
}
argc -= optind;
argv += optind;
if (argc > 0) {
NSString *path = [NSString stringWithUTF8String:argv[0]];
NSString *executedFromPath =[arguments objectForKey:@"executed-from"];
if (![path isAbsolutePath] && executedFromPath) {
path = [executedFromPath stringByAppendingPathComponent:path];
}
path = [path stringByStandardizingPath];
[arguments setObject:path forKey:@"path"];
}
return arguments;
}
+ (NSString *)supportDirectory {
NSString *cachePath = [NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *executableName = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleExecutable"];
@@ -25,7 +81,7 @@
NSError *error;
BOOL success = [fs createDirectoryAtPath:supportDirectory withIntermediateDirectories:YES attributes:nil error:&error];
if (!success) {
NSLog(@"Can't create support directory '%@' because %@", supportDirectory, [error localizedDescription]);
NSLog(@"Warning: Can't create support directory '%@' because %@", supportDirectory, [error localizedDescription]);
supportDirectory = @"";
}
@@ -70,67 +126,19 @@
[[AtomWindowController alloc] initBenchmarksThenExit:exitWhenDone];
}
- (void)parseArguments:(char **)argv count:(int)argc {
_arguments = [[NSMutableDictionary alloc] init];
// Defaults
#ifdef RESOURCE_PATH
[_arguments setObject:[NSString stringWithUTF8String:RESOURCE_PATH] forKey:@"resource-path"];
#endif
int opt;
int longindex;
if (argc > 2 && strcmp(argv[argc - 2], "-NSDocumentRevisionsDebugMode") == 0) { // Because Xcode inserts useless command-line args by default: http://trac.wxwidgets.org/ticket/13732
argc -= 2; // Ignore last two arguments
}
static struct option longopts[] = {
{ "resource-path", optional_argument, NULL, 'r' },
{ "benchmark", optional_argument, NULL, 'b' },
{ "test", optional_argument, NULL, 't' },
{ NULL, 0, NULL, 0 }
};
while ((opt = getopt_long(argc, argv, "r:bth?", longopts, &longindex)) != -1) {
switch (opt) {
case 'r':
[_arguments setObject:[NSString stringWithUTF8String:optarg] forKey:@"resource-path"];
break;
case 'b':
[_arguments setObject:[NSNumber numberWithBool:YES] forKey:@"benchmark"];
break;
case 't':
[_arguments setObject:[NSNumber numberWithBool:YES] forKey:@"test"];
break;
default:
printf("usage: atom [--resource-path=<path>] [<path>]");
}
}
argc -= optind;
argv += optind;
if (argc > 0) {
[_arguments setObject:[NSString stringWithUTF8String:argv[0]] forKey:@"path"];
}
}
# pragma mark NSApplicationDelegate
- (void)applicationWillFinishLaunching:(NSNotification *)notification {
_backgroundWindowController = [[AtomWindowController alloc] initInBackground];
if ([_arguments objectForKey:@"benchmark"]) {
if ([self.arguments objectForKey:@"benchmark"]) {
[self runBenchmarksThenExit:true];
}
else if ([_arguments objectForKey:@"test"]) {
else if ([self.arguments objectForKey:@"test"]) {
[self runSpecsThenExit:true];
}
else {
NSLog(@"%@", [_arguments objectForKey:@"path"]);
[self open:@"/Users/corey/atom"];//[_arguments objectForKey:@"path"]];
[self open:[self.arguments objectForKey:@"path"]];
}
}