Cli Class
Interact with the command line by accepting input options, parameters and output text.
beep($num = 1)
The beep method fires a system beep on the computer running the command.
| Static |
Yes |
| Parameters |
| Param |
Default |
Description |
| $num |
1 |
Number of beeps. |
|
| Returns |
void |
| Example |
Cli::beep(25);
|
color($text, $foreground, $background = null, $format = null)
The color method changes the color of a piece of text.
| Static |
Yes |
| Parameters |
| Param |
Default |
Description |
| $text |
Required |
String to be colored. |
| $foreground |
Required |
Foreground color of the string. |
| $background |
null |
Background color of the string. |
| $format |
null |
Other formatting to apply. Currently only the format 'underline' is support. |
|
| Returns |
string |
| Example |
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)
The error method will write a line of text to the command line as an error (similar to write but uses STDERR instead of STDOUT).
| Static |
Yes |
| Parameters |
| Param |
Default |
Description |
| $text |
empty string |
Text to output to STDERR for the output. |
|
| Returns |
void |
| Example |
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())
The prompt method prompts the user for input.
| Static |
Yes |
| Parameters |
| Param |
Default |
Description |
| $question |
null |
Ask the user a question and wait for input. |
| $options |
array() |
An array of options for the user to select from. |
|
| Returns |
string |
| Example |
// Waits for any key press
Cli::prompt();
// Takes any input
$color = Cli::prompt('What is your favorite color?');
// Takes any input, but offers default
$color = Cli::prompt('What is your favorite color?', 'white');
// Will only accept the options in the array
$ready = Cli::prompt('Are you ready?', array('y','n'));
|
option($name, null)
The option accepts an option from the initial command.
| Static |
Yes |
| Parameters |
| Param |
Default |
Description |
| $name |
Required |
Name of the option. |
| $default |
null |
A default value in case the option is not provided. |
|
| Returns |
string |
| Example |
$ php index.php user -v --v -name=John --name=John
|
wait($seconds = 0, $countdown = false)
The wait method will make the cli output wait for a given number of seconds and optionally show a countdown.
| Static |
Yes |
| Parameters |
| Param |
Default |
Description |
| $seconds |
0 |
Number of seconds to wait. |
| $countdown |
false |
Show the countdown in the output. |
|
| Returns |
void |
| Example |
Cli::write('Loading...');
Cli::wait(5, true);
|
write($text = '', $foreground = null, $background = null)
The write method will write a line of text to the command line.
| Static |
Yes |
| Parameters |
| Param |
Default |
Description |
| $text |
empty string |
Text to output to the command line. |
| $foreground |
null |
Foreground color of the string. |
| $background |
null |
Background color of the string. |
|
| Returns |
void |
| Example |
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.
| Static |
Yes |
| Parameters |
| Param |
Default |
Description |
| $resource |
null |
Any writable filehandle, or null to retrieve the current filehandle. |
|
| Returns |
The previous value of the filehandle (or current value if you are not changing it) |
| Example |
$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.
| Static |
Yes |
| Parameters |
| Param |
Default |
Description |
| $resource |
null |
Any writable filehandle, or null to retrieve the current filehandle. |
|
| Returns |
The previous value of the filehandle (or current value if you are not changing it) |
| Example |
$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? :(
|