在 Objective-C 中编写一个命令行工具,它接受输入、清除屏幕然后输出

发布于 2024-12-27 14:13:35 字数 675 浏览 0 评论 0原文

我希望我在这里没有要求太多。

我想创建一个将在终端窗口中运行的命令行工具。它将从终端获取输入,对字符串执行某些操作,清除屏幕,然后输出字符串。

#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 来编码。

谢谢,

编辑1*

需要明确的是,我想从程序中不断输入和输出。换句话说,在可执行文件开始运行后需要输入数据。不仅仅是在它最初执行时。

I hope I'm not asking for too much here.

I would like to create a command-line tool that will run in a terminal window. It will take input from the terminal, do something with the string, clear the screen and then output strings.

#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;

}

Is this possible? Any tips? I would like to be coding this as much as possible in Objective-C.

Thanks,

EDIT 1*

Just to be clear, I would like to be continuously inputting and outputting from the program. In other words, it would be necessary to enter data after the executable has started running. Not just when it initially executes.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

没有心的人 2025-01-03 14:13:35

这是可能的。创建项目时,使用 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;
}

This is possible. Use the "Command Line Tool" template in Xcode when you create your project.

A quick example could be:

#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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文