silverorange Code


Site.SiteCommandLineApplication
/Site/SiteCommandLineApplication.php at line 20

Class SiteCommandLineApplication

SwatObject
└─SiteObject
└─SiteApplication
└─SiteCommandLineApplication

public abstract class SiteCommandLineApplication
extends SiteApplication

An application designed to run on the command line

This class handles the creating and parsing of command line arguments and has the ability to display usage information.

Command line applications must implement the SiteApplication::run() method.

Copyright:
2006 silverorange
License:
http://www.gnu.org/copyleft/lesser.html LGPL License 2.1

Field Summary
final mixed

VERBOSITY_ALL

Verbosity level for showing all actions.

final mixed

VERBOSITY_ERRORS

Verbosity level for showing error messages.

final mixed

$VERBOSITY_NONE

Verbosity level for showing nothing.

protected array

$arguments

An array of SiteCommandLineArgument objects used by this application.

protected string

$documentation

Text describing the purpose of this application.

protected resource

$lock_file

The opened lock file.

protected string

$title

The title of this application.

protected integer

$verbosity

The level of verbosity to use.

Fields inherited from Site.SiteApplication
VAR_COOKIE, VAR_ENV, VAR_FILES, VAR_GET, VAR_POST, VAR_REQUEST, VAR_SERVER, VAR_SESSION, default_time_zone, id, locale, modules, modules_by_provides
Constructor Summary

SiteCommandLineApplication(string id, string config_filename, string title, string documentation)

Creates a new command line application.

Method Summary
void

addCommandLineArgument(SiteCommandLineArgument argument)

Adds a command line argument to this application.

protected void

debug(string string, boolean bold)

Displays output at the verbosity level SiteCommandLineApplication::VERBOSITY_ALL.

void

displayArgumentUsage(SiteCommandLineArgument argument)

Displays usage information for a single command line argument.

void

displayUsage()

Displays usage information for this command line application.

protected void

error(string string, boolean bold)

Displays output at the verbosity level SiteCommandLineApplication::VERBOSITY_ERRORS.

protected void

getLockFilename()

string

getScriptDirectory()

Gets an absolute path to the directory in which the currently running script resides.

protected void

lock()

Locks this application, preventing more than one instance from running.

protected void

output(string string, integer verbosity, boolean bold)

Displays a string based on the verbosity level of this application.

protected void

parseCommandLineArguments()

Automatically parses and interprets command line arguments of this application.

void

setApplicationDirectory(string directory)

Sets the application directory.

void

setVerbosity(integer verbosity)

Sets the level of verbosity to use for this application.

protected void

terminate(string string, integer verbosity, integer error_code)

Terminates this application and displays the specified error message.

protected void

unlock()

Unlocks this application, allowing more than one instance to run.

Methods inherited from Site.SiteApplication
addConfigDefinitions, addDefaultModule, addDefaultModules, addModule, configure, configureErrorHandling, getCountry, getDefaultModuleList, getInstance, getInstanceId, getLocale, getModule, hasModule, initModules, initVar, postInitConfigure, run

Field Detail

/Site/SiteCommandLineApplication.php at line 41

VERBOSITY_ALL

public final mixed VERBOSITY_ALL = 2

Verbosity level for showing all actions

See Also:
SiteCommandLineApplication::debug()

/Site/SiteCommandLineApplication.php at line 34

VERBOSITY_ERRORS

public final mixed VERBOSITY_ERRORS = 1

Verbosity level for showing error messages.

See Also:
SiteCommandLineApplication::error()

/Site/SiteCommandLineApplication.php at line 27

VERBOSITY_NONE

public final mixed $VERBOSITY_NONE

Verbosity level for showing nothing.


/Site/SiteCommandLineApplication.php at line 52

arguments

protected array $arguments = array()

An array of SiteCommandLineArgument objects used by this application


/Site/SiteCommandLineApplication.php at line 70

documentation

protected string $documentation

Text describing the purpose of this application

This is displayed in usage information.


/Site/SiteCommandLineApplication.php at line 91

lock_file

protected resource $lock_file

The opened lock file

See Also:
SiteCommandLineApplication::lock()
SiteCommandLineApplication::unlock()

/Site/SiteCommandLineApplication.php at line 61

title

protected string $title

The title of this application

The title is displayed in error messages and in usage information.


/Site/SiteCommandLineApplication.php at line 81

verbosity

protected integer $verbosity

The level of verbosity to use

