Here's a piece of code to enter a string (either NSString or NSAttributedString) input , which is a command line and parses it into two lines, the cmd command and args arguments:
NSString* cmd = [[input mutableCopy] autorelease]; NSString* args = [[input mutableCopy] autorelease]; NSScanner* scanner = [NSScanner scannerWithString:[input string]]; [scanner scanUpToCharactersFromSet:[NSCharacterSet whitespaceAndNewlineCharacterSet] intoString:&cmd]; if (![scanner scanUpToString:@"magicstring666" intoString:&args]) args = @"";
It seems to work, but the magic line is a rather absurd hack. In addition, Iām not at all sure that I am doing everything correctly with auto implementations.
ADDED: the solution should also be resistant to the original gaps. In addition, I initially had an input string called both input and inStr . Sorry for this confusion.
ADDED: I believe that one of the above rules gives the right to the fact that the answers so far do not correspond to the fact that the arguments should not have leading spaces.
command-line regex objective-c parsing cocoa
dreeves Jan 07 '09 at 21:47 2009-01-07 21:47
source share