diff --git a/ddcli/DDGetoptLongParser.h b/ddcli/DDGetoptLongParser.h index a0c09a6e..f37a0922 100644 --- a/ddcli/DDGetoptLongParser.h +++ b/ddcli/DDGetoptLongParser.h @@ -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 /** diff --git a/ddcli/DDGetoptLongParser.m b/ddcli/DDGetoptLongParser.m index de3962bc..29d4f3d4 100644 --- a/ddcli/DDGetoptLongParser.m +++ b/ddcli/DDGetoptLongParser.m @@ -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,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]; } diff --git a/mogenerator.m b/mogenerator.m index a977c850..22b4c8e1 100644 --- a/mogenerator.m +++ b/mogenerator.m @@ -608,6 +608,7 @@ - (void)application:(DDCliApplication*)app {nil, 0, 0}, }; [optionsParser addOptionsFromTable:optionTable]; + [optionsParser setArgumentsFilename:@".mogenerator-args"]; } - (void)printUsage {