简体   繁体   English

如何在CLI中将命令和选项传递给PHP脚本(例如php file.php mycommand -d“ my option”)

[英]How to pass commands and options to PHP script in CLI (e.g. php file.php mycommand -d “my option”)

I'm aware I can use getopt and argv in my PHP script, but I don't know how to do something like the following: 我知道我可以在PHP脚本中使用getopt和argv,但我不知道如何执行以下操作:

php file.php mycommand -d "my option"

I've tried to use getopt with this but I guess the mycommand upsets it: 我试图与此使用getopt,但我猜mycommand使其不高兴:

var_dump(getopt('d:')); // empty array

Currently I use $argv to get the command but using this with -x options means I need to do some processing to extract the options. 当前,我使用$ argv来获取命令,但是将其与-x选项一起使用意味着我需要进行一些处理以提取选项。 I'm hoping there is a proper way to do so: 我希望有一个适当的方法可以做到这一点:

array(5) {
  [0]=>
  string(27) "file.php"
  [1]=>
  string(4) "mycommand"
  [2]=>
  string(2) "-d"
  [3]=>
  string(4) "my option"
}

Is there a way to do so? 有办法吗?

You have to remove the space between option and value 您必须删除选项和值之间的空格

Command 命令

php 1.php -xmycommand -d"my option"

PHP Code PHP代码

var_dump(getopt('x:d:'));

Output 产量

array(2) {
  ["x"]=>
  string(9) "mycommand"
  ["d"]=>
  string(9) "my option"
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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