<commit_before><?php

require_once 'Site/SiteApplication.php';
require_once 'Site/exceptions/SiteException.php';
require_once 'Site/SiteCommandLineArgument.php';

/**
 * 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 {@link SiteApplication::run()}
 * method.
 *
 * @package   Site
 * @copyright 2006 silverorange
 * @license   http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
 */
abstract class SiteCommandLineApplication extends SiteApplication
{
	// {{{ class constants

	/**
	 * Verbosity level for showing nothing.
	 */
	const VERBOSITY_NONE = 0;

	/**
	 * Verbosity level for showing error messages.
	 *
	 * @see SiteCommandLineApplication::error()
	 */
	const VERBOSITY_ERRORS = 1;

	/**
	 * Verbosity level for showing all actions
	 *
	 * @see SiteCommandLineApplication::debug()
	 */
	const VERBOSITY_ALL = 2;

	// }}}
	// {{{ protected properties

	/**
	 * An array of {@link SiteCommandLineArgument} objects used by this
	 * application
	 *
	 * @var array
	 */
	protected $arguments = array();

	/**
	 * The title of this application
	 *
	 * The title is displayed in error messages and in usage information.
	 *
	 * @var string
	 */
	protected $title;

	/**
	 * Text describing the purpose of this application
	 *
	 * This is displayed in usage information.
	 *
	 * @var string
	 */
	protected $documentation;

	/**
	 * The level of verbosity to use
	 *
	 * This controls the output of {@link SiteCommandLineApplication::output()}.
	 *
	 * @var integer
	 *
	 * @see SiteCommandLineApplication::setVerbosity()
	 */
	protected $verbosity = 0;

	/**
	 * The opened lock file
	 *
	 * @var resource
	 *
	 * @see SiteCommandLineApplication::lock()
	 * @see SiteCommandLineApplication::unlock()
	 */
	protected $lock_file;

	// }}}
	// {{{ public function __construct()

	/**
	 * 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.
	 *
	 * @param string $id a unique identifier for this application.
	 * @param string $config_filename the filename of the configuration file.
	 *                                 If specified as null, no configuration
	 *                                 module is created and no special
	 *                                 configuration is performed.
	 * @param string $title the title of this application.
	 * @param string $documentation optional text describing the purpose of
	 *                               this application.
	 *
	 * @throws SiteException if this application is not created in a command-
	 *                        line environment.
	 */
	public function __construct($id, $config_filename, $title,
		$documentation = null)
	{
		if (!isset($_SERVER['argv'])) {
			throw new SiteException('Command line applications must be run '.
				'from the command line.');
		}

		parent::__construct($id, $config_filename);

		$this->title         = $title;
		$this->documentation = $documentation;

		// add help
		$help_argument = new SiteCommandLineArgument(
			array('-?', '-h', '--help'), 'displayUsage',
			Site::_('Displays this usage information and exits.'));

		$this->addCommandLineArgument($help_argument);

		// add verbosity
		$verbosity = new SiteCommandLineArgument(
			array('-v', '--verbose'), 'setVerbosity',
			Site::_('Sets the level of verbosity of this application. Pass 0 '.
				'to turn off all output.'));

		$verbosity->addParameter('integer',
			Site::_('--verbose expects a level between 0 and 2.'),
			self::VERBOSITY_ALL);

		$this->addCommandLineArgument($verbosity);
	}

	// }}}
	// {{{ public function addCommandLineArgument()

	/**
	 * 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:
	 *
	 * <code>
	 * $foo_argument = new SiteCommandLineArgument(array('-f', '--foo'), 'foo',
	 *     'Runs the foo() method.');
	 *
	 * $app->addCommandLineArgument($foo_argument);
	 * </code>
	 *
	 * @param SiteCommandLineArgument $argument the command line argument to
	 *                                           add.
	 */
	public function addCommandLineArgument(SiteCommandLineArgument $argument)
	{
		$this->arguments[] = $argument;
	}

	// }}}
	// {{{ public function setVerbosity()

