dongwei9771 2011-08-17 22:01
浏览 51
已采纳

无法将命令行参数传递给运行PHP CLI的NSTask

Edit: [Solved] see below.

I'm launching PHP as an NSTask and trying to pass some command line options to it:

NSTask *php = [[NSTask alloc] init];
[php setLaunchPath: phpPath];
NSArray *args = [[NSArray alloc] initWithObjects: @"-r 'echo 123;'", nil ];
[php setArguments: args];
[php launch];

When I run this from Objective-C I always get the following PHP errors:

For:
@"-r 'echo 123;'"
@"-r \'echo 123;\'"
@"-r \"echo 123;\""
Parse error: syntax error, unexpected $end in Command line code on line 1

For:
@"-r 'echo \'hello\';'" Parse error: syntax error, unexpected T_STRING in Command line code on line 1

For:
@"--run 'echo 123;'"
Usage: php [options] ...

It should be noted that all errors are reported from the PHP interpreter when called from Objective-C, but when I try the exact same option string on the command line, it works as expected.

What's wrong here? how should I write arguments to be correctly passed to the NSTask?

PS: My real goal is to execute some include() lines instead of the echo, which doesn't work either, and instead I get an error of an unexpected Encapsed String.

  • 写回答

1条回答 默认 最新

  • dongzai3139 2011-08-17 22:09
    关注

    Oh I saw it on another question.
    I'm treating -r 'echo 123;' as a single argument, when in fact they're two, it should be:
    NSArray *args = [[NSArray alloc] initWithObjects: @"-r", @"echo 123;", nil ];
    Sorry! and thanks.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 vue router 动态路由问题
  • ¥15 关于#.net#的问题:End Function
  • ¥15 无法import pycausal
  • ¥15 VS2022创建MVC framework提示:预安装的程序包具有对缺少的注册表值的引用
  • ¥15 weditor无法连接模拟器Local server not started, start with?
  • ¥20 6-3 String类定义
  • ¥15 嵌入式--定时器使用
  • ¥20 51单片机学习中的问题
  • ¥30 Windows Server 2016利用兩張網卡處理兩個不同網絡
  • ¥15 Python中knn问题