# 控制臺輸出(參數和選項)
[TOC]
命令行最有趣的部分,就是你可以利用參數(`arguments`)和選項(`options`)。參數和選項,能夠讓你從終端(`terminal`)向命令(`command`)動態地傳入信息。
## 使用控制臺參數
參數是字符串,多個參數由空格分隔,并且跟在命令名稱的后面。參數是有順序的,而且可以是可選的或必須的。例如,添加一個可選的 last_name 參數到命令中,并且令 name 參數必填:
```php
<?php
namespace app\console;
use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\Output;
class CreateUser extends Command
{
// ...
protected function configure()
{
$this
// 命令的名字("think" 后面的部分)
->setName('app:create-user')
// 配置一個參數
->addArgument('name', Argument::REQUIRED, 'Do you like ThinkPHP')
->addArgument('last_name', Argument::OPTIONAL, 'Your last name?')
// 運行 "php think list" 時的簡短描述
->setDescription('Creates new users.')
// 運行命令時使用 "--help" 選項時的完整命令描述
->setHelp("This command allows you to create users...");
}
protected function execute(Input $input, Output $output)
{
// ...
}
}
```
現在你可以在命令中訪問 last_name 參數:
```php
<?php
namespace app\console;
use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\Output;
class CreateUser extends Command
{
// ...
protected function configure()
{
$this
// 命令的名字("think" 后面的部分)
->setName('app:create-user')
// 配置一個參數
->addArgument('name', Argument::REQUIRED, 'Who like ThinkPHP ?')
->addArgument('last_name', Argument::OPTIONAL, 'Your last name?')
// 運行 "php think list" 時的簡短描述
->setDescription('Creates new users.')
// 運行命令時使用 "--help" 選項時的完整命令描述
->setHelp("This command allows you to create users...");
}
protected function execute(Input $input, Output $output)
{
$text = 'Hi ' . $input->getArgument('name');
$lastName = $input->getArgument('last_name');
if ($lastName) {
$text .= ' ' . $lastName;
}
$output->writeln($text . '!');
}
}
```
```bash
$ php think app:create-user kitty
Hi kitty!
$ php think app:create-user kitty cat
Hi kitty cat!
```
也可以讓參數接收“值的列表”(用空格分割)。但只有最后一個參數才能是列表:
```
$this
// ...
->addArgument(
'names',
Argument::IS_ARRAY,
'Who like ThinkPHP (separate multiple names with a space)?'
);
```
要使用列表,指定任意多的名字即可:
```bash
$ php think app:create-user Jim Kitty Lucy
```
你可以訪問到作為數組的 names 參數:
```
$names = $input->getArgument('names');
$text = '';
if (count($names) > 0) {
$text .= ' ' . implode(', ', $names);
}
```
有三種參數類型可用:
|參數類型|說明|
|:-------|:---|
|`Argument::REQUIRED`|參數必填。如果不提供,則命令不運行|
|`Argument::OPTIONAL`|參數可選,因此可以忽略|
|`Argument::IS_ARRAY`|參數可以包含任意多個值。因此,它必須使用在參數列表中的最后一個|
你可以像下面這樣同時使用 `IS_ARRAY` 和 `REQUIRED` 以及 `OPTIONAL` :
```
$this
// ...
->addArgument(
'names',
InputArgument::IS_ARRAY | InputArgument::REQUIRED,
'Who like ThinkPHP (separate multiple names with a space)?'
);
```
## 使用命令行選項
和參數不同,選項是沒有順序之分的 (也就是說你可以按任意順序指定它們) ,指定選項是用兩個中杠 (如:`--key`)。
選項 ***始終*** 是可選的,而且可以被設置為接收一個值 (如:`--dir=src`) ,或者是一個布爾值而不需要值 (如:`--key`)。
例如,向一個“信息在一行之內應該被輸出指定的次數”的命令中添加一個新的選項:
```
$this
// ...
->addOption(
'num',
null,
Option::VALUE_REQUIRED,
'How many messages should be print?',
1
);
```
接下來,使用這個命令來多次輸出信息:
```
for ($i = 0; $i < $input->getOption('iterations'); $i++) {
$output->writeln($text);
}
```
完整示例:
```php
<?php
namespace app\console;
use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;
class CreateUser extends Command
{
// ...
protected function configure()
{
$this
// 命令的名字("think" 后面的部分)
->setName('app:create-user')
// 配置一個參數
->addArgument('name', Argument::REQUIRED, 'Who like ThinkPHP ?')
// 配置一個選項
->addOption(
'num',
null,
Option::VALUE_REQUIRED,
'How many messages should be print?',
1
);
}
protected function execute(Input $input, Output $output)
{
$text = 'Hi ' . $input->getArgument('name');
for ($i = 0; $i < $input->getOption('num'); $i++) {
$output->writeln($text);
}
}
}
```
現在,運行命令時,你可以可選地指定一個 `--num` 了:
```bash
# 不提供 --num,使用的默認值是(1)
$ php think app:create-user kitty
Hi kitty
$ php think app:create-user kitty --num=7
Hi kitty
Hi kitty
Hi kitty
Hi kitty
Hi kitty
Hi kitty
Hi kitty
# 選項的順序是任意的的
$ php think app:create-user kitty --num=7 --key
$ php think app:create-user kitty --key --num=7
$ php think app:create-user --key --num=7 kitty
```
> 你還可以為選項聲明一個“以單個中杠開頭”的單字符的快捷方式,比如 `-n`:
```
$this
// ...
->addOption(
'num',
'n',
Option::VALUE_REQUIRED,
'How many messages should be print?',
1
);
```
選項 支持四種類型:
|選項類型|說明|示例|
|:-------|:---|:---|
|`Option::VALUE_IS_ARRAY`|此選項可接收多個值|--dir=/foo --dir=/bar|
|`Option::VALUE_NONE`|此選項不接受輸入的值|--key|
|`Option::VALUE_REQUIRED`|此選項的值必填,但選項本身仍然是可選的|--num=7|
|`Option::VALUE_OPTIONAL`|此選項的值可有可無|--key 或 --key=value|
你可以像下面這樣同時使用 `VALUE_IS_ARRAY` 和 `VALUE_REQUIRED` 或 `VALUE_OPTIONAL` :
```
$this
// ...
->addOption(
'animals',
null,
Option::VALUE_REQUIRED | Option::VALUE_IS_ARRAY,
'Which animals do you like?',
array('cat', 'dog')
);
```
> 當你創建命令時,使用選項并令其可選地接受一個值,它是不受約束的。但是,當這個選項并沒有具體值 (`command --language`) ,或者它根本沒被使用 (`command`) 的時候,你是沒有辦法做到區分這些情況的。這兩種情況下,此選項所收到的值都是 `null`。