当前位置: 首页 > 文档资料 > FuelPHP 中文文档 >

Cli - 类別

优质
小牛编辑
118浏览
2023-12-01

透过接受输入选项、参数及输出文字,与命令列互动。

beep($num = 1)

beep 方法在运行命令的电脑上触发系统提示声。

静态
参数
参数预设描述
$num1哔声次数。
回传
範例
Cli::beep(25);

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

color 方法改变一段文字的颜色。

静态
参数
参数预设描述
$text必要要上色的字串。
$foreground必要字串的前景色。
$backgroundnull字串的背景色。
$formatnull其他要套用的格式。目前只支援 '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 方法提示使用者输入。

静态
参数
参数预设描述
$questionnull询问使用者问题且等待输入。
$optionsarray()提供使用者选择的选项阵列。
回传字串
範例
// 等待按任何键
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必要选项名称。
$defaultnull当选项未被提供时的预设值。
回传字串
範例
$ php index.php user -v --v -name=John --name=John

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

wait 方法使 cli 输出等待一个所给的秒数,且可选择性的显示倒数计时。

静态
参数
参数预设描述
$seconds0等待的秒数。
$countdownfalse在输出显示倒数计时。
回传
範例
Cli::write('Loading...');
Cli::wait(5, true);

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

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

静态
参数
参数预设描述
$text空字串要输出到命令列的文字。
$foregroundnull字串的前景色。
$backgroundnull字串的背景色。
回传
範例
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.

静态
参数
参数预设描述
$resourcenullAny 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.

静态
参数
参数预设描述
$resourcenullAny 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? :(