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],
        ];
    }
《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
《L02 从零构建论坛系统》
以构建论坛项目 LaraBBS 为线索,展开对 Laravel 框架的全面学习。应用程序架构思路贴近 Laravel 框架的设计哲学。
讨论数量: 5

简单理解,以 -- 或者 - 开头的“参数”,叫选项(options),直接跟在命令后面的是参数(arguments)


以 cURL 为例

# curl --help
Usage: curl [options...] <url>
 -d, --data <data>          HTTP POST data
 -f, --fail                 Fail fast with no output on HTTP errors
 -h, --help <category>      Get help for commands
 -i, --include              Include protocol response headers in the output
 -o, --output <file>        Write to file instead of stdout
 -O, --remote-name          Write output to a file named as the remote file
 -s, --silent               Silent mode
 -T, --upload-file <file>   Transfer local FILE to destination
 -u, --user <user:password> Server user and password
 -A, --user-agent <name>    Send User-Agent <name> to server
 -v, --verbose              Make the operation more talkative
 -V, --version              Show version number and quit
 ...
 -X, --request <method>   Specify request method to use
 ...
This is not the full help, this menu is stripped into categories.
Use "--help category" to get an overview of all categories.
For all options use the manual or "--help all".

比如 curl -X POST https://httpbin.org/post 这里的 -X 是选项(option),后面的 POST 是选项的值(注:选项不一定有值,有可能只是个 bool 开关。),而不是参数,最后面的地址 https://httpbin.org/post 就是 <url> 参数(argument)。

另外,选项(options)和参数(arguments) 都是可以有多个的。不过选项可以不考虑传入的顺序,参数需要严格按照定义的顺序来传。

6个月前 评论
Rache1 (作者) 6个月前
KingMarx (楼主) 6个月前
KingMarx (楼主) 6个月前

楼上已经做了生动的介绍了,getOptions()getArguments()只是这些形式语义化表述,如果你深究来源,那你可以看下linux命令行的格式说明,在php中已经内置了获取这些参数的函数方法,如果有兴趣可以看下。
getopt

6个月前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!