简体   繁体   中英

Can't pass command line arguments to an NSTask running the PHP CLI

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM