<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                步驟: 執行命令:php think?hello xiaoming zhangsan 1\. command文件夾中創建自定義指令php class文件,且繼承 think\\console\\Command類 2\. 包含?configure、execute方法 3\. config/console.php 添加命令 ![](https://img.kancloud.cn/7d/06/7d063585d047ac9c2d6770ad853891d1_1626x542.png) #使用控制臺參數 #參數是字符串,多個參數由空格分隔,并且跟在命令名稱的后面。參數是有順序的,而且可以是可選的或必須的。例如,添加一個可選的 last\_name 參數到命令中,并且令 name 參數必填: #使用控制臺參數 #參數是字符串,多個參數由空格分隔,并且跟在命令名稱的后面。參數是有順序的,而且可以是可選的或必須的。例如,添加一個可選的 last\_name 參數到命令中,并且令 name 參數必填: 代碼: ![](https://img.kancloud.cn/59/10/591030da98014434d46bbd5d65dfc445_2872x1750.png) ??注意看參數和選項的調用區別 方式一:控制臺調用 ?命令:php think printTest liujie 20 --num 2 ?![](https://img.kancloud.cn/56/3d/563d1ff080bbdebe10e7fb8578bde859_2564x400.png) ?方式二:控制器中調用 ![](https://img.kancloud.cn/ba/12/ba12d191be3f1f99121707f017abe0ee_1116x705.png) # 控制臺輸出(參數和選項) 命令行最有趣的部分,就是你可以利用參數(`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 . '!'); } } ~~~ ~~~sql $ php think app:create-user kitty Hi kitty! $ php think app:create-user kitty cat Hi kitty cat! ~~~ 也可以讓參數接收“值的列表”(用空格分割)。但只有最后一個參數才能是列表: ~~~kotlin $this // ... ->addArgument( 'names', Argument::IS_ARRAY, 'Who like ThinkPHP (separate multiple names with a space)?' ); ~~~ 要使用列表,指定任意多的名字即可: ~~~sql $ php think app:create-user Jim Kitty Lucy ~~~ 你可以訪問到作為數組的 names 參數: ~~~bash $names = $input->getArgument('names'); $text = ''; if (count($names) > 0) { $text .= ' ' . implode(', ', $names); } ~~~ 有三種參數類型可用: | 參數類型 | 說明 | | --- | --- | | `Argument::REQUIRED` | 參數必填。如果不提供,則命令不運行 | | `Argument::OPTIONAL` | 參數可選,因此可以忽略 | | `Argument::IS_ARRAY` | 參數可以包含任意多個值。因此,它必須使用在參數列表中的最后一個 | 你可以像下面這樣同時使用?`IS_ARRAY`?和?`REQUIRED`?以及?`OPTIONAL`?: ~~~php $this // ... ->addArgument( 'names', InputArgument::IS_ARRAY | InputArgument::REQUIRED, 'Who like ThinkPHP (separate multiple names with a space)?' ); ~~~ ## 使用命令行選項 和參數不同,選項是沒有順序之分的 (也就是說你可以按任意順序指定它們) ,指定選項是用兩個中杠 (如:`--key`)。 選項?*始終*?是可選的,而且可以被設置為接收一個值 (如:`--dir=src`) ,或者是一個布爾值而不需要值 (如:`--key`)。 例如,向一個“信息在一行之內應該被輸出指定的次數”的命令中添加一個新的選項: ~~~kotlin $this // ... ->addOption( 'num', null, Option::VALUE_REQUIRED, 'How many messages should be print?', 1 ); ~~~ 接下來,使用這個命令來多次輸出信息: ~~~bash 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`?了: ~~~sql # 不提供 --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`: ~~~kotlin $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`?: ~~~php $this // ... ->addOption( 'animals', null, Option::VALUE_REQUIRED | Option::VALUE_IS_ARRAY, 'Which animals do you like?', array('cat', 'dog') ); ~~~ > 當你創建命令時,使用選項并令其可選地接受一個值,它是不受約束的。但是,當這個選項并沒有具體值 (`command --language`) ,或者它根本沒被使用 (`command`) 的時候,你是沒有辦法做到區分這些情況的。這兩種情況下,此選項所收到的值都是?`null`。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看