Skip to content

Commit

Permalink
Merge pull request rentzsch#163 from simonwhitaker/add-dotfile-support
Browse files Browse the repository at this point in the history
[NEW] Add support for setting command-line options via a JSON config file. (Simon Whitaker)
  • Loading branch information
rentzsch committed May 31, 2013
2 parents dd28322 + 12fd8ab commit 717ecba
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
10 changes: 10 additions & 0 deletions ddcli/DDGetoptLongParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ typedef struct
int mCurrentOption;
NSMutableArray * mUtf8Data;
DDGetoptFunction mGetoptFunction;
NSString *mArgumentsFilename;
}

/**
Expand Down Expand Up @@ -176,6 +177,15 @@ typedef struct
- (NSArray *) parseOptionsWithArguments: (NSArray *) arguments
command: (NSString *) command;

/**
* If set, provides the name of a file, located in the current working
* directory, containing command-line arguments in a simple JSON array
*
* @param filename Name of the file to look for in the current working directory
*/

- (void)setArgumentsFilename:(NSString*)filename;

@end

/**
Expand Down
32 changes: 32 additions & 0 deletions ddcli/DDGetoptLongParser.m
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ - (void) dealloc
[mOptionString release];
[mOptionsData release];
[mUtf8Data release];
[mArgumentsFilename release];

[super dealloc];
}
Expand All @@ -87,6 +88,10 @@ - (void) setTarget: (id) target
mTarget = target;
}

- (void) setArgumentsFilename:(NSString *)filename
{
mArgumentsFilename = [filename copy];
}

- (void) setGetoptLongOnly: (BOOL) getoptLongOnly
{
Expand Down Expand Up @@ -163,6 +168,33 @@ - (NSArray *) parseOptions
{
NSProcessInfo * processInfo = [NSProcessInfo processInfo];
NSArray * arguments = [processInfo arguments];

if (mArgumentsFilename != nil) {
if (NSClassFromString(@"NSJSONSerialization") == nil) {
fprintf(stderr, "Warning: ignoring %s, feature supported from OS X 10.7 onwards\n", [mArgumentsFilename UTF8String]);
} else {
NSFileManager *fm = [NSFileManager defaultManager];
NSString *argumentsFilePath = [[fm currentDirectoryPath] stringByAppendingPathComponent:mArgumentsFilename];
if ([fm fileExistsAtPath:argumentsFilePath]) {
NSError *error;
NSArray *argumentsFromFile = [NSJSONSerialization JSONObjectWithData:[NSData dataWithContentsOfFile:argumentsFilePath] options:0 error:&error];
if (argumentsFromFile != nil) {
NSAssert([arguments count] > 0, @"Process has no arguments (not even the command). Weird.");
NSString *command = [arguments objectAtIndex:0];
arguments = [arguments subarrayWithRange:NSMakeRange(1, [arguments count] - 1)];

NSMutableArray *mutableArguments = [NSMutableArray arrayWithObject:command];
[mutableArguments addObjectsFromArray:argumentsFromFile];
[mutableArguments addObjectsFromArray:arguments];
arguments = [NSArray arrayWithArray:mutableArguments];
} else {
fprintf(stderr, "Error reading %s: %s\n", [mArgumentsFilename UTF8String], [[error localizedDescription] UTF8String]);
exit(1);
}
}
}
}

NSString * command = [processInfo processName];
return [self parseOptionsWithArguments: arguments command: command];
}
Expand Down
1 change: 1 addition & 0 deletions mogenerator.m
Original file line number Diff line number Diff line change
Expand Up @@ -608,6 +608,7 @@ - (void)application:(DDCliApplication*)app
{nil, 0, 0},
};
[optionsParser addOptionsFromTable:optionTable];
[optionsParser setArgumentsFilename:@".mogenerator-args"];
}

- (void)printUsage {
Expand Down

0 comments on commit 717ecba

Please sign in to comment.