make:command生成的文件中有两个默认方法getArguments和getOptions有什么作用?
使用php artisan make:command 命令生成的文件有两个默认方法。getArguments 和getOptions,请问这两个方法有什么作用啊?
/**
* Get the console command arguments.
*/
protected function getArguments(): array
{
return [
['example', InputArgument::REQUIRED, 'An example argument.'],
];
}
/**
* Get the console command options.
*/
protected function getOptions(): array
{
return [
['example', null, InputOption::VALUE_OPTIONAL, 'An example option.', null],
];
}
简单理解,以
--
或者-
开头的“参数”,叫选项(options),直接跟在命令后面的是参数(arguments)以 cURL 为例
比如
curl -X POST https://httpbin.org/post
这里的-X
是选项(option),后面的POST
是选项的值(注:选项不一定有值,有可能只是个 bool 开关。),而不是参数,最后面的地址https://httpbin.org/post
就是<url>
参数(argument)。另外,选项(options)和参数(arguments) 都是可以有多个的。不过选项可以不考虑传入的顺序,参数需要严格按照定义的顺序来传。
楼上已经做了生动的介绍了,
getOptions()
和getArguments()
只是这些形式语义化表述,如果你深究来源,那你可以看下linux命令行的格式说明,在php中已经内置了获取这些参数的函数方法,如果有兴趣可以看下。getopt