	/**
	 * Sets the level of verbosity to use for this application
	 *
	 * @param integer $verbosity the level of verbosity to use.
	 */
	public function setVerbosity($verbosity)
	{
		$this->verbosity = (integer)$verbosity;
	}

	// }}}
	// {{{ public function setApplicationDirectory()

	/**
	 * Sets the application directory
	 *
	 * @param string $directory the directory to use.
	 */
	public function setApplicationDirectory($directory)
	{
		chdir($directory);
	}

	// }}}
	// {{{ public function displayUsage()

	/**
	 * Displays usage information for this command line application
	 */
	public function displayUsage()
	{
		echo $this->title, "\n\n";
		if ($this->documentation !== null)
			echo $this->documentation, "\n\n";

		echo Site::_('OPTIONS'), "\n";

		foreach ($this->arguments as $argument) {
			$this->displayArgumentUsage($argument);
		}

		exit(0);
	}

	// }}}
	// {{{ public function displayArgumentUsage()

	/**
	 * Displays usage information for a single command line argument
	 *
	 * @param SiteCommandLineArgument $argument the command line argument for
	 *                                           which to display usage
	 *                                           information.
	 */
	public function displayArgumentUsage(SiteCommandLineArgument $argument)
	{
		echo implode(', ', $argument->getNames()), "\n",
			"   ", $argument->getDocumentation(), "\n\n";
	}

	// }}}
	// {{{ public function getScriptDirectory()

	/**
	 * Gets an absolute path to the directory in which the currently running
	 * script resides
	 *
	 * @return string the absolute path to the directory in which the currently
	 *                running script resides.
	 */
	public function getScriptDirectory()
	{
		return dirname(realpath($_SERVER['SCRIPT_FILENAME']));
	}
<commit_msg>add boilerplate to run. This won't break any current code (since they'll all expect run to be abstract), but seems like  boilerplate that should be included in all command line applications. All need to parse the command line arguements,  and at worst, modules is left empty. We could clean up existing command line applications with this in mind is the  ambigious future.<commit_after><?php

require_once 'Site/SiteApplication.php';
require_once 'Site/exceptions/SiteException.php';
require_once 'Site/SiteCommandLineArgument.php';

/**
 * 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 {@link SiteApplication::run()}
 * method.
 *
 * @package   Site
 * @copyright 2006-2009 silverorange
 * @license   http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
 */
abstract class SiteCommandLineApplication extends SiteApplication
{
	// {{{ class constants

	/**
	 * Verbosity level for showing nothing.
	 */
	const VERBOSITY_NONE = 0;

	/**
	 * Verbosity level for showing error messages.
	 *
	 * @see SiteCommandLineApplication::error()
	 */
	const VERBOSITY_ERRORS = 1;

	/**
	 * Verbosity level for showing all actions
	 *
	 * @see SiteCommandLineApplication::debug()
	 */
	const VERBOSITY_ALL = 2;

	// }}}
	// {{{ protected properties

	/**
	 * An array of {@link SiteCommandLineArgument} objects used by this
	 * application
	 *
	 * @var array
	 */
	protected $arguments = array();

	/**
	 * The title of this application
	 *
	 * The title is displayed in error messages and in usage information.
	 *
	 * @var string
	 */
	protected $title;

	/**
	 * Text describing the purpose of this application
	 *
	 * This is displayed in usage information.
	 *
	 * @var string
	 */
	protected $documentation;

	/**
	 * The level of verbosity to use
	 *
	 * This controls the output of {@link SiteCommandLineApplication::output()}.
	 *
	 * @var integer
	 *
	 * @see SiteCommandLineApplication::setVerbosity()
	 */
	protected $verbosity = 0;

	/**
	 * The opened lock file
	 *
	 * @var resource
	 *
	 * @see SiteCommandLineApplication::lock()
	 * @see SiteCommandLineApplication::unlock()
	 */
	protected $lock_file;

	// }}}
	// {{{ public function __construct()

