Skip to content

Commit 717ecba

Browse files
committed
Merge pull request rentzsch#163 from simonwhitaker/add-dotfile-support
[NEW] Add support for setting command-line options via a JSON config file. (Simon Whitaker)
2 parents dd28322 + 12fd8ab commit 717ecba

File tree

3 files changed

+43
-0
lines changed

3 files changed

+43
-0
lines changed

ddcli/DDGetoptLongParser.h

+10
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ typedef struct
8585
int mCurrentOption;
8686
NSMutableArray * mUtf8Data;
8787
DDGetoptFunction mGetoptFunction;
88+
NSString *mArgumentsFilename;
8889
}
8990

9091
/**
@@ -176,6 +177,15 @@ typedef struct
176177
- (NSArray *) parseOptionsWithArguments: (NSArray *) arguments
177178
command: (NSString *) command;
178179

180+
/**
181+
* If set, provides the name of a file, located in the current working
182+
* directory, containing command-line arguments in a simple JSON array
183+
*
184+
* @param filename Name of the file to look for in the current working directory
185+
*/
186+
187+
- (void)setArgumentsFilename:(NSString*)filename;
188+
179189
@end
180190

181191
/**

ddcli/DDGetoptLongParser.m

+32
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ - (void) dealloc
7373
[mOptionString release];
7474
[mOptionsData release];
7575
[mUtf8Data release];
76+
[mArgumentsFilename release];
7677

7778
[super dealloc];
7879
}
@@ -87,6 +88,10 @@ - (void) setTarget: (id) target
8788
mTarget = target;
8889
}
8990

91+
- (void) setArgumentsFilename:(NSString *)filename
92+
{
93+
mArgumentsFilename = [filename copy];
94+
}
9095

9196
- (void) setGetoptLongOnly: (BOOL) getoptLongOnly
9297
{
@@ -163,6 +168,33 @@ - (NSArray *) parseOptions
163168
{
164169
NSProcessInfo * processInfo = [NSProcessInfo processInfo];
165170
NSArray * arguments = [processInfo arguments];
171+
172+
if (mArgumentsFilename != nil) {
173+
if (NSClassFromString(@"NSJSONSerialization") == nil) {
174+
fprintf(stderr, "Warning: ignoring %s, feature supported from OS X 10.7 onwards\n", [mArgumentsFilename UTF8String]);
175+
} else {
176+
NSFileManager *fm = [NSFileManager defaultManager];
177+
NSString *argumentsFilePath = [[fm currentDirectoryPath] stringByAppendingPathComponent:mArgumentsFilename];
178+
if ([fm fileExistsAtPath:argumentsFilePath]) {
179+
NSError *error;
180+
NSArray *argumentsFromFile = [NSJSONSerialization JSONObjectWithData:[NSData dataWithContentsOfFile:argumentsFilePath] options:0 error:&error];
181+
if (argumentsFromFile != nil) {
182+
NSAssert([arguments count] > 0, @"Process has no arguments (not even the command). Weird.");
183+
NSString *command = [arguments objectAtIndex:0];
184+
arguments = [arguments subarrayWithRange:NSMakeRange(1, [arguments count] - 1)];
185+
186+
NSMutableArray *mutableArguments = [NSMutableArray arrayWithObject:command];
187+
[mutableArguments addObjectsFromArray:argumentsFromFile];
188+
[mutableArguments addObjectsFromArray:arguments];
189+
arguments = [NSArray arrayWithArray:mutableArguments];
190+
} else {
191+
fprintf(stderr, "Error reading %s: %s\n", [mArgumentsFilename UTF8String], [[error localizedDescription] UTF8String]);
192+
exit(1);
193+
}
194+
}
195+
}
196+
}
197+
166198
NSString * command = [processInfo processName];
167199
return [self parseOptionsWithArguments: arguments command: command];
168200
}

mogenerator.m

+1
Original file line numberDiff line numberDiff line change
@@ -608,6 +608,7 @@ - (void)application:(DDCliApplication*)app
608608
{nil, 0, 0},
609609
};
610610
[optionsParser addOptionsFromTable:optionTable];
611+
[optionsParser setArgumentsFilename:@".mogenerator-args"];
611612
}
612613

613614
- (void)printUsage {

0 commit comments

Comments
 (0)