可可包装器,用于交互式Unix命令 [英] Cocoa wrapper for an interactive Unix command

查看:96
本文介绍了可可包装器,用于交互式Unix命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,所以我知道您可以创建一个NSTask来使用Objective-C运行命令行工具:

Ok, so I know you can make an NSTask to run command line tools with Objective-C:

NSTask *task;
task = [[NSTask alloc] init];
[task setLaunchPath: @"/usr/bin/gdb"];
[task launch];

我只是想知道是否有一种方法可以与诸如gdb之类的交互式命令行工具进行通信.这将涉及基于用户交互(例如runkillquitgdb)提供命令输入,然后根据其输出的信息做出反应.

I'm just wondering if there's a way to communicate with interactive command line tools such a as gdb. This would involve giving the command inputs based on user interaction (like run, kill or quit with gdb) and then reacting based on the information it outputs.

推荐答案

您可以将NSTask的setStandardInput:setStandardOutput:setStandardError:选择器与NSPipe实例结合使用,以与启动的程序进行通信.

You can use NSTask's setStandardInput:, setStandardOutput: and setStandardError: selectors in conjunction with NSPipe instances to communicate with the launched program.

例如,要读取任务的输出:

For example, to read the task's output:

task = [[NSTask alloc] init];
[task setStandardOutput: [NSPipe pipe]];
[task setStandardError: [task standardOutput]]; // Get standard error output too
[task setLaunchPath: @"/usr/bin/gdb"];
[task launch];

然后,您可以获得一个NSFileHandle实例,该实例可用于读取以下任务的输出:

You can then obtain an NSFileHandle instance that you can use to read the task's output with:

NSFileHandle *readFromMe = [[task standardOutput] fileHandleForReading]; 

要设置用于向gdb发送命令的管道,您需要添加

To set up a pipe for sending commands to gdb, you would add

[task setStandardInput: [NSPipe pipe]];

之前,您启动任务.然后用

before you launch the task. Then you get the NSFileHandle with

NSFileHandle *writeToMe = [[task standardInput] fileHandleForWriting];

这篇关于可可包装器,用于交互式Unix命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