Cli 类別

透過接受輸入選項、參数及輸出文字,與命令列互动。

beep($num = 1)

beep 方法在运行命令的電腦上觸发系统提示聲。

靜態
參数
參数 預設 描述
$num 1 嗶聲次数。
回傳
範例
Cli::beep(25);

color($text, $foreground, $background = null, $format = null)

color 方法改變一段文字的顏色。

靜態
參数
參数 預設 描述
$text 必要 要上色的字串。
$foreground 必要 字串的前景色。
$background null 字串的背景色。
$format null 其他要套用的格式。目前只支援 'underline' 格式。
回傳 字串
範例
if (true === false)
{
	$message = Cli::color('Error: The universe is broken.', 'red');
}

else
{
	$message = Cli::color('All is well with the world.', 'green');
}

Cli::write($message);

error($text)

error 方法会寫一行文字到命令列做为一个错误(类似 write 但使用 STDERR 而非 STDOUT)。

靜態
參数
參数 預設 描述
$text 空字串 要輸出到 STDERR 的文字。
回傳
範例
Cli::error('Failure: You hit the wrong key with your chubby hands, try using a stick to poke the keyboard.');

prompt($question = null, $options = array())

prompt 方法提示使用者輸入。

靜態
參数
參数 預設 描述
$question null 詢問使用者問題且等待輸入。
$options array() 提供使用者選擇的選項陣列。
回傳 字串
範例
// 等待按任何鍵
Cli::prompt();

// 接受任何輸入
$color = Cli::prompt('What is your favorite color?');

// 接受任何輸入,但提供預設值
$color = Cli::prompt('What is your favorite color?', 'white');

// 只接受陣列中的選項
$ready = Cli::prompt('Are you ready?', array('y','n'));

option($name, null)

option 接受來自初始命令的選項。

靜態
參数
參数 預設 描述
$name 必要 選項名稱。
$default null 當選項未被提供時的預設值。
回傳 字串
範例
$ php index.php user -v --v -name=John --name=John

wait($seconds = 0, $countdown = false)

wait 方法使 cli 輸出等待一个所給的秒数,且可選擇性的顯示倒数計時。

靜態
參数
參数 預設 描述
$seconds 0 等待的秒数。
$countdown false 在輸出顯示倒数計時。
回傳
範例
Cli::write('Loading...');
Cli::wait(5, true);

write($text = '', $foreground = null, $background = null)

write 方法将寫一行文字到命令列。

靜態
參数
參数 預設 描述
$text 空字串 要輸出到命令列的文字。
$foreground null 字串的前景色。
$background null 字串的背景色。
回傳
範例
Cli::write('Hello World!');

stdout($resource = null)

Changes or retrieves the current stdout stream. This is STDOUT by default.

Note the public property $nocolor can be set to true to force all output to plaintext.

靜態
參数
參数 預設 描述
$resource null Any writable filehandle, or null to retrieve the current filehandle.
回傳 The previous value of the filehandle (or current value if you are not changing it)
範例
$buffer = fopen('php://temp', 'w');
$stdout = Cli::stdout($buffer);
Cli::write("Hello World!");
Cli::error("Where's my text? :(");
Cli::stdout($stdout);
Cli::write("There it is!");

rewind($buffer);
file_put_contents('out.log', stream_get_contents($buffer));

// $ cat out.log
// Hello World!

stderr($resource = null)

Changes or retrieves the current stderr stream. This is STDERR by default.

Note the public property $nocolor can be set to true to force all output to plaintext.

靜態
參数
參数 預設 描述
$resource null Any writable filehandle, or null to retrieve the current filehandle.
回傳 The previous value of the filehandle (or current value if you are not changing it)
範例
$errors = fopen('php://temp', 'w');
$stderr = Cli::stderr($errors);
Cli::write("Hello World!");
Cli::error("Where's my text? :(");
Cli::stderr($stderr);
Cli::write("There it is!");

rewind($errors);
file_put_contents('out.log', stream_get_contents($errors));

// $ cat out.log
// Where's my text? :(