[Solar-svn] Revision 2584
pmjones at solarphp.com
pmjones at solarphp.com
Tue Jul 10 16:00:42 CDT 2007
Solar_Cli_*: [NEW] Test placeholders for CLI-based apps (commands).
Added: trunk/Solar/Cli/Base/Info/help.txt
===================================================================
Added: trunk/Solar/Cli/Base/Info/options.php
===================================================================
--- trunk/Solar/Cli/Base/Info/options.php (rev 0)
+++ trunk/Solar/Cli/Base/Info/options.php 2007-07-10 21:00:42 UTC (rev 2584)
@@ -0,0 +1,13 @@
+<?php
+return array(
+ 'verbose' => array(
+ 'long' => 'verbose',
+ 'short' => 'V',
+ 'descr' => 'Display verbose output when available.',
+ ),
+ 'version' => array(
+ 'long' => 'version',
+ 'short' => 'v',
+ 'descr' => 'Display version information and exit.',
+ ),
+);
\ No newline at end of file
Added: trunk/Solar/Cli/Base/Locale/en_US.php
===================================================================
--- trunk/Solar/Cli/Base/Locale/en_US.php (rev 0)
+++ trunk/Solar/Cli/Base/Locale/en_US.php 2007-07-10 21:00:42 UTC (rev 2584)
@@ -0,0 +1,8 @@
+<?php
+return array(
+ 'ERR_NO_COMMAND' => 'No command given.',
+ 'ERR_NO_HELP' => 'No help is available for this command.',
+ 'ERR_UNKNOWN_COMMAND' => 'Unknown command: {:cmd}',
+ 'HELP_TRY_SOLAR_HELP' => 'Try %Ksolar help%n for a list of commands.',
+ 'HELP_VALID_OPTIONS' => 'Valid options for this command are ...',
+);
\ No newline at end of file
Added: trunk/Solar/Cli/Base.php
===================================================================
--- trunk/Solar/Cli/Base.php (rev 0)
+++ trunk/Solar/Cli/Base.php 2007-07-10 21:00:42 UTC (rev 2584)
@@ -0,0 +1,65 @@
+<?php
+class Solar_Cli_Base extends Solar_Controller_Command {
+
+ /**
+ *
+ * Displays a "command not recognized" message.
+ *
+ * @param string $cmd The requested command.
+ *
+ * @return void
+ *
+ */
+ protected function _exec($cmd = null)
+ {
+ if ($cmd) {
+ $this->_println('ERR_UNKNOWN_COMMAND', 1, array('cmd' => $cmd));
+ } else {
+ $this->_println('ERR_NO_COMMAND');
+ }
+
+ $this->_println("HELP_TRY_SOLAR_HELP");
+ }
+
+ /**
+ *
+ * Pre-exec logic.
+ *
+ * Catches --version and -v to display version information and exit.
+ *
+ * @return bool True to skip _exec(), false otherwise.
+ *
+ */
+ protected function _preExec()
+ {
+ $skip_exec = false;
+
+ switch (true) {
+
+ case $this->_options['version']:
+ $this->_print("Solar command-line tool, version ");
+ $this->_println($this->apiVersion() . '.');
+ $skip_exec = true;
+ break;
+
+ }
+
+ return $skip_exec;
+ }
+
+ /**
+ *
+ * Post-execution logic.
+ *
+ * @return void
+ *
+ */
+ protected function _postExec()
+ {
+ parent::_postExec();
+
+ // return terminal to normal colors
+ $this->_print("%n");
+ }
+}
+
Added: trunk/Solar/Cli/Help/Info/help.txt
===================================================================
Added: trunk/Solar/Cli/Help.php
===================================================================
--- trunk/Solar/Cli/Help.php (rev 0)
+++ trunk/Solar/Cli/Help.php 2007-07-10 21:00:42 UTC (rev 2584)
@@ -0,0 +1,77 @@
+<?php
+class Solar_Cli_Help extends Solar_Cli_Base {
+
+ protected function _exec($cmd = null)
+ {
+ if ($cmd) {
+ $this->_displayCommandHelp($cmd);
+ } else {
+ $this->_displayCommandList();
+ }
+ }
+
+ // need access to the Console app stack, and the routing map.
+ // inject these into every command, or duplicate logic here?
+ // or add queryable methods so we can get command synonyms?
+ protected function _displayCommandHelp($cmd = null)
+ {
+ $this->_println();
+
+ // the list of known command-to-class mappings
+ $list = $this->_console->getCommandList();
+
+ // is this a known command?
+ if (empty($list[$cmd])) {
+ $this->_println('ERR_UNKNOWN_COMMAND', 1, array('cmd' => $cmd));
+ return;
+ }
+
+ $class = $list[$cmd];
+ $obj = Solar::factory($class);
+ $help = $obj->getInfoHelp();
+ if ($help) {
+ $this->_println($help);
+ } else {
+ $this->_println('ERR_NO_HELP');
+ }
+
+ $this->_println();
+
+ $opts = $obj->getInfoOptions();
+ if ($opts) {
+
+ $this->_println('HELP_VALID_OPTIONS');
+ $this->_println();
+
+ foreach ($opts as $key => $val) {
+ $this->_println($key);
+ $val = str_replace("\n", "\n ", wordwrap(": $val"));
+ $this->_println($val);
+ $this->_println();
+ }
+ }
+ }
+
+ protected function _displayCommandList()
+ {
+ $text = <<<TEXT
+Solar command-line tool.
+Usage: %Ksolar <command> <options> <params>%n
+
+The solar command-line tool helps with common tasks.
+
+Try 'solar help <command>' for help on a specific command.
+
+Available commands:
+TEXT;
+
+ // print the main text
+ $this->_println($text);
+
+ // now get the list of available commands
+ $list = $this->_console->getCommandList();
+ foreach ($list as $key => $val) {
+ $this->_println(" $key");
+ }
+ }
+}
\ No newline at end of file
Added: trunk/Solar/Cli/RunTests.php
===================================================================
--- trunk/Solar/Cli/RunTests.php (rev 0)
+++ trunk/Solar/Cli/RunTests.php 2007-07-10 21:00:42 UTC (rev 2584)
@@ -0,0 +1,7 @@
+<?php
+class Solar_Cli_RunTests extends Solar_Cli_Base {
+ protected function _exec()
+ {
+ $this->_println('found the test runner');
+ }
+}
\ No newline at end of file
More information about the Solar-svn
mailing list