Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for setting command-line options via a JSON config file #163

Merged
merged 2 commits into from
May 31, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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