	/**
	 * 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.
	 *
	 * @param string $id a unique identifier for this application.
	 * @param string $config_filename the filename of the configuration file.
	 *                                 If specified as null, no configuration
	 *                                 module is created and no special
	 *                                 configuration is performed.
	 * @param string $title the title of this application.
	 * @param string $documentation optional text describing the purpose of
	 *                               this application.
	 *
	 * @throws SiteException if this application is not created in a command-
	 *                        line environment.
	 */
	public function __construct($id, $config_filename, $title,
		$documentation = null)
	{
		if (!isset($_SERVER['argv'])) {
			throw new SiteException('Command line applications must be run '.
				'from the command line.');
		}

		parent::__construct($id, $config_filename);

		$this->title         = $title;
		$this->documentation = $documentation;

		// add help
		$help_argument = new SiteCommandLineArgument(
			array('-?', '-h', '--help'), 'displayUsage',
			Site::_('Displays this usage information and exits.'));

		$this->addCommandLineArgument($help_argument);

		// add verbosity
		$verbosity = new SiteCommandLineArgument(
			array('-v', '--verbose'), 'setVerbosity',
			Site::_('Sets the level of verbosity of this application. Pass 0 '.
				'to turn off all output.'));

		$verbosity->addParameter('integer',
			Site::_('--verbose expects a level between 0 and 2.'),
			self::VERBOSITY_ALL);

		$this->addCommandLineArgument($verbosity);
	}

	// }}}
	// {{{ public function addCommandLineArgument()

	/**
	 * 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:
	 *
	 * <code>
	 * $foo_argument = new SiteCommandLineArgument(array('-f', '--foo'), 'foo',
	 *     'Runs the foo() method.');
	 *
	 * $app->addCommandLineArgument($foo_argument);
	 * </code>
	 *
	 * @param SiteCommandLineArgument $argument the command line argument to
	 *                                           add.
	 */
	public function addCommandLineArgument(SiteCommandLineArgument $argument)
	{
		$this->arguments[] = $argument;
	}

	// }}}
	// {{{ public function setVerbosity()

	/**
	 * Sets the level of verbosity to use for this application
	 *
	 * @param integer $verbosity the level of verbosity to use.
	 */
	public function setVerbosity($verbosity)
	{
		$this->verbosity = (integer)$verbosity;
	}

	// }}}
	// {{{ public function setApplicationDirectory()

	/**
	 * Sets the application directory
	 *
	 * @param string $directory the directory to use.
	 */
	public function setApplicationDirectory($directory)
	{
		chdir($directory);
	}

	// }}}
	// {{{ public function displayUsage()

	/**
	 * Displays usage information for this command line application
	 */
	public function displayUsage()
	{
		echo $this->title, "\n\n";
		if ($this->documentation !== null)
			echo $this->documentation, "\n\n";

		echo Site::_('OPTIONS'), "\n";

		foreach ($this->arguments as $argument) {
			$this->displayArgumentUsage($argument);
		}

		exit(0);
	}

	// }}}
	// {{{ public function displayArgumentUsage()

	/**
	 * Displays usage information for a single command line argument
	 *
	 * @param SiteCommandLineArgument $argument the command line argument for
	 *                                           which to display usage
	 *                                           information.
	 */
	public function displayArgumentUsage(SiteCommandLineArgument $argument)
	{
		echo implode(', ', $argument->getNames()), "\n",
			"   ", $argument->getDocumentation(), "\n\n";
	}

	// }}}
	// {{{ public function getScriptDirectory()

	/**
	 * Gets an absolute path to the directory in which the currently running
	 * script resides
	 *
	 * @return string the absolute path to the directory in which the currently
	 *                running script resides.
	 */
	public function getScriptDirectory()
	{
		return dirname(realpath($_SERVER['SCRIPT_FILENAME']));
	}

	// }}}
	// {{{ public function run()

	/**
	 * Run the application. Includes boilerplate init of modules and command
	 * line arguments.
	 */
	public function run()
	{
		$this->initModules();
		$this->parseCommandLineArguments();
	}

{'fraction_non_alphanumeric': 0.11736465026959154, 'fraction_numerical': 0.002363542359110717, 'mean_word_length': 2.970674486803519, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 20, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6521309', 'n_tokens_mistral': 3996, 'n_tokens_neox': 3564, 'n_words': 1247}