我有一个Cocoa应用程序,允许用户通过在NSTextBox等UI项目中输入命令来执行shell命令(就像使用终端一样)。应用程序允许有一些预定的参数,这些参数将转换为应用程序内的某些字符。
该应用程序还允许多线程,其中可以同时运行一些命令。
我现在面临的挑战是让用户输入命令,因为它在终端中具有所有空格等。我很难分解用户输入的NSArray元素是要被提供给NSTask的参数。例如,存在一些在其间具有空间的路径或参数,例如, / users / Application Support / testFolder /或 'test - b'
我现在正在做的是我有固定数量的NSTextBoxes,每个NSTextBox只允许1个参数。另一个问题是,如果某个命令的参数数量少于我的总NSTextBox,我必须给参数赋予“”,因此,命令可能无法正确执行。 我想让用户更加灵活地按原样输入。然后,我的应用程序能够处理字符并使用NSTask运行用户输入的命令。
....
paramArray = [cmdParam componentsSeparatedByString:@","];
NSString *launchPath = [paramArray objectAtIndex:0];
if ([launchPath rangeOfString:@"\""].location == NSNotFound) {
int nLineNo = [launchPath integerValue];
launchPath = [self cmdReading:nLineNo uutIndex:nUut];
}
else {
launchPath = [launchPath stringByReplacingOccurrencesOfString:@"\"" withString:@""];
}
NSString *param1 = [paramArray objectAtIndex:1];
if ([param1 rangeOfString:@"\""].location == NSNotFound) {
if ([param1 caseInsensitiveCompare:@"UUT_NUMBER"] == NSOrderedSame) {
param1 = [NSString stringWithFormat:@"UUT%d", nUut+1];
}
else if ([param1 caseInsensitiveCompare:@"GET_UUT_NUMBER"] == NSOrderedSame) {
param1 = [NSString stringWithFormat:@"%d", nUut+1];
}
else if ([param1 length] == 0) {
param1 = @"";
}
else {
int nLineNo = [param1 integerValue];
param1 = [self cmdReading:nLineNo uutIndex:nUut];
}
}
else {
param1 = [param1 stringByReplacingOccurrencesOfString:@"\"" withString:@""];
}
NSString *param2 = [paramArray objectAtIndex:2];
if ([param2 rangeOfString:@"\""].location == NSNotFound) {
if ([param2 caseInsensitiveCompare:@"UUT_NUMBER"] == NSOrderedSame) {
param2 = [NSString stringWithFormat:@"UUT%d", nUut+1];
}
else if ([param2 caseInsensitiveCompare:@"GET_UUT_NUMBER"] == NSOrderedSame) {
param2 = [NSString stringWithFormat:@"%d", nUut+1];
}
else if ([param2 length] == 0) {
param2 = @"";
}
else {
int nLineNo = [param2 integerValue];
param2 = [self cmdReading:nLineNo uutIndex:nUut];
}
}
else {
param2 = [param2 stringByReplacingOccurrencesOfString:@"\"" withString:@""];
}
NSString *param3 = [paramArray objectAtIndex:3];
if ([param3 rangeOfString:@"\""].location == NSNotFound) {
if ([param3 caseInsensitiveCompare:@"UUT_NUMBER"] == NSOrderedSame) {
param3 = [NSString stringWithFormat:@"UUT%d", nUut+1];
}
else if ([param3 caseInsensitiveCompare:@"GET_UUT_NUMBER"] == NSOrderedSame) {
param3 = [NSString stringWithFormat:@"%d", nUut+1];
}
else if ([param3 length] == 0) {
param3 = @"";
}
else {
int nLineNo = [param3 integerValue];
param3 = [self cmdReading:nLineNo uutIndex:nUut];
}
}
else {
param3 = [param3 stringByReplacingOccurrencesOfString:@"\"" withString:@""];
}
NSString *param4 = [paramArray objectAtIndex:4];
if ([param4 rangeOfString:@"\""].location == NSNotFound) {
if ([param4 caseInsensitiveCompare:@"UUT_NUMBER"] == NSOrderedSame) {
param4 = [NSString stringWithFormat:@"UUT%d", nUut+1];
}
else if ([param4 caseInsensitiveCompare:@"GET_UUT_NUMBER"] == NSOrderedSame) {
param4 = [NSString stringWithFormat:@"%d", nUut+1];
}
else if ([param4 length] == 0) {
param4 = @"";
}
else {
int nLineNo = [param4 integerValue];
param4 = [self cmdReading:nLineNo uutIndex:nUut];
}
}
else {
param4 = [param4 stringByReplacingOccurrencesOfString:@"\"" withString:@""];
}
NSString *param5 = [paramArray objectAtIndex:5];
if ([param5 rangeOfString:@"\""].location == NSNotFound) {
if ([param5 caseInsensitiveCompare:@"UUT_NUMBER"] == NSOrderedSame) {
param5 = [NSString stringWithFormat:@"UUT%d", nUut+1];
}
else if ([param5 caseInsensitiveCompare:@"GET_UUT_NUMBER"] == NSOrderedSame) {
param5 = [NSString stringWithFormat:@"%d", nUut+1];
}
else if ([param5 length] == 0) {
param5 = @"";
}
else {
int nLineNo = [param5 integerValue];
param5 = [self cmdReading:nLineNo uutIndex:nUut];
}
}
else {
param5 = [param5 stringByReplacingOccurrencesOfString:@"\"" withString:@""];
}
NSString *param6 = [paramArray objectAtIndex:6];
if ([param6 rangeOfString:@"\""].location == NSNotFound) {
if ([param6 caseInsensitiveCompare:@"UUT_NUMBER"] == NSOrderedSame) {
param6 = [NSString stringWithFormat:@"UUT%d", nUut+1];
}
else if ([param6 caseInsensitiveCompare:@"GET_UUT_NUMBER"] == NSOrderedSame) {
param6 = [NSString stringWithFormat:@"%d", nUut+1];
}
else if ([param6 length] == 0) {
param6 = @"";
}
else {
int nLineNo = [param6 integerValue];
param6 = [self cmdReading:nLineNo uutIndex:nUut];
}
}
else {
param6 = [param6 stringByReplacingOccurrencesOfString:@"\"" withString:@""];
}
NSString *param7 = [paramArray objectAtIndex:7];
if ([param7 rangeOfString:@"\""].location == NSNotFound) {
if ([param7 caseInsensitiveCompare:@"UUT_NUMBER"] == NSOrderedSame) {
param7 = [NSString stringWithFormat:@"UUT%d", nUut+1];
}
else if ([param7 caseInsensitiveCompare:@"GET_UUT_NUMBER"] == NSOrderedSame) {
param7 = [NSString stringWithFormat:@"%d", nUut+1];
}
else if ([param7 length] == 0) {
param7 = @"";
}
else {
int nLineNo = [param7 integerValue];
param7 = [self cmdReading:nLineNo uutIndex:nUut];
}
}
else {
param7 = [param7 stringByReplacingOccurrencesOfString:@"\"" withString:@""];
}
NSLog(@"_Terminal_Command for UUT:%d, launchPath=%@", nUut+1, launchPath);
NSLog(@"_Terminal_Command for UUT:%d, param1=%@", nUut+1, param1);
NSLog(@"_Terminal_Command for UUT:%d, param2=%@", nUut+1, param2);
NSLog(@"_Terminal_Command for UUT:%d, param3=%@", nUut+1, param3);
NSLog(@"_Terminal_Command for UUT:%d, param4=%@", nUut+1, param4);
NSLog(@"_Terminal_Command for UUT:%d, param5=%@", nUut+1, param5);
NSLog(@"_Terminal_Command for UUT:%d, param6=%@", nUut+1, param6);
NSLog(@"_Terminal_Command for UUT:%d, param7=%@", nUut+1, param7);
NSTask* task = [NSTask new];
[task setLaunchPath:launchPath];
[task setArguments:[NSArray arrayWithObjects:param1,
param2, param3,
param4, param5,
param6, param7, nil]];
...