Skip to content

Commit

Permalink
Refactor run_command to use blocks.
Browse files Browse the repository at this point in the history
This cleans up redudant code between commands, co-locating command-specific
behaviour and command-agnostic structure.
  • Loading branch information
Schoonology committed May 20, 2016
1 parent be03401 commit 4bf06de
Showing 1 changed file with 40 additions and 69 deletions.
109 changes: 40 additions & 69 deletions main.m
Original file line number Diff line number Diff line change
Expand Up @@ -80,80 +80,51 @@ void open_url(NSString *url) {
* For more information on the desired behaviour of commands, see the README.
*/
int run_command(NSString *command, NSArray<NSString *> *args) {
if ([@"default" isEqualToString:command]) {
if ([args count] < 1) {
error(@"Error: \"default\" command requires a BROWSER argument.");
return 1;
} else if ([args count] > 1) {
error(@"Error: Too many arguments for \"default\" command.");
return 1;
}

[settings setObject:args[0] forKey:kDefaultAppNameKey];
[settings writeToFile:kSettingsFileName atomically:FALSE];

return 0;
}

if ([@"add" isEqualToString:command]) {
if ([args count] < 2) {
error(@"Error: \"add\" command requires both RULE and BROWSER arguments.");
return 1;
} else if ([args count] > 2) {
error(@"Error: Too many arguments for \"add\" command.");
return 1;
}

[settings setObject:args[1] forKey:args[0]];
[settings writeToFile:kSettingsFileName atomically:FALSE];

return 0;
}

if ([@"remove" isEqualToString:command]) {
if ([args count] < 1) {
error(@"Error: \"remove\" command requires a RULE argument.");
return 1;
} else if ([args count] > 1) {
error(@"Error: Too many arguments for \"remove\" command.");
return 1;
}

[settings removeObjectForKey:args[0]];
[settings writeToFile:kSettingsFileName atomically:FALSE];

NSDictionary *commands = @{
@"default": @[@[@"BROWSER"], ^(NSArray<NSString *> *args) {
[settings setObject:args[0] forKey:kDefaultAppNameKey];
[settings writeToFile:kSettingsFileName atomically:FALSE];
}],
@"add": @[@[@"RULE", @"BROWSER"], ^(NSArray<NSString *> *args) {
[settings setObject:args[1] forKey:args[0]];
[settings writeToFile:kSettingsFileName atomically:FALSE];
}],
@"remove": @[@[@"RULE"], ^(NSArray<NSString *> *args) {
[settings removeObjectForKey:args[0]];
[settings writeToFile:kSettingsFileName atomically:FALSE];
}],
@"list": @[@[], ^(NSArray<NSString *> *args) {
printf("Rules:\n");
[settings enumerateKeysAndObjectsUsingBlock:^(NSString *rule, NSString *browser, BOOL *stop) {
printf(" %s => %s\n", [rule UTF8String], [browser UTF8String]);
}];
}],
@"quit": @[@[], ^(NSArray<NSString *> *args) {
int pid = [[settings objectForKey:kProcessIdentifierKey] intValue];
if (pid) {
kill(pid, SIGHUP);
}
}],
};

id pair = [commands objectForKey:command];
int desiredCount = [[pair firstObject] count];
int countDiff = [args count] - desiredCount;
void (^block)(NSArray *) = [pair lastObject];

if (pair && countDiff == 0) {
block(args);
return 0;
}

if ([@"list" isEqualToString:command]) {
if ([args count] > 0) {
error(@"Error: No arguments allowed for \"list\" command.");
return 1;
}

printf("Rules:\n");
[settings enumerateKeysAndObjectsUsingBlock:^(NSString *rule, NSString *browser, BOOL *stop) {
printf(" %s => %s\n", [rule UTF8String], [browser UTF8String]);
}];

return 0;
}

if ([@"quit" isEqualToString:command]) {
if ([args count] > 0) {
error(@"Error: No arguments allowed for \"quit\" command.");
return 1;
}

int pid = [[settings objectForKey:kProcessIdentifierKey] intValue];
if (pid) {
kill(pid, SIGHUP);
}

return 0;
if (!pair) {
error(@"Error: \"%@\" is not a valid Browter command.", command);
} else if (countDiff > 0) {
error(@"Error: Too many arguments for \"%@\" command.", command);
} else if (countDiff < 0) {
error(@"Error: \"%@\" command requires %@ arguments.", command, desiredCount);
}

error(@"Error: \"%@\" is not a valid Browter command.", command);
return 1;
}

Expand Down

0 comments on commit 4bf06de

Please sign in to comment.