gpt4 book ai didi

php getopt 返回空数组

转载 作者:行者123 更新时间:2023-12-04 02:06:28 28 4
gpt4 key购买 nike

我正在尝试使用 getopt() 从 PHP7 cli 获取选项,但是在调用 php myprocess.php task -d -o 时我得到一个空数组。不知道我错过了什么,希望你能帮助我。

这就是我阅读选项的方式:

$options = getopt("d::o::");

谢谢

最佳答案

正如手册所说

The parsing of options will end at the first non-option found, anything that follows is discarded.



因此,“任务”中的 t 是“找到的第一个非选项”,这就是为什么后面的任何内容(包括 -d-o )都被丢弃的原因。

因此,您可以将 task 移动到 shell 调用的末尾:
> php myprocess.php -d -o task

或者也将任务设置为选项:
$options = getopt("d::o::t::");   // added `t` option
echo'<pre>',print_r($options),'</pre>';

> php myprocess.php -d -o -ttask

输出:
Array
(
[d] =>
[o] =>
[t] => task
)

关于php getopt 返回空数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43031242/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com