PHP code example of taro / chant-cli

1. Go to this page and download the library: Download taro/chant-cli library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

taro / chant-cli example snippets


   
   namespace Taro\App\Console\Commands;
   
   use Taro\Libs\Command\Command;
   
   class SampleCommand extends Command
   {
       // ターミナル上で入力するコマンドシグネチャ
       // オプション引数を [] で囲んで追加する。必須でない場合は 最後に ? を付ける
       public $signature = 'command:sample_command [arg1] [arg2?]';
   
       // パラメーター cf. ['name', 'age']  "--"は省略
       public $params = [];
       
       // フラグ cf. ['a','b','c']  "-"は省略
       public $flags = [];
   
       // コマンドの説明
       public $description = 'SampleCommand Class';
   
       // コマンド実行時の処理本体
       public function handle()
       {
           // Here is where you write any script executed by this command
           $this->textInfo('sample_command command (' . $this->signature . ')');
   
   
       }
   }
   

   use Taro\App\Console\Commands\SampleCommand; <= 追加
   
   class CommandList
   {
   
       public static $commands = [
           // SomeCommand::class
           ListRegisteredCommands::class,
           HelpCommand::class,
           MakeCommand::class,
           ...
           
           SampleCommand::class, <= 追加
       ];
   }
   

# bash を使わない場合は php コマンドで実行
php chant.php 実行するコマンド

// 必須
[arg1]
// 必須でない (引数名の最後に ? を付ける)
[arg2?]

public function handle()
{
	// オプション引数
    $arg1 = $this->argument('arg1'); // $arg1 == 'alpha'

    // name パラメーター
    $name = $this->parameter('name'); // $name == 'hoge'
    
    // is_test パラメーター (値が指定されていない場合は、trueを返す)
    $is_test = $this->parameter('is_test'); // $is_test == true
    
    // x フラグ (フラグがあれば true を返す)
    $x = $this->flag('x'); // $x == true
}

public function handle()
{
 	// 文字列を表示
    $this->text('test');
  	// 赤色文字列を表示
    $this->textDanger('test');
 	// 黄色文字列を表示
    $this->textWarning('test');    
 	// 緑色文字列を表示
    $this->textInfo('test');    
 	// 緑背景色の文字列を表示
    $this->success('test'); 
 	// 赤背景色の文字列を表示
    $this->error('test');     
}

public function handle()
{
    // question('質問文を書く')
    // ユーザーの入力が戻り値
    $result = $this->question('question1?');
}

public function handle()
{
    // confirm('確認文を書く')
    // ユーザーの入力に応じて、true/false が返される
    // yes/y なら true, それ以外は false
    $result = $this->confirm('confirm?');
}

public function handle()
{
    // 2次元データを見やすいテーブルで表示する
    // $this->table(headers[], body[ row1[],row2[],row3[] ... ], '色指定')
    // 指定できる色は black,red,green,blue,cyan,yellow, white
    $this->table(
        ['col1', 'col2'],
        [
            ['1cell1', '1cell2'],
            ['2cell1', '2cell2'],
            ['3cell1', '3cell2'],
            ['4cell1', '4cell2'],
        ],
        'green'
    );	
}