Permalink
Browse files

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

The file's called .mogenerator-args, format is a simple JSON array. A
typical example might be:

    [
        "--template-var", "arc=true",
        "--model", "MyApp.xcdatamodeld/MyApp.xcdatamodel",
        "--machine-dir", "Model/machine",
        "--human-dir", "Model"
    ]
  • Loading branch information...
1 parent dba7cff commit f2b506377494a79640a0113f7f4a7a36124aba8a Simon Whitaker committed May 30, 2013
Showing with 39 additions and 0 deletions.
  1. +10 −0 ddcli/DDGetoptLongParser.h
  2. +28 −0 ddcli/DDGetoptLongParser.m
  3. +1 −0 mogenerator.m
@@ -85,6 +85,7 @@ typedef struct
int mCurrentOption;
NSMutableArray * mUtf8Data;
DDGetoptFunction mGetoptFunction;
+ NSString *mArgumentsFilename;
}
/**
@@ -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
/**
@@ -73,6 +73,7 @@ - (void) dealloc
[mOptionString release];
[mOptionsData release];
[mUtf8Data release];
+ [mArgumentsFilename release];
[super dealloc];
}
@@ -87,6 +88,10 @@ - (void) setTarget: (id) target
mTarget = target;
}
+- (void) setArgumentsFilename:(NSString *)filename
+{
+ mArgumentsFilename = [filename copy];
+}
- (void) setGetoptLongOnly: (BOOL) getoptLongOnly
{
@@ -163,6 +168,29 @@ - (NSArray *) parseOptions
{
NSProcessInfo * processInfo = [NSProcessInfo processInfo];
NSArray * arguments = [processInfo arguments];
+
+ if (mArgumentsFilename != nil) {
+ 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];
}
View
@@ -608,6 +608,7 @@ - (void)application:(DDCliApplication*)app
{nil, 0, 0},
};
[optionsParser addOptionsFromTable:optionTable];
+ [optionsParser setArgumentsFilename:@".mogenerator-args"];
}
- (void)printUsage {

0 comments on commit f2b5063

Please sign in to comment.