2012-01-17 69 views
4

我希望我在這裏不要求太多。在Objective-C中編寫一個命令行工具,它接受輸入,清除屏幕然後輸出

我想創建一個命令行工具,它將在終端窗口中運行。它將從終端獲取輸入,對字符串進行一些操作,清除屏幕然後輸出字符串。

#import <Foundation/Foundation.h> 
#include <stdlib.h> 

int main (int argc, const char *argv[]) 
{  
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; 
    NSLog (@"Running...."); 

     // take the argument as an NSString 
     // do something with the NSString. 
     // clear the terminal screen. 
     // output the manipulated screen. 

    [pool drain]; 
    return 0; 

} 

這可能嗎?有小費嗎?我想盡可能在​​Objective-C中編碼。

感謝,

EDIT 1 *

只是要清楚,我想連續輸入和程序輸出。換句話說,在可執行文件開始運行之後,有必要輸入數據。不只是最初執行時。

+0

你總是可以調用'EXEC( 「在/ usr/bin中/清除」);'清屏。要接受輸入,可以使用任何C或C++方法,例如'fscanf()'或'std :: cin'。 – user1118321 2012-01-17 14:29:56

回答

4

這是可能的。在創建項目時使用Xcode中的「命令行工具」模板。

一個簡單的例子可以是:

#import <Foundation/Foundation.h> 

int main (int argc, const char * argv[]) 
{ 
    @autoreleasepool { 
     char input[50]; 
     while (true) { 
      // take the argument as an NSString 
      NSLog(@"Enter some text please: "); 
      fgets(input, sizeof input, stdin); 
      NSString *argument = [[NSString stringWithCString:input encoding:NSUTF8StringEncoding] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; 

      // do something with the NSString. 
      NSString *uppercase = [argument uppercaseString]; 

      // clear the terminal screen. 
      system("clear"); 

      // output the manipulated screen. 
      NSLog(@"Hello, %@!", uppercase); 
     } 
    } 
    return 0; 
} 
相關問題