[limb-svn] r5456 - in 3.x/limb/cli: bin src tests/cases
svn at limb-project.com
svn at limb-project.com
Mon Apr 2 12:07:00 MSD 2007
Author: pachanga
Date: 2007-04-02 12:07:00 +0400 (Mon, 02 Apr 2007)
New Revision: 5456
URL: http://fisheye.limb-project.com/changelog/limb/?cs=5456
Modified:
3.x/limb/cli/bin/limb
3.x/limb/cli/src/lmbCliBaseCmd.class.php
3.x/limb/cli/src/lmbCliInput.class.php
3.x/limb/cli/src/lmbCliRunner.class.php
3.x/limb/cli/tests/cases/lmbCliInputTest.class.php
3.x/limb/cli/tests/cases/lmbCliRunnerTest.class.php
Log:
-- lmbCliInput :: strictMode($flag) added, it allows to control whether lmbCliInput should treat input as strict, e.g. throw error on missing option, etc. While in not strict mode options which are not declared explicitly are simply added.
-- lmbCliRunner passes shifted $argv into concrete lmbCliBaseCmd mapped method
-- lmbCliBaseCmd :: __construct(..) now explicitly requires lmbCliResponse
-- lmbCliBaseCmd :: execute(..) now accepts raw $argv
-- lmbCliBaseCmd :: set/getInput,Output methods removed
Modified: 3.x/limb/cli/bin/limb
===================================================================
--- 3.x/limb/cli/bin/limb 2007-04-01 19:27:12 UTC (rev 5455)
+++ 3.x/limb/cli/bin/limb 2007-04-02 08:07:00 UTC (rev 5456)
@@ -16,6 +16,7 @@
$output->setVerbose(true);
$input = new lmbCliInput('h|help;c|config=');
+$input->strictMode(false);
$input->read();
$config = false;
Modified: 3.x/limb/cli/src/lmbCliBaseCmd.class.php
===================================================================
--- 3.x/limb/cli/src/lmbCliBaseCmd.class.php 2007-04-01 19:27:12 UTC (rev 5455)
+++ 3.x/limb/cli/src/lmbCliBaseCmd.class.php 2007-04-02 08:07:00 UTC (rev 5456)
@@ -9,37 +9,21 @@
* @version $Id$
* @package cli
*/
+lmb_require('limb/cli/src/lmbCliInput.class.php');
+
abstract class lmbCliBaseCmd
{
- protected $input;
- protected $output;
-
- function setInput($input)
+ function __construct($output)
{
- $this->input = $input;
- }
-
- function getInput()
- {
- return $this->input;
- }
-
- function setOutput($output)
- {
$this->output = $output;
}
- function getOutput()
+ function help($argv)
{
- return $this->output;
- }
-
- function execute()
- {
return 0;
}
- function help($action = '')
+ function execute($argv)
{
return 0;
}
Modified: 3.x/limb/cli/src/lmbCliInput.class.php
===================================================================
--- 3.x/limb/cli/src/lmbCliInput.class.php 2007-04-01 19:27:12 UTC (rev 5455)
+++ 3.x/limb/cli/src/lmbCliInput.class.php 2007-04-02 08:07:00 UTC (rev 5456)
@@ -15,6 +15,7 @@
protected $options = array();
protected $arguments = array();
protected $throw_exception = false;
+ protected $strict_mode = true;
protected $argv;
function __construct()
@@ -23,6 +24,11 @@
$this->_addOptions($args);
}
+ function strictMode($flag = true)
+ {
+ $this->strict_mode = $flag;
+ }
+
function setMinimumArguments($minimum_args)
{
$this->minimum_args = $minimum_args;
@@ -109,6 +115,11 @@
return isset($this->arguments[$index]) ? $this->arguments[$index] : $default;
}
+ function addOption($option)
+ {
+ $this->options[] = $option;
+ }
+
function getOptions()
{
return $this->options;
@@ -203,7 +214,7 @@
elseif($postponed_option->isValueForbidden())
unset($postponed_option);
}
- elseif(isset($postponed_option))
+ elseif(isset($postponed_option) && $this->strict_mode)
{
$postponed_option->setValue($arg);
unset($postponed_option);
@@ -267,7 +278,15 @@
protected function _approveOption($name)
{
if(!$option = $this->getOption($name))
- throw new lmbCliException("Option '{$name}' is illegal");
+ {
+ if(!$this->strict_mode)
+ {
+ $option = new lmbCliOption($name);
+ $this->addOption($option);
+ }
+ else
+ throw new lmbCliException("Option '{$name}' is illegal");
+ }
$option->touch();
return $option;
Modified: 3.x/limb/cli/src/lmbCliRunner.class.php
===================================================================
--- 3.x/limb/cli/src/lmbCliRunner.class.php 2007-04-01 19:27:12 UTC (rev 5455)
+++ 3.x/limb/cli/src/lmbCliRunner.class.php 2007-04-02 08:07:00 UTC (rev 5456)
@@ -67,8 +67,8 @@
if(!$command = $this->_mapCommandToObject($command_name))
$this->_error("Command '$command_name' is invalid(could not map it to the command class)");
- $args = $this->input->getArguments();
- array_shift($args);
+ $argv = $this->input->getArgv();
+ array_shift($argv);
$action = 'execute';
@@ -78,20 +78,10 @@
if(method_exists($command, $method))
{
$action = $method;
- array_shift($args);
+ array_shift($argv);
}
}
-
- $input = new lmbCliInput();
- $input->read($args, false);
-
- $command->setInput($input);
- $command->setOutput($this->output);
-
- if($this->input->isOptionPresent('help'))
- return $command->help($action);
-
- return $this->_exit((int)$command->$action());
+ return $this->_exit((int)$command->$action($argv));
}
protected function _exit($code = 0)
@@ -121,7 +111,7 @@
if($resolved = lmb_glob($path))
{
require_once($resolved[0]);
- return new $class();
+ return new $class($this->output);
}
}
}
Modified: 3.x/limb/cli/tests/cases/lmbCliInputTest.class.php
===================================================================
--- 3.x/limb/cli/tests/cases/lmbCliInputTest.class.php 2007-04-01 19:27:12 UTC (rev 5455)
+++ 3.x/limb/cli/tests/cases/lmbCliInputTest.class.php 2007-04-02 08:07:00 UTC (rev 5456)
@@ -199,5 +199,15 @@
$this->assertNull($cli->getOptionValue('b'));
$this->assertEqual($cli->getOptionValue('k'), 2);
}
+
+ function testUseRelaxedMode()
+ {
+ $argv = array('foo.php', 'arg1', '--opt1', 'arg2', 'arg3');
+ $cli = new lmbCliInput();
+ $cli->strictMode(false);
+ $this->assertTrue($cli->read($argv));
+ $this->assertTrue($cli->isOptionPresent('opt1'));
+ $this->assertEqual($cli->getArguments(), array('arg1', 'arg2', 'arg3'));
+ }
}
?>
\ No newline at end of file
Modified: 3.x/limb/cli/tests/cases/lmbCliRunnerTest.class.php
===================================================================
--- 3.x/limb/cli/tests/cases/lmbCliRunnerTest.class.php 2007-04-01 19:27:12 UTC (rev 5455)
+++ 3.x/limb/cli/tests/cases/lmbCliRunnerTest.class.php 2007-04-02 08:07:00 UTC (rev 5456)
@@ -123,7 +123,7 @@
$this->assertEqual($runner->execute(), 1);
}
- function testSanitizeAction()
+ function testSanitizeActionName()
{
$input = new lmbCliInput();
$output = new lmbCliResponse();
@@ -140,6 +140,41 @@
$this->assertEqual($runner->execute(), 1);
}
+ function testPassArgvToAction()
+ {
+ $input = new lmbCliInput();
+ $output = new lmbCliResponse();
+
+ $input->strictMode(false);
+ $input->read(array('foo.php', $cmd = $this->_randomName(), 'foo', '--dry-run', '-c', 'bar'));
+
+ $runner = new lmbCliRunner($input, $output);
+ $runner->setCommandSearchPath($this->tmp_dir);
+ $runner->returnOnExit();
+ $runner->throwOnError();
+
+ $this->_createCommandClass($cmd, 'function foo($argv){var_dump($argv);}');
+
+ ob_start();
+ $runner->execute();
+ $str = ob_get_contents();
+ ob_end_clean();
+
+ $expected = <<<EOD
+array(3) {
+ [0]=>
+ string(9) "--dry-run"
+ [1]=>
+ string(2) "-c"
+ [2]=>
+ string(3) "bar"
+}
+
+EOD;
+
+ $this->assertEqual($expected, $str);
+ }
+
function _createCommandClass($name, $body='')
{
$class = lmbCliRunner :: commandToClass($name);
More information about the limb-svn
mailing list