我有一个程序,它应该引起2档,以使用FileMerge
进行比较。如何强制的命令行参数字符串被引用
这并不工作,但偶尔会失败。我怀疑这是当作为参数传递的路径包含空格字符。
下面的代码片段构建任务并启动它。
NSTask *task = [[NSTask alloc] init];
NSPipe *pipe = [NSPipe pipe];
[task setStandardOutput: pipe];
[task setStandardInput:[NSPipe pipe]]; //The magic line that keeps your log where it belongs
NSFileHandle *file = [pipe fileHandleForReading];
[task setLaunchPath: @"/bin/sh"];
NSArray *arguments = [NSArray arrayWithObjects:
@"-c" ,
[[NSUserDefaults standardUserDefaults] stringForKey:PREF_COMPARE_COMMAND],
@"Compare", // $0 place holder
source,
target,
nil];
[task setArguments:arguments];
[task setEnvironment:[NSDictionary dictionaryWithObject:@"/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin" forKey:@"PATH"]];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(pipeReadCompletionNotification:)
name:NSFileHandleReadCompletionNotification
object:file];
[file readInBackgroundAndNotify];
[task launch];
我已经尝试了很多选项,尝试使用引号将空格或引号括起来,但没有成功。我欢迎任何建议。
典型的参数作为运行的结果是: -
"-c",
"opendiff $1 $2",
Compare,
"/Users/ian/temp/Indian Pacific/RailRes Travel Documentation1.pdf",
"/Users/ian/temp/Indian Pacific/RailRes Travel Documentation.pdf"
我试图
[source stringByReplacingOccurrencesOfString:@" " withString:@"\\ "],
[source stringByReplacingOccurrencesOfString:@" " withString:@"\ "],
的第一实际插入\\
第二产生一个编译错误unknown escape sequence
我试图肯Thomases的建议(知道我的名字没有'
)
[[@"'" stringByAppendingString:source] stringByAppendingString:@"'"],
[[@"'" stringByAppendingString:target] stringByAppendingString:@"'"],
不幸的是这导致了争论
"-c",
"opendiff $1 $2",
Compare,
"'/Users/ian/temp/Indian Pacific/RailRes Travel Documentation1.pdf'",
"'/Users/ian/temp/Indian Pacific/RailRes Travel Documentation.pdf'"
,并以同样的方式失败。 /Users/ian/temp/Indian does not exist
编辑_______________________工作守则_____________________________________
NSArray *arguments = [NSArray arrayWithObjects:
@"-c" ,
[NSString stringWithFormat:@"%@ '%@' '%@'", @"opendiff", source, target],
nil];
您是否尝试用“\”替换“”? – danh