This controls the output of SiteCommandLineApplication::output().

See Also:
SiteCommandLineApplication::setVerbosity()

Constructor Detail

/Site/SiteCommandLineApplication.php at line 115

SiteCommandLineApplication

public SiteCommandLineApplication(string id, string config_filename, string title, string documentation)

Creates a new command line application

By default, the 'help' and 'verbosity' command line arguments are added. This 'help' argument is set to display usage information and the 'verbosity' argument sets the level of debug output.

Parameters:
id - a unique identifier for this application.
config_filename - the filename of the configuration file. If specified as null, no configuration module is created and no special configuration is performed.
title - the title of this application.
documentation - optional text describing the purpose of this application.
Throws:
if this application is not created in a command- line environment.

Method Detail

/Site/SiteCommandLineApplication.php at line 170

addCommandLineArgument

public void addCommandLineArgument(SiteCommandLineArgument argument)

Adds a command line argument to this application

Command line arguments may be added either when the class is used or in the class definition.

For example, to create a command line argument accepting either '-f' or '--foo' that runs the foo() method, use the following:

<?php
$foo_argument 
= new SiteCommandLineArgument(array('-f''--foo'), 'foo',
    
'Runs the foo() method.');

$app->addCommandLineArgument($foo_argument);
?>

Parameters:
argument - the command line argument to add.

/Site/SiteCommandLineApplication.php at line 370

debug

protected void debug(string string, boolean bold)

Displays output at the verbosity level SiteCommandLineApplication::VERBOSITY_ALL

Parameters:
string - the string to display.
bold - optional. Whether or not to display the string using a bold font on supported terminals. Defaults to false.

/Site/SiteCommandLineApplication.php at line 232

displayArgumentUsage

public void displayArgumentUsage(SiteCommandLineArgument argument)

Displays usage information for a single command line argument

Parameters:
argument - the command line argument for which to display usage information.

/Site/SiteCommandLineApplication.php at line 207

displayUsage

public void displayUsage()

Displays usage information for this command line application


/Site/SiteCommandLineApplication.php at line 387

error

protected void error(string string, boolean bold)

Displays output at the verbosity level SiteCommandLineApplication::VERBOSITY_ERRORS

Parameters:
string - the string to display.
bold - optional. Whether or not to display the string using a bold font on supported terminals. Defaults to false.

/Site/SiteCommandLineApplication.php at line 480

getLockFilename

protected void getLockFilename()

/Site/SiteCommandLineApplication.php at line 248

getScriptDirectory

public string getScriptDirectory()

Gets an absolute path to the directory in which the currently running script resides

Returns:
the absolute path to the directory in which the currently running script resides.

/Site/SiteCommandLineApplication.php at line 423

lock

protected void lock()

Locks this application, preventing more than one instance from running


/Site/SiteCommandLineApplication.php at line 340

output

protected void output(string string, integer verbosity, boolean bold)

Displays a string based on the verbosity level of this application

Parameters:
string - the string to display.
verbosity - the verbosity level to display at. If this application's verbosity is less than this level, the string is not displayed.
bold - optional. Whether or not to display the string using a bold font on supported terminals. Defaults to false.

/Site/SiteCommandLineApplication.php at line 262

parseCommandLineArguments

protected void parseCommandLineArguments()

Automatically parses and interprets command line arguments of this application

See Also:
SiteCommandLineApplication::addCommandLineArgument()

/Site/SiteCommandLineApplication.php at line 196

setApplicationDirectory

public void setApplicationDirectory(string directory)

Sets the application directory

Parameters:
directory - the directory to use.

/Site/SiteCommandLineApplication.php at line 183

setVerbosity

public void setVerbosity(integer verbosity)

Sets the level of verbosity to use for this application

Parameters:
verbosity - the level of verbosity to use.

/Site/SiteCommandLineApplication.php at line 410

terminate

protected void terminate(string string, integer verbosity, integer error_code)

Terminates this application and displays the specified error message

Program execution ceases after this method is run. Code present in object destructors will still run.

Parameters:
string - the string to dipslay.
verbosity - optional. the verbosity level to display at. If this application's verbosity is less than this level, the string is not displayed. Defaults to {@link SiteCommandLineApplication::VERBOSITY_ERRORS}.
error_code - optional. The error code with which to terminate execution. If not specified, 1 is used.

/Site/SiteCommandLineApplication.php at line 462

unlock

protected void unlock()

Unlocks this application, allowing more than one instance to run


silverorange Code