\Horde_Cli

Horde_Cli API for basic command-line functionality/checks.

Summary

Methods
Properties
Constants
__construct()
getWidth()
writeln()
clearScreen()
indent()
bold()
color()
red()
green()
blue()
yellow()
header()
message()
fatal()
block()
prompt()
passwordPrompt()
readStdin()
init()
runningFromCLI()
shutdown()
No public properties found
No constants found
_addLines()
_setWidth()
_detectPager()
$_output
$_newline
$_space
$_clearscreen
$_indent
$_color
$_width
N/A
No private methods found
No private properties found
N/A

Properties

$_output

$_output : resource

The output pipe.

Type

resource

$_newline

$_newline : string

The newline string to use.

Type

string

$_space

$_space : string

The formatted space string to use.

Type

string

$_clearscreen

$_clearscreen : string

The string to use for clearing the screen.

Type

string

$_indent

$_indent : string

The indent string to use.

Type

string

$_width

$_width : integer

The terminal width.

Type

integer

Methods

__construct()

__construct(array  $opts = array()) 

Detect the current environment (web server or console) and sets internal values accordingly.

Use init() if you also want to set environment variables that may be missing in a CLI environment.

Parameters

array $opts

Configuration options:

  • 'output': (resource, optional) Open pipe to write the output to.
  • 'pager': (boolean) Pipe output through a pager? @since 2.3.0

getWidth()

getWidth() : integer

Retuns the detected terminal screen width.

Defaults to 80 if the width cannot be detected automatically.

Returns

integer —

The terminal screen width or null if not a terminal.

writeln()

writeln(string  $text = '', boolean  $pre = false) 

Prints $text on a single line.

Parameters

string $text

The text to print.

boolean $pre

If true the linebreak is printed before the text instead of after it.

clearScreen()

clearScreen() 

Clears the entire screen, if possible.

indent()

indent(string  $text) : string

Returns the indented string.

Parameters

string $text

The text to indent.

Returns

string —

The indented text.

bold()

bold(string  $text) : string

Returns a bold version of $text.

Parameters

string $text

The text to bold.

Returns

string —

The bolded text.

color()

color(string  $color, string  $text) : string

Returns a colored version of $text.

Parameters

string $color

The color to use. @see $_foregroundColors

string $text

The text to print in this color.

Returns

string —

The colored text.

red()

red(string  $text) : string

Returns a red version of $text.

Parameters

string $text

The text to print in red.

Returns

string —

The red text.

green()

green(string  $text) : string

Returns a green version of $text.

Parameters

string $text

The text to print in green.

Returns

string —

The green text.

blue()

blue(string  $text) : string

Returns a blue version of $text.

Parameters

string $text

The text to print in blue.

Returns

string —

The blue text.

yellow()

yellow(string  $text) : string

Returns a yellow version of $text.

Parameters

string $text

The text to print in yellow.

Returns

string —

The yellow text.

header()

header(string  $message, string  $below = '-', string  $above = null) 

Creates a header from a string by drawing character lines above or below the header content.

Parameters

string $message

A message to turn into a header.

string $below

Character to use for drawing the line below the message.

string $above

Character to use for drawing the line above the message.

message()

message(  $message, string  $type = 'cli.message') 

Displays a message.

Parameters

$message
string $type

The type of message: 'cli.error', 'cli.warning', 'cli.success', or 'cli.message'.

fatal()

fatal(mixed  $error) 

Displays a fatal error message.

Parameters

mixed $error

The error text to display, an exception or an object with a getMessage() method.

block()

block(string  $text, string  $color, string  $margin = '', integer  $width = null) : string

Formats text in a visual block with optional margin.

Parameters

string $text

The block text.

string $color

The background color.

string $margin

The block margin string.

integer $width

The block width.

Returns

string —

The formatted block.

prompt()

prompt(string  $prompt, array  $choices = null, string  $default = null) : mixed

Prompts for a user response.

Parameters

string $prompt

The message to display when prompting the user.

array $choices

The choices available to the user or null for a text input.

string $default

The default value if no value specified.

Returns

mixed —

The user's response to the prompt.

passwordPrompt()

passwordPrompt(string  $prompt) : string

Interactively prompts for input without echoing to the terminal.

Requires a bash shell or Windows and won't work with safe_mode settings (uses shell_exec).

From: http://www.sitepoint.com/blogs/2009/05/01/interactive-cli-password-prompt-in-php/

Parameters

string $prompt

The message to display when prompting the user.

Returns

string —

The user's response to the prompt.

readStdin()

readStdin() : string

Reads everything that is sent through standard input and returns it as a single string.

Returns

string —

The contents of the standard input.

init()

init(array  $opts = array()) : \Horde_Cli

CLI scripts shouldn't timeout, so try to set the time limit to none. Also initialize a few variables in $_SERVER that aren't present from the CLI.

Parameters

array $opts

Configuration options:

  • 'pager': (boolean) Pipe output through a pager? @since 2.3.0

Returns

\Horde_Cli

A Horde_Cli instance.

runningFromCLI()

runningFromCLI() : boolean

Make sure we're being called from the command line, and not via the web.

Returns

boolean —

True if we are, false otherwise.

shutdown()

shutdown() 

Destroys any session on script end.

_addLines()

_addLines(array  $buffer, string  $content) : string

Adds content to a line buffer, wrapping long lines if necessary.

Parameters

array $buffer

The line buffer.

string $content

The lines to add.

Returns

string —

The updated line buffer.

_setWidth()

_setWidth() 

Detects the terminal screen width.

_detectPager()

_detectPager() 

Detects available pagers (less, more) and creates pipes to output through them.