gpt4 book ai didi

php - 在 CLI 中获取 PHP 脚本的参数

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

我试图在 CLI 中运行 PHP 脚本时将参数传递给它。我一直在使用以下代码来这样做...

// Set argument variables
// A is the filetype
// B is the filename and directory
$arguments = getopt("a:b:");

// If we're importing pies...
if($arguments['a'] == 'pies') {
echo 'File Conversion and Import of PIES Data Beginning Now...' . "\n\n";
}

命令看起来像这样……:

php fileimport.php -a pies -b /filepath/filename.xml

我已经看到参数以其他方式传递(对于其他脚本),例如...

php fileimport --type=pies --path=filepath/filename.xml

我个人更喜欢这种方法,但我不知道如何编写我的 PHP 脚本来收集这些参数,更不用说期待它们了。我确信这非常简单,如果我错过了显而易见的内容,我很抱歉,但我希望能在这里找到一些方向。

谢谢!

最佳答案

PHP documentation 中所述:

$shortopts  = "";

$longopts = array(
"type:",
"path:",
);
// Get command line arguments
$options = getopt($shortopts, $longopts);
echo 'Input Options: ', PHP_EOL;
var_dump($options);

关于php - 在 CLI 中获取 PHP 脚本的参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18787840/

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